The weeb for the next gen discord boat - Wamellow wamellow.com
bot discord
at master 784 B view raw
1import { useEffect } from "react"; 2 3/** 4 * Hook that handles closure of a component when clicking outside of a "wamellow-modal" class. 5 * This component must be mounted within the component that is being closed. 6 */ 7export function ClickOutside({ 8 onClose 9}: { 10 onClose: () => void; 11}) { 12 13 useEffect(() => { 14 const handleDocumentClick = (event: MouseEvent): void => { 15 16 // @ts-expect-error -- It think's closest doesn't exist, but it does 17 if (!event.target?.closest(".wamellow-modal")) { 18 onClose(); 19 } 20 }; 21 22 document.addEventListener("click", handleDocumentClick); 23 24 return () => { 25 document.removeEventListener("click", handleDocumentClick); 26 }; 27 }, []); 28 29 return <></>; 30}