@hitachivantara/uikit-react-core
Version:
Core React components for the NEXT Design System.
30 lines (29 loc) • 857 B
JavaScript
const SELECTORS = 'input, button, select, textarea, a[href], [tabindex]:not([tabindex="-1"])';
const getFocusableList = (node) => node?.querySelectorAll(SELECTORS) || [];
const 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]
};
};
const getFirstAndLastFocus = (node) => {
const focusableList = getFocusableList(node);
return {
first: focusableList[0] || null,
last: focusableList[focusableList.length - 1] || null
};
};
export {
getPrevNextFocus as default,
getFirstAndLastFocus,
getFocusableList,
getPrevNextFocus
};