Skip to content

[popups] Fix programmatic focus return - #4849

Merged
atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/fix-programmatic-focus-return
May 26, 2026
Merged

atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/fix-programmatic-focus-return

Conversation

@atomiks

@atomiks atomiks commented May 19, 2026

Copy link
Copy Markdown
Contributor

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

  • Prefer the previously focused opener for programmatic popup opens.
  • Keep reference-first focus restoration for trigger-opened popups and disconnected-reference fallback behavior.
  • Add coverage for a controlled Dialog triggerId opened from a separate control.

@atomiks atomiks added component: alert dialog Changes related to the alert dialog component. type: bug It doesn't behave as expected. component: dialog Changes related to the dialog component. accessibility a11y component: popover Changes related to the popover component. labels May 19, 2026
@pkg-pr-new

pkg-pr-new Bot commented May 19, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4849
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4849
    

commit: 885c852

@code-infra-dashboard

code-infra-dashboard Bot commented May 19, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+161B(+0.03%) 🔺+28B(+0.02%)

Details of bundle changes

Performance

Total duration: 1,312.88 ms -29.13 ms(-2.2%) | Renders: 50 (+0) | Paint: 1,961.83 ms -76.45 ms(-3.8%)

Test Duration Renders
Checkbox mount (500 instances) 116.90 ms 🔺+25.45 ms(+27.8%) 1 (+0)
Select open (500 options) 43.14 ms ▼-16.06 ms(-27.1%) 14 (+0)

10 tests within noise — details


Check out the code infra dashboard for more information about this PR.

@atomiks
atomiks force-pushed the codex/fix-programmatic-focus-return branch from 3e76c18 to 47f079d Compare May 19, 2026 05:50
@netlify

netlify Bot commented May 19, 2026

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 3e76c18
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a0bf967f9cef500082be585
😎 Deploy Preview https://deploy-preview-4849--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks force-pushed the codex/fix-programmatic-focus-return branch from 47f079d to 55a20ef Compare May 19, 2026 05:52
@netlify

netlify Bot commented May 19, 2026

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 47f079d
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a0bfa1d31110200087cc64d
😎 Deploy Preview https://deploy-preview-4849--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented May 19, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit fc996bf
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a15861e723b650008703c35
😎 Deploy Preview https://deploy-preview-4849--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks force-pushed the codex/fix-programmatic-focus-return branch from 55a20ef to 9c7e953 Compare May 21, 2026 08:58
@atomiks
atomiks force-pushed the codex/fix-programmatic-focus-return branch from 9c7e953 to f6a8e63 Compare May 21, 2026 09:01
@atomiks
atomiks marked this pull request as ready for review May 21, 2026 09:10

@flaviendelangle flaviendelangle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. Menu and Select are silently excluded from the fix — MenuPopup.tsx / SelectPopup.tsx use FloatingFocusManager but don't pass openInteractionType, so it defaults to '' (FloatingFocusManager.tsx:264) and '' == null is false → preferPreviousFocus is always false for them. This is not a regression (they keep prior reference-first behavior), and the == null check (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 == null check (vs. falsy) deliberately limits the behavior to components that pass openMethod. Otherwise a future reader will "fix" it to !openInteractionTypeRef.current and silently change Menu/Select behavior.

  2. No test for the preserved path — both new tests open via a separate programmatic control, exercising only the preferPreviousFocus === true branch. 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) and getPreviouslyFocusedElement() fallback (FloatingFocusManager.tsx:797-801) is exactly the kind of edge this code touches, but no test removes the originating control/trigger while open.
  • openInteractionTypeRef dep-array entry is a no-op (:858) — useValueAsRef returns a stable ref, so it never re-triggers the effect. Behavior is still correct (the trigger sets openMethod synchronously before mount). Harmless but slightly misleading; fine to leave.
  • 'html'/documentElement not rejected by the staleness guard at :799 (only 'body'). This matches the existing addPreviouslyFocusedElement convention, so it's consistent — purely theoretical.
  • Dead "Close externally" button in PopoverRoot.detached-triggers.test.tsx:216 — the test now closes via the in-popup Popover.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

  1. Decide on Menu/Select scope and add the clarifying comment at line 734 (Important).
  2. Optionally add the preserved-path and disconnected-reference tests.
  3. 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 (local master is behind, pulling in unrelated NumberField/menu commits) — those are not part of this PR; the canonical gh pr diff is clean at 3 files.

@atomiks
atomiks force-pushed the codex/fix-programmatic-focus-return branch from 301e9a4 to 885c852 Compare May 26, 2026 13:11
@atomiks
atomiks merged commit f70b316 into mui:master May 26, 2026
6 checks passed
@atomiks
atomiks deleted the codex/fix-programmatic-focus-return branch May 26, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accessibility a11y component: alert dialog Changes related to the alert dialog component. component: dialog Changes related to the dialog component. component: popover Changes related to the popover component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accessibility: Focus moves to the wrong trigger when using "Open programmatically" buttons

2 participants