import {useEffect, useState} from "react" interface ModalProps { show: boolean children?: JSX.Element | null onClose: () => void customId?: string ignoreInputs?: boolean } export default function Modal({show, onClose, children, customId, ignoreInputs}: ModalProps) { const [createdAt] = useState(Date.now()) useEffect(() => { if (!show) return; function handleClose(e: any) { if (!ignoreInputs && !document.getElementById(customId || 'modal-content')?.contains(e.target) && Date.now() - createdAt > 1000) { onClose() } } document.addEventListener('click', handleClose) document.body.style.overflow = "hidden" return () => { document.removeEventListener('click', handleClose) document.body.style.overflow = "visible" } }, [show, onClose, customId, createdAt]) if (!show) { return <> } return (
{children}
) }