Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function scopeTab (element, event, onLeavingFinalTabbable) {
const node = findDOMNode(element)
const tabbable = findTabbable(node)
if (!tabbable.length) {
event.preventDefault()
return
}
// Account for a changing tabindex of the active element
// (a case that happens with Menu for KO a11y)
if (containsActiveElement(element)) {
const activeElement = getActiveElement()
if (tabbable.indexOf(activeElement) === -1) {
tabbable.push(activeElement)
}
}
const finalTabbable = tabbable[event.shiftKey ? 0 : tabbable.length - 1]
const leavingFinalTabbable = (
isActiveElement(finalTabbable) ||
// handle immediate shift+tab after opening with mouse
isActiveElement(node) ||
// already left final tabbable
!containsActiveElement(element)
)
if (!leavingFinalTabbable) return
get activeElement () {
return getActiveElement(this.doc)
}