koval-ui
Version:
React components collection with minimalistic design. Supports theming, layout, and input validation.
31 lines (30 loc) • 1.03 kB
JavaScript
"use client";
import { useRef as b, useState as y, useEffect as a, useCallback as E } from "react";
import { useEventListener as m, EventType as h } from "./useEventListener.js";
const k = (u, e, r = !0) => {
const s = b(null), [c, n] = y([]);
a(() => {
if (r && e) {
s.current = u == null ? void 0 : u.querySelectorAll(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
);
const t = s.current ? s.current[0] : null, f = s.current ? s.current[s.current.length - 1] : null;
n([t, f]);
}
}, [e, u, r]), a(() => {
var t;
r && e && ((t = c[0]) == null || t.focus());
}, [r, c, e]);
const o = E(
(t) => {
const [f, l] = c;
r && e && t.key === "Tab" && (t.shiftKey && document.activeElement === f ? (t.preventDefault(), l.focus()) : !t.shiftKey && document.activeElement === l && (t.preventDefault(), f.focus()));
},
[r, c, e]
);
m(h.keydown, o);
};
export {
k as useFocusTrap
};
//# sourceMappingURL=useFocusTrap.js.map