import * as React from 'react'; import { clsx } from 'clsx'; import useNoticeTimer from './hooks/useNoticeTimer'; import { isNonNullable, isReactRenderable, useEvent } from '@rc-component/util'; import useClosable, { type ClosableType } from './hooks/useClosable'; import DefaultProgress from './Progress'; import type { NotificationProgressProps } from './Progress'; export interface NotificationClassNames { wrapper?: string; root?: string; icon?: string; section?: string; title?: string; description?: string; actions?: string; close?: string; progress?: string; } export interface NotificationStyles { wrapper?: React.CSSProperties; root?: React.CSSProperties; icon?: React.CSSProperties; section?: React.CSSProperties; title?: React.CSSProperties; description?: React.CSSProperties; actions?: React.CSSProperties; close?: React.CSSProperties; progress?: React.CSSProperties; } export interface ComponentsType { progress?: React.ComponentType; } export interface NotificationProps { // Style prefixCls: string; className?: string; style?: React.CSSProperties; classNames?: NotificationClassNames; styles?: NotificationStyles; components?: ComponentsType; // UI title?: React.ReactNode; description?: React.ReactNode; icon?: React.ReactNode; actions?: React.ReactNode; role?: string; closable?: ClosableType; offset?: number; notificationIndex?: number; stackInThreshold?: boolean; props?: React.HTMLAttributes & Record; // Behavior duration?: number | false | null; showProgress?: boolean; times?: number; hovering?: boolean; pauseOnHover?: boolean; // Function onClick?: React.MouseEventHandler; onMouseEnter?: React.MouseEventHandler; onMouseLeave?: (event?: React.MouseEvent) => void; /** @deprecated Please use `closable.onClose` instead. */ onClose?: () => void; } const Notification = React.forwardRef((props, ref) => { const { // Style prefixCls, className, style, classNames, styles, components, // UI title, description, icon, actions, role, closable, offset, notificationIndex, stackInThreshold, props: rootProps, // Behavior duration = 4.5, showProgress, hovering: forcedHovering, pauseOnHover = true, // Function onClick, onMouseEnter, onMouseLeave, onClose, } = props; const [percent, setPercent] = React.useState(0); const noticePrefixCls = `${prefixCls}-notice`; // ========================= Close ========================== const [mergedClosable, closableConfig, closeBtnAriaProps] = useClosable(closable); const onInternalClose = useEvent(() => { closableConfig.onClose?.(); onClose?.(); }); // ======================== Duration ======================== const [hovering, setHovering] = React.useState(false); const [onResume, onPause] = useNoticeTimer(duration, onInternalClose, setPercent); const validPercent = 100 - Math.min(Math.max(percent * 100, 0), 100); const Progress = components?.progress || DefaultProgress; React.useEffect(() => { if (!pauseOnHover) { return; } if (forcedHovering) { onPause(); } else if (!hovering) { onResume(); } }, [forcedHovering, hovering, onPause, onResume, pauseOnHover]); // ========================= Hover ========================== function onInternalMouseEnter(event: React.MouseEvent) { setHovering(true); if (pauseOnHover) { onPause(); } onMouseEnter?.(event); } function onInternalMouseLeave(event?: React.MouseEvent) { setHovering(false); if (pauseOnHover && !forcedHovering) { onResume(); } onMouseLeave?.(event); } function onInternalCloseClick(event: React.MouseEvent) { event.preventDefault(); event.stopPropagation(); onInternalClose(); } const destroy = useEvent(() => { if (hovering) { onInternalMouseLeave(); } }); React.useEffect(() => destroy, [destroy]); // ======================== Position ======================== const offsetRef = React.useRef(offset); if (offset !== undefined) { offsetRef.current = offset; } const notificationIndexRef = React.useRef(notificationIndex); if (notificationIndex !== undefined) { notificationIndexRef.current = notificationIndex; } const mergedOffset = offset ?? offsetRef.current; const mergedNotificationIndex = notificationIndex ?? notificationIndexRef.current ?? 0; // ======================== Content ========================= const titleNode = isNonNullable(title) ? (
{title}
) : null; const descNode = isNonNullable(description) ? (
{description}
) : null; const hasTitle = titleNode !== null; const hasDescription = descNode !== null; let contentNode: React.ReactNode; if (hasTitle && hasDescription) { contentNode = (
{titleNode} {descNode}
); } else { contentNode = titleNode || descNode; } if (isNonNullable(icon)) { contentNode = (
{icon}
{contentNode}
); } const actionsNode = isReactRenderable(actions) ? (
{actions}
) : null; // ========================= Render ========================= const mergedStyle: React.CSSProperties & { '--notification-index'?: number; '--notification-y'?: string; } = { '--notification-index': mergedNotificationIndex, ...styles?.root, ...style, }; if (mergedOffset !== undefined) { mergedStyle['--notification-y'] = `${mergedOffset}px`; } const mergedRole = role ?? rootProps?.role ?? 'alert'; return (
{contentNode} {actionsNode} {mergedClosable && ( )} {showProgress && typeof duration === 'number' && duration > 0 && ( )}
); }); if (process.env.NODE_ENV !== 'production') { Notification.displayName = 'Notification'; } export default Notification;