UNPKG

@hitachivantara/uikit-react-core

Version:
26 lines (25 loc) 989 B
//#region src/utils/focusableElementFinder.ts var SELECTORS = "input, button, select, textarea, a[href], [tabindex]:not([tabindex=\"-1\"])"; /** Gets the list of focusable elements. */ var getFocusableList = (node) => node?.querySelectorAll(SELECTORS) || []; /** Auxiliary function to find adjacent nodes to focus. */ var getPrevNextFocus = (nodeId) => { const nodes = getFocusableList(document); const nbNodes = nodes.length; let index = 0; for (; index < nbNodes; index += 1) if (nodes[index].id === nodeId) break; return { nextFocus: nodes[index + 1 > nbNodes - 1 ? 0 : index + 1], prevFocus: nodes[index - 1 < 0 ? nbNodes - 1 : index - 1] }; }; /** Get the first and last focusable element from a node. */ var getFirstAndLastFocus = (node) => { const focusableList = getFocusableList(node); return { first: focusableList[0] || null, last: focusableList[focusableList.length - 1] || null }; }; //#endregion export { getPrevNextFocus as default, getFirstAndLastFocus };