Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created October 10, 2018 10:52
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 barneycarroll/8bdda320d17497ce8a4d7532376da46c to your computer and use it in GitHub Desktop.
Save barneycarroll/8bdda320d17497ce8a4d7532376da46c to your computer and use it in GitHub Desktop.
Get user-focusable elements within a context in tab order
// Source
const tabitha = (context = document) =>
[
...context.querySelectorAll('*')
]
.filter(element => {
const style =
window.getComputedStyle(element)
return (
element.tabIndex != -1
&&
style.display != 'none'
&&
style.visibility != 'hidden'
)
})
.sort((a, b) =>
a.tabIndex > b.tabIndex ? 1 : 0
)
const elements = tabitha()
// Proof (?)
let lastIndex
document.addEventListener('focus', ({target}) => {
const index = elements.indexOf(target)
console.log(target, index)
if(index == -1)
debugger
if(lastIndex === undefined)
return
if(index < lastIndex && !(index === 0 && lastIndex +1 === elements.length))
debugger
}, {
capture: true,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment