import React, { forwardRef, useEffect, useRef, useState, type HTMLAttributes } from 'react' import classnames from 'classnames' import { type HeadingProps } from '~components/Heading' import { type NotificationVariant } from '~components/Notification/types' import { type OverrideClassName } from '~components/types/OverrideClassName' import { isRefObject } from '~components/utils/isRefObject' import { CancelButton } from '../CancelButton' import { NotificationHeading } from '../NotificationHeading' import { NotificationIconVariant } from '../NotificationIcon' import styles from './GenericNotification.module.css' type GenericNotificationBase = { style: 'global' | 'inline' | 'toast' children?: React.ReactNode title?: string persistent?: boolean onHide?: () => void noBottomMargin?: boolean forceMultiline?: boolean headingProps?: HeadingProps } & Omit>, 'style'> export type GenericNotificationVariant = { variant: NotificationVariant } export type GenericNotificationProps = GenericNotificationBase & GenericNotificationVariant export const GenericNotification = forwardRef( ( { variant, style, children, title, persistent = false, onHide, noBottomMargin, forceMultiline, headingProps, classNameOverride, ...restProps }, ref, ): JSX.Element | null => { const [isHidden, setIsHidden] = useState(true) const [isRemoved, setIsRemoved] = useState(false) const fallbackRef = useRef(null) const containerRef = isRefObject(ref) ? ref : fallbackRef useEffect(() => { requestAnimationFrame(() => { if (containerRef.current) { setIsHidden(false) } }) }, [containerRef]) const getMarginTop = (): string => { if (isHidden && containerRef.current) { return -containerRef.current.clientHeight + 'px' } return '0' } const onTransitionEnd = (e: React.TransitionEvent): void => { // Be careful: this assumes the final CSS property to be animated is "margin-top". if (isHidden && e.propertyName === 'margin-top') { setIsRemoved(true) onHide?.() } } if (isRemoved) { return null } return (
{style !== 'global' && ( )} {children &&
{children}
}
{!persistent && setIsHidden(true)} />}
) }, ) GenericNotification.displayName = 'GenericNotification'