UNPKG

koval-ui

Version:

React components collection with minimalistic design. Supports theming, layout, and input validation.

30 lines (29 loc) 977 B
"use client"; import { useRef as m, useState as b, useEffect as a, useCallback as y } from "react"; import { useEventListener as E, EventType as h } from "./useEventListener.js"; const k = (c, t, r = !0) => { const s = m(null), [u, n] = b([]); a(() => { if (r && t) { s.current = c?.querySelectorAll( 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' ); const e = s.current ? s.current[0] : null, l = s.current ? s.current[s.current.length - 1] : null; n([e, l]); } }, [t, c, r]), a(() => { r && t && u[0]?.focus(); }, [r, u, t]); const o = y( (e) => { const [l, f] = u; r && t && e.key === "Tab" && (e.shiftKey && document.activeElement === l ? (e.preventDefault(), f.focus()) : !e.shiftKey && document.activeElement === f && (e.preventDefault(), l.focus())); }, [r, u, t] ); E(h.keydown, o); }; export { k as useFocusTrap }; //# sourceMappingURL=useFocusTrap.js.map