UNPKG

@konstructio/ui

Version:

A set of reusable and customizable React components built for konstruct.io

28 lines (27 loc) 747 B
import { useContext as l, useRef as u, useEffect as s } from "react"; import { ModalContext as a } from "../contexts/Modal.context.js"; const f = () => { const t = l(a), n = u(null); if (!t) throw new Error("useModal must be used within a ModalProvider"); const { onClose: r } = t; return s(() => { var e; const o = document.activeElement; return (e = n.current) == null || e.focus(), () => { o == null || o.focus(); }; }, []), s(() => { const o = new AbortController(), e = (c) => { c.code === "Escape" && r(); }; return window.addEventListener("keydown", e, { signal: o.signal }), () => { o.abort(); }; }, [r]), { ...t, closeBtnRef: n }; }; export { f as useModal };