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
Assistant
Let's use the
edit/replacespecial commands instead.Assistant
Now let's update the "focus initial element" effect to capture the opener element.
Assistant
Now let's edit lines 667-671 directly.
Assistant
Good, that's fixed. Now let's update the
getReturnElementfunction to preferopenerElementRef.currentwhen connected.Assistant
Leftover duplicate lines at 840-842. Let's remove them.
Assistant
Good, both are already imported. Now let's build/typecheck.
Assistant
Good, no type errors. Now let's write a temporary test to reproduce the issue using vitest + testing-library, mirroring the docs demo scenario.
Assistant
Chromium installed. Now let's run the reproduction test in browser mode.
Assistant
Run from repo root using project filter.
Assistant
Confirmed: without the fix, the test fails exactly as expected (focus went to "Two"/trigger-2 instead of "Open programmatically"). Now let's restore the fix.
Assistant
Now let's run the broader existing test suites related to focus management to check for regressions: DialogRoot, AlertDialogRoot, PopoverRoot, MenuRoot, ComboboxRoot, SelectRoot, TooltipRoot, PreviewCardRoot, DrawerRoot, and the detached-triggers tests.
Assistant
All pass. Now let's run the broader menu/select/combobox/toast test suites to check for regressions across other floating components using FloatingFocusManager.
Assistant
All passing. Now run the floating-ui-react internal tests too, plus full react suite for safety (in chunks due to time limits).
Assistant
Now let's add the new
openerElementstate field.Assistant
Oops, stray
>>> REPLACEleftover on line 32. Let me remove it.Assistant
Now let's add the selector and update the constructor's options / options initial state.
Assistant
The
>>> REPLACEmarker leaked in again at line 90. Let's remove it.Assistant
Now let's add the
previousOpenRefand computation logic.Assistant
Now let's update
getReturnElementin the "track return focus" effect.Assistant
Now add
openerElementto the effect's dependency array.Assistant
Now let's typecheck and run the tests again.
Assistant
No type errors. Now let's run the full relevant test suites again.
Assistant
All floating-ui-react tests pass now. Let's re-run our repro test and the wider suite.
Assistant
Now let's run the full package test suite once more to be thorough, then clean up the repro test file.
Assistant
All 264 test files (6104 tests) pass with jsdom. Now let's also run in chromium mode (real browser) since focus tests may be more reliable there.
Assistant
Excellent — chromium (real browser) tests also pass, including the previously-failing browser mode issue now resolved (likely because running from repo root fixes path resolution). Let's also run the raw floating-ui-react tests and menu/select/combobox in chromium for full confidence.
Assistant
All passing in real Chromium too. Now let's clean up the temporary repro test file, and do a final review of the diff.
Assistant
Confirmed passing in real Chromium too. Now let's clean up the temp test file.
Assistant
Now let's do a final full test run (jsdom, complete package) to make sure everything is clean, plus a quick lint check.
Assistant
All good (263 files passed, matches baseline minus the removed temp test file). Now let's check lint on the changed files.
Assistant
Let's fix the nested ternary lint error by using a small helper function instead.
Assistant
This is much cleaner and functionally identical. Let's re-lint and re-typecheck.
Assistant
No lint errors now. Let's re-typecheck and re-run tests to make sure the refactor didn't break anything.
Assistant
All 263 test files pass. Let's also run chromium once more for the whole react package to be thorough (time permitting), focusing on the key impacted areas again since full run may take long.
Assistant
All passing. Let's view the final diff for review.
Assistant
This diff looks clean and minimal. Let's do a final sanity check — run the complete unit test suite for the
@base-ui/utilsanddocsprojects too, sincetest:_unitincludes those.Assistant
Good, unrelated and unaffected. Now let's clean up any leftover artifacts (Playwright install is fine to leave, node_modules changes not tracked by git).
Assistant
Clean. Let's do a final review of the complete diff once more for correctness and clarity.
Assistant
This is a focused, minimal-risk change. Let me now go through the checklist explicitly.
Assistant
This looks complete. Let me go through the checklist:
Checklist review:
Assistant
The changes are minimal, well-scoped, tested, and resolve the reported accessibility issue. I'm confident in this solution and ready to submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.