Skip to content

Instantly share code, notes, and snippets.

@pl12133
Last active September 4, 2017 15:15
Show Gist options
  • Save pl12133/186ec3b599aba9a541e1ab4dafc9d593 to your computer and use it in GitHub Desktop.
Save pl12133/186ec3b599aba9a541e1ab4dafc9d593 to your computer and use it in GitHub Desktop.
function isInert(node) {
// See https://www.w3.org/TR/html5/editing.html#inert
let sty = getComputedStyle(node);
return node.offsetHeight <= 0 || /hidden/.test(sty.getPropertyValue('visibility'));
}
function focusNext(e) {
// Selector lifted from `jkup/focusable.git`
let focusable = Array.from(document.querySelectorAll('a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex="0"], [contenteditable], audio[controls], video[controls]')),
step = e && e.shiftKey ? -1 : 1,
activeIndex = focusable.indexOf(document.activeElement),
nextActiveIndex = activeIndex + step,
nextActive = focusable[nextActiveIndex];
// Skip inert elements.
while (nextActive && isInert(nextActive)) { nextActive = focusable[(nextActiveIndex += step)]; }
if (nextActive) {
nextActive.focus();
e && e.preventDefault();
}
else {
// Allow focus to leave the document when there is no nextActive
document.activeElement.blur();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment