"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/use-window-size"; export default function Modal({ children, showModal, setShowModal, showBlur = true, }: { children: React.ReactNode; showModal: boolean; setShowModal: Dispatch>; showBlur?: boolean; }) { 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(); return ( {showModal && ( <> {isMobile && ( {children} )} {isDesktop && showBlur && ( <> { if (desktopModalRef.current === e.target) { setShowModal(false); } }} > {children} setShowModal(false)} /> )} )} ); }