/* eslint-disable react-hooks/exhaustive-deps */ import { useEffect, useRef } from "react"; import useModalStore from "../store/modalStore"; import clsx from "clsx"; function ModalContainer() { const { modal, setModal, position } = useModalStore(); const divRef = useRef(null); const backdropRef = useRef(null); const modalRef = useRef(null); function handleResize() { if (!modalRef.current) return; if (divRef.current!.clientHeight > modalRef.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); }; }, []); return ( modal && (
setModal(null)} /> {modal}
) ); } export default ModalContainer;