Skip to content

[popups] Focus the trigger before a screen reader press opens it - #6016

Merged
atomiks merged 3 commits into
mui:masterfrom
henryqdineen:hqd-sr-press-focus-trigger
Oct 19, 2026
Merged

atomiks merged 3 commits into
mui:masterfrom
henryqdineen:hqd-sr-press-focus-trigger

Conversation

@henryqdineen

Copy link
Copy Markdown
Contributor

Follow-up to #5527. We ran into this while upgrading to 1.9.0, when a focus-return test for this pattern started failing. Our tests still use an older @testing-library/user-event (14.5.2), whose synthetic clicks look like screen reader presses to Base UI, so they ran this code path. With 14.6.7 the same test passes without this fix, so newer user-event versions won't surface it; the new test reproduces the failure with an explicit screen reader press sequence instead.

Problem

Since #5527, a screen reader press opens the popup synchronously inside the trigger's mousedown handler, before the reader moves focus to its cursor. When the popup opens, FloatingFocusManager records the element focused before the open as its focus-return fallback, so it records whatever had focus before the press instead of the trigger.

That breaks the documented "open a dialog from a menu" pattern for screen reader users: the dialog has no trigger of its own, so when it closes it falls back to the recorded element, and focus lands on whatever control had focus before the menu opened instead of the menu trigger.

<button>Before</button>
<Menu.Root>
  <Menu.Trigger>Playlist</Menu.Trigger>
  …<Menu.Item onClick={() => setDialogOpen(true)}>Delete</Menu.Item>…
</Menu.Root>
<AlertDialog.Root open={dialogOpen} onOpenChange={setDialogOpen}>…</AlertDialog.Root>

With focus on "Before", a screen reader press on "Playlist" → "Delete" → "Cancel" returns focus to "Before". Before #5527 the open waited a frame, by which time the reader had focused the trigger, so focus returned to "Playlist".

Fix

In useClick's virtual-press branch, focus the trigger (with preventScroll) before opening. This restores the order a screen reader press had before #5527: the trigger is focused, then the popup opens and moves focus inside synchronously, so the reader still doesn't see a stray move.

It stands in for the browser's own mousedown focus, so it follows the same rules. It is skipped when:

  • the popup is already open (pressing an inactive trigger, toggle: false, or a hover-opened popup);
  • the trigger is typeable, since the browser already focuses those;
  • an earlier mousedown handler prevented the default. Combobox.Trigger does this to keep focus on its input.

Tests

  • A Chromium test in MenuRoot.test.tsx for the dialog-from-menu pattern, driven entirely by screen reader presses. It fails without the fix (focus returns to "Before").
  • A Chromium test in ComboboxTrigger.test.tsx that a screen reader press on the trigger keeps focus on the input. Nothing covered this before; it fails if the trigger is focused regardless of a prevented default.
  • The existing screen reader press tests (Menu, submenu, FloatingFocusManager) still pass, as does the full Chromium suite.

Follow-ups

Not in this PR, but worth considering:

  • One screen reader press signal. "Was this a screen reader press?" is answered in two places, differently: useClick checks isVirtualPointerEvent on the pointerdown, while FloatingFocusManager checks isVirtualClick on the mousedown (detail === 0, no pointerType). They can disagree on the same press, for example when the press reports detail: 1, which MenuRoot.test.tsx notes screen readers can do.
  • Record the opener instead of inferring it. FloatingFocusManager infers the opener from activeElement at open time, and already special-cases body to record domReference. With a shared signal it could record domReference for a virtual press directly, and useClick wouldn't need to move focus at all.

🤖 Generated with Claude Code

Since mui#5527, a screen reader press opens the popup synchronously in the trigger's mousedown handler, before the reader moves focus to its cursor. The popup records whatever had focus before (not the trigger) as its focus-return fallback, so a dialog opened from a menu item returns focus to that earlier element instead of the menu trigger.

The focus stands in for the browser's own mousedown focus, so it is skipped when the popup is already open, for typeable triggers, and when an earlier handler prevented the default (Combobox.Trigger keeps focus on its input that way).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pkg-pr-new

pkg-pr-new Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

commit: bc73b58

@code-infra-dashboard

code-infra-dashboard Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+65B(+0.01%) 🔺+20B(+0.01%)

Details of bundle changes

Performance

Total duration: 406.15 ms -9.01 ms(-2.2%) | Renders: 75 (+0) | Paint: 733.38 ms -20.96 ms(-2.8%)

Test Duration Renders
Popover mount (300 instances) 12.42 ms ▼-4.24 ms(-25.4%) 1 (+0)

14 tests within noise — details


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

@netlify

netlify Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit bc73b58
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ad5d65543409751d69ef4f4
😎 Deploy Preview https://deploy-preview-6016--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 added type: bug It doesn't behave as expected. scope: all components Widespread work has an impact on almost all components. labels Oct 19, 2026

@atomiks atomiks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix!

@atomiks
atomiks merged commit d354c03 into mui:master Oct 19, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: all components Widespread work has an impact on almost all components. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants