Skip to content

Commit

Permalink
Improve detection of focusable elements
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoprietog committed Nov 5, 2023
1 parent dc05473 commit 12138f7
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ export class FocusTrap {
}

get tabbableElements() {
return Array.from(this.element.querySelectorAll("*")).filter((element) => {
return element.tabIndex >= 0 && !element.inert && !element.disabled && visible(element)
})
return Array.from(this.element.querySelectorAll("*")).filter(elementIsFocusable)
}

get firstTabbableElement() {
Expand All @@ -92,10 +90,7 @@ export class FocusTrap {
}
}

function visible(element) {
return (
!(element.hidden || element.closest("[hidden]")) &&
(!element.type || element.type !== "hidden") &&
(element.offsetWidth > 0 || element.offsetHeight > 0)
)
function elementIsFocusable(element) {
const inertDisabledOrHidden = "[inert], :disabled, [hidden], details:not([open]), dialog:not([open])"
return !!element && element.closest(inertDisabledOrHidden) == null && element.tabIndex >= 0 && typeof element.focus == "function"
}

0 comments on commit 12138f7

Please sign in to comment.