Repository navigation
[popups] Fix programmatic focus return - #4849
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,312.88 ms -29.13 ms(-2.2%) | Renders: 50 (+0) | Paint: 1,961.83 ms -76.45 ms(-3.8%)
10 tests within noise — details Check out the code infra dashboard for more information about this PR. |
3e76c18 to
47f079d
Compare
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
47f079d to
55a20ef
Compare
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
55a20ef to
9c7e953
Compare
9c7e953 to
f6a8e63
Compare
flaviendelangle
left a comment
There was a problem hiding this comment.
PR #4849 Review — [popups] Fix programmatic focus return
Verdict: Sound, well-targeted fix. No blocking issues. The PR diff is exactly 3 files (1 source, 2 tests). The core change correctly makes programmatically-opened controlled dialogs/popovers return focus to the originating control instead of the associated triggerId trigger, while preserving reference-first behavior for trigger-opened popups.
How the fix works (verified)
preferPreviousFocus = openInteractionTypeRef.current == null (FloatingFocusManager.tsx:734). Only Dialog, Popover, Combobox, and Drawer pass openInteractionType={openMethod} — which is null on a programmatic open and a non-null InteractionType on a trigger interaction. So trigger opens keep reference-first; programmatic opens prefer the previously-focused control. getNodeName is already imported and the disconnected/body staleness guard mirrors the existing addPreviouslyFocusedElement logic. Logic is correct.
Critical Issues
None. (The test-analyzer's reported Critical — Dialog test vacuous in jsdom — is incorrect: the test is Chromium-gated; jsdom skips it. Disregard.)
Important / Worth a decision
-
Menu and Select are silently excluded from the fix —
MenuPopup.tsx/SelectPopup.tsxuseFloatingFocusManagerbut don't passopenInteractionType, so it defaults to''(FloatingFocusManager.tsx:264) and'' == nullisfalse→preferPreviousFocusis alwaysfalsefor them. This is not a regression (they keep prior reference-first behavior), and the== nullcheck (rather than!value) is in fact what scopes the feature to opt-in components. But it's subtle. Recommendation: confirm excluding Menu/Select is intentional, and add a one-line comment at line 734 explaining that the== nullcheck (vs. falsy) deliberately limits the behavior to components that passopenMethod. Otherwise a future reader will "fix" it to!openInteractionTypeRef.currentand silently change Menu/Select behavior. -
No test for the preserved path — both new tests open via a separate programmatic control, exercising only the
preferPreviousFocus === truebranch. A regression that always preferred previous focus would pass every new assertion. The broader component suites likely cover trigger-open→return-to-trigger, but a dedicated assertion here (open via the trigger, close, assert focus returns to that trigger) would lock in the preserved behavior.
Suggestions
- Disconnected-reference fallback is untested — the new
referenceReturnElement = null(disconnected trigger) andgetPreviouslyFocusedElement()fallback (FloatingFocusManager.tsx:797-801) is exactly the kind of edge this code touches, but no test removes the originating control/trigger while open. openInteractionTypeRefdep-array entry is a no-op (:858) —useValueAsRefreturns a stable ref, so it never re-triggers the effect. Behavior is still correct (the trigger setsopenMethodsynchronously before mount). Harmless but slightly misleading; fine to leave.'html'/documentElementnot rejected by the staleness guard at:799(only'body'). This matches the existingaddPreviouslyFocusedElementconvention, so it's consistent — purely theoretical.- Dead "Close externally" button in
PopoverRoot.detached-triggers.test.tsx:216— the test now closes via the in-popupPopover.Close, so the renamed external button is never clicked. Either remove it or add an assertion exercising the externally-driven close.
Strengths
- Minimal, surgical change to a notoriously subtle area (focus restoration).
- Preserves all existing fallback semantics (disconnected reference, outside-press, explicit
returnFocus). - New tests are correctly Chromium-gated (focus assertions need real layout/focus) and capture the button reference before close rather than re-querying.
- Fully CLAUDE.md/AGENTS.md compliant (
useIsoLayoutEffect,activeElement,ownerDocument,contains).
Recommended actions
- Decide on Menu/Select scope and add the clarifying comment at line 734 (Important).
- Optionally add the preserved-path and disconnected-reference tests.
- Confirm CI Chromium is green (the two new tests are the actual regression coverage; jsdom skips them).
Reviewer notes
- jsdom run confirmed: 17 of 20 Dialog detached-trigger tests are skipped — the programmatic test at line 179 sits inside
describe.skipIf(isJSDOM)(line 15), so it is Chromium-only. - A couple of the sub-agents were misled by
git diff master...HEAD(localmasteris behind, pulling in unrelated NumberField/menu commits) — those are not part of this PR; the canonicalgh pr diffis clean at 3 files.
301e9a4 to
885c852
Compare
Fixes #4169
Controlled dialogs and popovers can be associated with a triggerId even when a separate control opened them programmatically. This returns focus to that originating control on close instead of always preferring the associated trigger.
Root cause
The default return-focus path preferred the associated reference trigger, even for programmatic opens where the focused opener was a different control.
Changes