@anoki/fse-ui
Version:
FSE UI components library
19 lines (18 loc) • 736 B
JavaScript
import { useEffect as l } from "react";
const f = (t, n) => {
l(() => {
if (!n || !t.current) return;
const o = t.current.querySelectorAll(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
), c = o[0], r = o[o.length - 1], s = (e) => {
e.key === "Tab" && (e.shiftKey ? document.activeElement === c && (r.focus(), e.preventDefault()) : document.activeElement === r && (c.focus(), e.preventDefault()));
}, u = document.activeElement;
return c.focus(), document.addEventListener("keydown", s), () => {
document.removeEventListener("keydown", s), n && (u == null || u.focus());
};
}, [n, t]);
};
export {
f as useFocusTrap
};
//# sourceMappingURL=index.es477.js.map