Skip to content

Instantly share code, notes, and snippets.

@mxmason
Last active February 17, 2022 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxmason/c4fadc3bd55571bce511b497a36eb788 to your computer and use it in GitHub Desktop.
Save mxmason/c4fadc3bd55571bce511b497a36eb788 to your computer and use it in GitHub Desktop.
const FOCUSABLE_ELEMENTS = `
[contenteditable]:not([tabindex^="-"]):not([inert]):not([aria-hidden="true"]),
[tabindex]:not([tabindex^="-"]):not([inert]):not([aria-hidden="true"])
a[href]:not([tabindex^="-"]):not([inert]):not([aria-hidden="true"]),
area[href]:not([tabindex^="-"]):not([inert]):not([aria-hidden="true"]),
audio:not([tabindex^="-"]):not([inert]):not([aria-hidden="true"]),
button:not([disabled]):not([inert]):not([aria-hidden="true"]),
iframe:not([tabindex^="-"]):not([inert]):not([aria-hidden="true"]),
input:not([disabled]):not([inert]):not([aria-hidden="true"]),
select:not([disabled]):not([inert]):not([aria-hidden="true"]),
textarea:not([disabled]):not([inert]):not([aria-hidden="true"]),
video:not([tabindex^="-"]):not([inert]):not([aria-hidden="true"])`;
function _isNodeHidden(node) {
if (node.getClientRects().length === 0) {
return true;
} else {
return window.getComputedStyle(node).visibility === "hidden";
}
}
function getFocusableChildren(node) {
const elems = node.querySelectorAll(FOCUSABLE_ELEMENTS);
const focusableChildren = [];
const len = elems.length;
let elem;
let i;
for (i = 0; i < len; i++) {
elem = elems[i];
if (!_isNodeHidden(el)) {
focusableChildren.push(el);
}
}
return focusableChildren;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment