Skip to content

Instantly share code, notes, and snippets.

@chrisjpatty
Last active October 31, 2019 19:20
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 chrisjpatty/c2512ae051268af6bdc7e93b5e960cb2 to your computer and use it in GitHub Desktop.
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.
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