import { useEffect, RefObject } from 'react'; export function useOutsideAlerter( ref: RefObject, handler: () => void, additionalDeps: unknown[], ) { useEffect(() => { function handleClickOutside(this: Document, event: MouseEvent) { if (ref.current && !ref.current.contains(event.target as Node)) { handler(); } } document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [ref, ...additionalDeps]); }