/* eslint-disable react-hooks/exhaustive-deps */ import { useEffect, useRef } from "react"; import useModalStore from "../stores/useModalStore"; import { AnimatePresence, motion } from "motion/react"; import CloseIcon from "./icons/CloseIcon"; import { clsx as cn } from "clsx"; import Button from "./Button"; function ModalContainer() { const { modal, setModal, position } = useModalStore(); const divRef = useRef(null); const backdropRef = useRef(null); const popoverRef = useRef(null); const containerRef = useRef(null); function handleResize() { if (!popoverRef.current) return; if (divRef.current!.clientHeight > popoverRef.current!.clientHeight) { backdropRef.current!.style.height = `${divRef.current!.clientHeight}px`; } else { backdropRef.current!.style.height = `100%`; } } function handleKeydown(e: KeyboardEvent) { if (e.key !== "Escape") return; setModal(null); } useEffect(() => { window.addEventListener("resize", handleResize); window.addEventListener("keydown", handleKeydown); return () => { window.removeEventListener("resize", handleResize); window.removeEventListener("keydown", handleKeydown); }; }, []); useEffect(() => { if (!modal) return; document.body.style.overflow = "hidden"; return () => { document.body.style.overflow = "auto"; }; }, [modal]); return ( {modal && (
setModal(null)} />
{modal}
)} ); } export default ModalContainer;