UNPKG

@konstructio/ui

Version:

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

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