/* eslint-disable @typescript-eslint/no-empty-function */ /* eslint-env browser */ import React, { useEffect, useLayoutEffect, useRef, FC, } from 'react'; import AUButtonComponent from './AUButtonComponent'; const dismissTimeout = 5000; // ReturnType frem for NodeJS.Timeout: biblioteket koerer i // browseren og skal ikke afhaenge af @types/node. TS 6 auto-inkluderer heller // ikke laengere @types via typeRoots, saa NodeJS-namespacet er ikke garanteret. let timeoutId: ReturnType | null = null; type Props = { message: IToastMessage; buttonText?: string; dismiss?: () => void; onOpen?: () => void; }; const AUToastComponent: FC = ({ message: pMessage, buttonText = 'OK', dismiss = () => { }, onOpen = () => { }, }) => { const { type, message, header } = pMessage; const toastRef = useRef(null); const persistent = type !== 'confirm'; const moveOut = (element: HTMLElement): void => { const moveOutClass = 'toast-notification--move-out'; element.classList.add(moveOutClass); const moveOutElement = document.querySelector(`.${moveOutClass}`); if (moveOutElement && typeof dismiss === 'function') { moveOutElement.addEventListener('animationend', () => { dismiss(); }); } }; useEffect(() => { if (typeof onOpen === 'function') { onOpen(); } }, []); useLayoutEffect(() => { const element = toastRef.current; if (!persistent) { if (element) { element.addEventListener('animationend', () => { timeoutId = setTimeout(() => { moveOut(element); }, dismissTimeout); }); element.addEventListener('mouseenter', () => { if (timeoutId) { clearTimeout(timeoutId); element.addEventListener('mouseleave', () => { moveOut(element); }); } }); } } else { element?.querySelector('button')?.focus(); } }, []); let className = 'toast-notification'; if (persistent) { className = `${className} toast-notification--persistent`; } if (type !== 'default') { className = `${className} toast-notification--${type}`; } return (
{ header && (

{header}

) }

{ persistent && ( { if (toastRef.current) { moveOut(toastRef.current); } }} /> ) }
); }; AUToastComponent.displayName = 'AUToastComponent'; export default AUToastComponent;