import React, { FC, ReactNode, useCallback, useEffect, useRef } from 'react'; import classNames from 'classnames'; import { NotificationOptions } from './NotificationsContext'; import { motion } from 'framer-motion'; import css from './Notification.module.css'; export interface NotificationProps extends NotificationOptions { id: number; component?: ReactNode; onClose: (id: number) => void; } export const Notification: FC = ({ id, title, showClose, body, timeout, className, variant, component, onClose }) => { const timeoutRef = useRef(null); const clearTimer = useCallback(() => clearTimeout(timeoutRef.current), []); const startTimer = useCallback(() => { clearTimer(); timeoutRef.current = setTimeout(() => onClose?.(id), timeout); }, [id, timeout, onClose, clearTimer]); useEffect(() => { startTimer(); return () => clearTimer(); }, [clearTimer, startTimer]); return ( {component} {!component && (
{title &&
{title}
} {body && (
{typeof body === 'string' ? ( ) : ( body )}
)}
{showClose && ( )}
)}
); };