Skip to content

Commit

Permalink
improve performance of checks
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Sep 9, 2022
1 parent 8aee053 commit 45082b5
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/@headlessui-react/src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,16 @@ let PopoverRoot = forwardRefWithAs(function Popover<
// doesn't really matter if they are portalled or not because we can follow the default tab
// order. But if they are not, then we can consider it being portalled so that we can ensure
// that tab and shift+tab (hopefully) go to the correct spot.
let allFocusableElements = getFocusableElements()
let focusableElementIndexesInPanel = getFocusableElements(panel).map((panel) =>
allFocusableElements.indexOf(panel)
)
let buttonIdx = allFocusableElements.indexOf(button)
let elements = getFocusableElements()
let buttonIdx = elements.indexOf(button)

let beforeIdx = (buttonIdx + elements.length - 1) % elements.length
let afterIdx = (buttonIdx + 1) % elements.length

let beforeElement = elements[beforeIdx]
let afterElement = elements[afterIdx]

if (!focusableElementIndexesInPanel.some((otherIdx) => otherIdx - buttonIdx === 1)) {
if (!panel.contains(beforeElement) && !panel.contains(afterElement)) {
return true
}

Expand Down

0 comments on commit 45082b5

Please sign in to comment.