"use client"; import { Dispatch, SetStateAction, useCallback, useEffect, useRef, } from "react"; import FocusTrap from "focus-trap-react"; import { AnimatePresence, motion } from "framer-motion"; import Leaflet from "./leaflet"; import useWindowSize from "@/lib/hooks/useWindowSize"; export default function Modal({ children, showModal, setShowModal, }: { children: React.ReactNode; showModal: boolean; setShowModal: Dispatch>; }) { const desktopModalRef = useRef(null); const onKeyDown = useCallback( (e: KeyboardEvent) => { if (e.key === "Escape") { setShowModal(false); } }, [setShowModal], ); useEffect(() => { document.addEventListener("keydown", onKeyDown); return () => document.removeEventListener("keydown", onKeyDown); }, [onKeyDown]); const { isMobile, isDesktop } = useWindowSize(); useEffect(() => { if (showModal) { document.body.style.top = `-${window.scrollY}px`; document.body.style.position = "fixed"; } else { const scrollY = document.body.style.top; document.body.style.position = ""; document.body.style.top = ""; window.scrollTo(0, parseInt(scrollY || "0") * -1); } return () => { const scrollY = document.body.style.top; document.body.style.position = ""; document.body.style.top = ""; window.scrollTo(0, parseInt(scrollY || "0") * -1); }; }, [showModal]); return ( {showModal && ( <> {isMobile && {children}} {isDesktop && ( <> { if (desktopModalRef.current === e.target) { setShowModal(false); } }} >
{children}
setShowModal(false)} /> )} )}
); }