Accessibility: Focus moves to the wrong trigger when using "Open programmatically"…
- User
Accessibility: Focus moves to the wrong trigger when using "Open programmatically" buttons
Current behavior
In the Alert Dialog, Dialog and Popover components, there is a variant that includes the option to use multiple triggers to open the dialogs. When using the "Open Programmatically" button to open the dialog/popup, then closing it, focus moves to a different control than the one that triggered the dialog/popup.
See:
- [Alert Dialog: Controlled mode with multiple triggers](https://base-ui.com/react/components/alert-dialog#controlled-mode-with-multiple-triggers
- Dialog: Controlled mode with multiple triggers
- Popover: Controlled mode with multiple triggers
This could cause failures against WCAG 2.2 Success Criterion 2.4.3 Focus Order when implemented.
Expected behavior
Keyboard focus should move back to the originating control (if available) when closing a dialog as long as a page refresh or redirection is not triggered.
If the control is no longer available, focus should move next to that originating control's position in the DOM.
Reproducible example
- Go to [Alert Dialog: Controlled mode with multiple triggers](https://base-ui.com/react/components/alert-dialog#controlled-mode-with-multiple-triggers
- Using keyboard, move focus to the "open programmatically" button
- Press Enter to activate the control and open the dialog
- Press Esc or activate the close button to close the dialog
- Note focus has moved back to the "Delete" button and not the "open programmatically" button as expected.
Base UI version
v1.2.0
Which browser are you using?
Chrome 145.0.7632.76
Which OS are you using?
Mac OS Tahoe 26.2
Which assistive tech are you using (if applicable)?
Keyboard
Additional context
Recommend keeping track of focus and returning it to the originating control.
- Assistant
Investigating focus restoration for programmatic opens in Alert Dialog, Dialog, and Popover — tracing how the demos and focus logic handle multiple triggers.
- Assistant
Implementing the fix in
FloatingFocusManager— it currently prefersdomReference(active trigger) over the element that had focus when the popup opened. - Assistant
Using the per-instance opening focus target from the effect closure instead of the global stack — implementing the fix and adding tests.
- Assistant
Adding tests for programmatic open focus restoration:
- Assistant
The broad fix breaks nested dialogs. Checking how to detect registered triggers in FloatingFocusManager:
- Assistant
Refining the fix: only prefer the opening focus target when it's not a registered trigger, and keep the global stack fallback for nested dialogs.
- Assistant
The keepMounted failure happens when focus was inside the popup at open time. Excluding in-popup elements from the opening-focus preference.
- Assistant
The controlled dialog test needs an
onOpenChangehandler to close on Escape. - Assistant
Fixed focus restoration when dialogs/popovers are opened programmatically with a controlled
triggerId.Root cause
When opening via an external button (e.g. "Open programmatically"), the popup still associates with the trigger from
triggerId(e.g. "Delete"). On close,FloatingFocusManageralways returned focus to that associated trigger (domReference) instead of the control that actually opened the popup.Fix
Updated
getDefaultReturnFocusElementinFloatingFocusManager.tsxto prefer the element that had focus when the popup opened when all of these are true:- It differs from the active trigger (
domReference) - It is not a registered trigger element
- It is not inside the popup content
- It is still connected to the DOM
This covers Alert Dialog, Dialog, and Popover without new public APIs, since they all use
FloatingFocusManagerfor focus management.Tests added
FloatingFocusManager.test.tsx— returns focus to the opener when it differs from the reference- — keyboard open via "Open programmatically", close via Escape, focus returns to that button (not the associated trigger)
- It differs from the active trigger (
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.