Last active
October 31, 2019 19:20
-
-
Save chrisjpatty/c2512ae051268af6bdc7e93b5e960cb2 to your computer and use it in GitHub Desktop.
This is a handy utility function for getting the first focusable element within a certain scope.
This file contains hidden or 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
const getFirstFocusableElementByScope = (scope = '') => { | |
const focusableSelectors = [ | |
'button', | |
'[href]', | |
'input', | |
'select', | |
'textarea', | |
'[tabindex]:not([tabindex="-1"])' | |
] | |
.map(x => `${scope} ` + x) | |
.join(', ') | |
const focusables = document.querySelectorAll(focusableSelectors) | |
return focusables[0] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment