Repository navigation
[popups] Focus the trigger before a screen reader press opens it - #6016
Merged
Merged
Conversation
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>
henryqdineen
requested review from
atomiks,
colmtuite,
flaviendelangle,
jjenzz and
michaldudak
as code owners
October 16, 2026 17:43
commit: |
Bundle size
PerformanceTotal duration: 406.15 ms -9.01 ms(-2.2%) | Renders: 75 (+0) | Paint: 733.38 ms -20.96 ms(-2.8%)
14 tests within noise — details 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. |
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.
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
mousedownhandler, before the reader moves focus to its cursor. When the popup opens,FloatingFocusManagerrecords 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.
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 (withpreventScroll) 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
mousedownfocus, so it follows the same rules. It is skipped when:toggle: false, or a hover-opened popup);mousedownhandler prevented the default.Combobox.Triggerdoes this to keep focus on its input.Tests
MenuRoot.test.tsxfor the dialog-from-menu pattern, driven entirely by screen reader presses. It fails without the fix (focus returns to "Before").ComboboxTrigger.test.tsxthat 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.FloatingFocusManager) still pass, as does the full Chromium suite.Follow-ups
Not in this PR, but worth considering:
useClickchecksisVirtualPointerEventon thepointerdown, whileFloatingFocusManagerchecksisVirtualClickon themousedown(detail === 0, nopointerType). They can disagree on the same press, for example when the press reportsdetail: 1, whichMenuRoot.test.tsxnotes screen readers can do.FloatingFocusManagerinfers the opener fromactiveElementat open time, and already special-casesbodyto recorddomReference. With a shared signal it could recorddomReferencefor a virtual press directly, anduseClickwouldn't need to move focus at all.🤖 Generated with Claude Code