import React, {forwardRef, useEffect, useRef, useState} from 'react'; import type {NotificationProps} from '../../typings/hyper'; const Notification = forwardRef>((props, ref) => { const dismissTimer = useRef(undefined); const [dismissing, setDismissing] = useState(false); useEffect(() => { setDismissTimer(); }, []); useEffect(() => { // if we have a timer going and the notification text // changed we reset the timer resetDismissTimer(); setDismissing(false); }, [props.text]); const handleDismiss = () => { setDismissing(true); }; const onElement = (el: HTMLDivElement | null) => { if (el) { el.addEventListener('webkitTransitionEnd', () => { if (dismissing) { props.onDismiss(); } }); const {backgroundColor} = props; if (backgroundColor) { el.style.setProperty('background-color', backgroundColor, 'important'); } if (ref) { if (typeof ref === 'function') ref(el); else ref.current = el; } } }; const setDismissTimer = () => { if (typeof props.dismissAfter === 'number') { dismissTimer.current = setTimeout(() => { handleDismiss(); }, props.dismissAfter); } }; const resetDismissTimer = () => { clearTimeout(dismissTimer.current); setDismissTimer(); }; useEffect(() => { return () => { clearTimeout(dismissTimer.current); }; }, []); const {backgroundColor, color} = props; const opacity = dismissing ? 0 : 1; return (
{props.customChildrenBefore} {props.children || props.text} {props.userDismissable ? ( [x] ) : null} {props.customChildren}
); }); Notification.displayName = 'Notification'; export default Notification;