ISSUE DESCRIPTION Accessibility: Focus moves to the wrong trigger when using "Open…
- User
ISSUE DESCRIPTION
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
I'll start by exploring the repository structure and understanding how focus management works for Dialog, Alert Dialog, and Popover components.
- Assistant
Let me examine the dialog and focus-related source code.
- Assistant
Let me search for focus return related code more specifically.
- Assistant
rg is not available, let me use grep.
- Assistant
Let me look at the dialog root and how open is controlled with triggers and programmatic opening.
- Assistant
Let me read DialogHandle, DialogStore, and the FloatingFocusManager focus-return logic.
- Assistant
Let me check the docs demo and how the "Open Programmatically" button works.
- Assistant
Let me read the controlled mode demo.
- Assistant
Now I'll inspect FloatingFocusManager's return-focus logic and how dialog sets the reference.
- Assistant
Let me look at the return focus part more specifically.
- Assistant
Let me look at the lifecycle for return focus when closing.
- Assistant
I see - when the FloatingFocusManager mounts, it captures
previouslyFocusedElement = activeElement(doc)which should be the "Open programmatically" button. Then on unmount it should restore focus to... - Assistant
The return-focus logic prefers
domReference(the associated trigger) over the previously focused element — that’s the bug. Let me check existing tests and related history. - Assistant
Let me look at the FloatingFocusManager tests related to returnFocus, and also dig into addPreviouslyFocusedElement. Also check dialog tests for focus return with detached triggers.
- Assistant
Let me also check git log and if there's any related issue or PR. Also let me look at return focus tests for Dialog more carefully -maybe they expect focus to go t...
- Assistant
There's a very relevant PR and a test about returning focus when a detached dialog trigger unmounts. Let me inspect those.
- Assistant
Let me look at the FloatingFocusManager.test for return focus to understand intended behavior better.
- Assistant
Let me check the docs and finalFocus interface to clarify intended return-focus behavior.
- Assistant
Let me look for any tests expecting focus return to the controlled trigger specifically, and check how openMethod / imperative open sets fire.
- Assistant
Let me check graceful return when an origin control is removed from the DOM.
- Assistant
Checking how FloatingFocusManager is wired and when disable happens on close.
- Assistant
I'll update
getReturnElementto prefer the previously focused (originating) element over the associated trigger. - Assistant
The edit went to the wrong file — reverting DialogPopup and applying the change to FloatingFocusManager instead.
- Assistant
Good, now I can edit the correct file.
- Assistant
I need to verify effect order and whether
enqueueFocusruns synchronously. - Assistant
I'll update the docs comments to match the new preference order.
- Assistant
I'll update the documentation strings to reflect the new focus-return preference order.
- Assistant
Next I'll run the related focus and dialog tests to validate the fix.
- Assistant
I'll write a temporary test reproducing the issue, then run the dialog and focus-manager tests.
- Assistant
The full chromosome suite timed out — trying a smaller, focused set of tests.
- Assistant
Something is hanging. Let me try differently - maybe check if vitest is available and what the browser setup needs.
- Assistant
Piping may be the problem — I'll run the tests without
tail. - Assistant
Corepack is hanging while fetching pnpm — I'll invoke vitest from node_modules instead.
- Assistant
I can use node_modules/.bin/vitest directly.
- Assistant
Need to use the path to cross-env.
- Assistant
JSDOM passed; focus tests need Chromium. Running those next.
- Assistant
Need to install playwright browsers.
- Assistant
Chromium is ready — rerunning the focus-related tests.
- Assistant
Dialog tests passed. Next I'll add a temporary reproduction for the bug and run popover tests.
- Assistant
Creating a temporary reproduction test and running detached-triggers tests.
- Assistant
I'll fix the reproduction test so detached triggers use a handle, matching the docs demo.
- Assistant
Confirming the bug existed before the change by temporarily reverting and re-running the reproduction.
- Assistant
Confirmed: without the fix focus went to Delete; restoring the fix and removing the temp test.
- Assistant
All related package tests passed. Running the JSDOM focus-manager and select/combobox suites next.
- Assistant
There's a failing keepMounted test — inspecting it next.
- Assistant
The keepMounted case fails because previously focused can be inside the popup. Preferring only an outside originating control.
- Assistant
Re-running the failing test and the reportons Next.
- Assistant
All tests passed. Cleaning up the temp test and running a final broad verification.
- Assistant
Verifying the final form of the core change.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.