Skip to content

Instantly share code, notes, and snippets.

@tsmd
Created February 1, 2020 11:14
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 tsmd/310c37d4c1118a5c21733f9ff4b3646b to your computer and use it in GitHub Desktop.
Save tsmd/310c37d4c1118a5c21733f9ff4b3646b to your computer and use it in GitHub Desktop.
function rollFocus(backFlag) {
let focusIndex
const currentIndex = this.tabbableElements.indexOf(document.activeElement)
if (currentIndex < 0) {
focusIndex = 0
} else {
const tabbableLength = this.tabbableElements.length
const direction = backFlag ? -1 : 1
focusIndex = (currentIndex + direction + tabbableLength) % tabbableLength
}
this.focus(this.tabbableElements[focusIndex])
}
function focus(element) {
if (!element) {
return
}
element.focus()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment