Created
October 10, 2018 10:52
-
-
Save barneycarroll/8bdda320d17497ce8a4d7532376da46c to your computer and use it in GitHub Desktop.
Get user-focusable elements within a context in tab order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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