@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
23 lines (22 loc) • 1.05 kB
JavaScript
"use client";
import { findTabbableDescendants } from "./tabbable.mjs";
//#region packages/@mantine/hooks/src/use-focus-trap/scope-tab.ts
function scopeTab(node, event) {
const tabbable = findTabbableDescendants(node);
if (!tabbable.length) {
event.preventDefault();
return;
}
const finalTabbable = tabbable[event.shiftKey ? 0 : tabbable.length - 1];
const root = node.getRootNode();
let leavingFinalTabbable = finalTabbable === root.activeElement || node === root.activeElement;
const activeElement = root.activeElement;
if (activeElement.tagName === "INPUT" && activeElement.getAttribute("type") === "radio") leavingFinalTabbable = tabbable.filter((element) => element.getAttribute("type") === "radio" && element.getAttribute("name") === activeElement.getAttribute("name")).includes(finalTabbable);
if (!leavingFinalTabbable) return;
event.preventDefault();
const target = tabbable[event.shiftKey ? tabbable.length - 1 : 0];
if (target) target.focus();
}
//#endregion
export { scopeTab };
//# sourceMappingURL=scope-tab.mjs.map