mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-27 22:30:56 +00:00
33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
'use client';
|
|
import { findTabbableDescendants } from './tabbable.mjs';
|
|
|
|
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;
|
|
const activeElementIsRadio = activeElement.tagName === "INPUT" && activeElement.getAttribute("type") === "radio";
|
|
if (activeElementIsRadio) {
|
|
const activeRadioGroup = tabbable.filter(
|
|
(element) => element.getAttribute("type") === "radio" && element.getAttribute("name") === activeElement.getAttribute("name")
|
|
);
|
|
leavingFinalTabbable = activeRadioGroup.includes(finalTabbable);
|
|
}
|
|
if (!leavingFinalTabbable) {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
const target = tabbable[event.shiftKey ? tabbable.length - 1 : 0];
|
|
if (target) {
|
|
target.focus();
|
|
}
|
|
}
|
|
|
|
export { scopeTab };
|
|
//# sourceMappingURL=scope-tab.mjs.map
|