Repository navigation
[popover] Keep focus that moved elsewhere before the deferred focus restore - #6015
Merged
Merged
Conversation
cristianchiriac
requested review from
atomiks,
colmtuite,
flaviendelangle,
jjenzz and
michaldudak
as code owners
October 16, 2026 15:53
commit: |
Bundle size
PerformanceTotal duration: 387.39 ms -57.69 ms(-13.0%) | Renders: 75 (+0) | Paint: 714.09 ms -84.55 ms(-10.6%)
…and 1 more (+9 within noise) — details Metric alarms
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
…estore With restoreFocus 'popup', the popup was re-focused on the next animation frame unconditionally, pulling back focus that the app or the user had moved in the meantime. Only re-focus while focus is still lost.
atomiks
force-pushed
the
fix-deferred-focus-restore
branch
from
October 20, 2026 04:55
5ee616e to
dc4e9c8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5980
FloatingFocusManager'shandleFocusOutsidewithrestoreFocus: 'popup'(whichDialogPopupandPopoverPopuppass) focuses the popup when the focused element is removed and focus falls to<body>. It then focuses it again on the next animation frame, to win the race where the browser moves focus back to the removed element. That deferred focus was unconditional. If the app or the user moved focus somewhere else before the frame ran, it was pulled back to the popup. For example, app code that focuses another control right after a removal lost that focus, and a nested non-modal Popover opened in that window closed with reasonfocus-out.The frame callback now re-focuses the popup only while focus is still lost (
activeElementis<body>ornull, read with the shadow-DOM-safeactiveElementutility). The race it exists for is unchanged: in that case focus is lost when the frame runs, so the popup is still re-focused.Tests (
FloatingFocusManager.test.tsx,prop: restoreFocus, Chromium only like the other removal tests there):popup: keeps focus that moved elsewhere before the deferred re-focus: removes the focused button and focuses another one before the frame runs. Frames are held with arequestAnimationFramespy so the order is deterministic. Fails before this change (the popup steals focus) and passes after.popup: re-focuses the popup when focus is still lost on the next frame: the sibling case with focus lost again before the frame. The popup is re-focused, and the test fails if the deferred focus is removed.Validation:
pnpm test:chromium FloatingFocusManager Dialog Popover --no-watch: 1001 passed.pnpm test:jsdom FloatingFocusManager Dialog Popover --no-watch: 832 passed.eslintandprettier --checkon both files, andtsc --noEmitfor@base-ui/react: clean.I have followed (at least) the PR section of the contributing guide.