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
Now let's write a reproduction test file.
Assistant
Reproduced the bug exactly as described. Now let's implement the fix in FloatingFocusManager.tsx.
Assistant
Now let's rerun the reproduction test.
Assistant
Fix works. Now let's remove the repro test file and run the full existing test suites (FloatingFocusManager, Dialog, Popover, AlertDialog, Menu, Tooltip, PreviewCard, Combobox, Select, NavigationMenu) in chromium browser mode to check for regressions.
Assistant
Found a regression. Let's inspect this test.
Assistant
Also add a log at the returnElement computation:
Assistant
Now run test with logs.
Assistant
Good, restored correctly (console.logs removed, our fix intact). Now let's refine the condition to exclude elements contained within the floating tree.
Assistant
containsis already used elsewhere in the file (e.g.contains(floating, activeEl)). Let's confirm import and checkgetResolvedInsideElementsusage too, to decide which check is most robust.Assistant
Good,
containsalready imported. Now update the condition.Assistant
All pass now. Let's re-run chromium browser mode too, and re-verify our repro scenario.
Assistant
Now let's recreate the repro test and confirm it still passes with the refined fix.
Assistant
Both scenarios pass. Now let's run broader test suites for dialog, popover, alert-dialog, menu, tooltip, preview-card, combobox, select, navigation-menu in both jsdom and chromium modes to check for regressions.
Assistant
All jsdom tests pass. Now let's run the same in chromium mode to cover the skipIf(isJSDOM) tests (detached-triggers, multiple-triggers, etc.)
Assistant
All tests pass in both jsdom and chromium modes. Now let's also run the full floating-ui-react test suite (in case there are more tests referencing focus behavior in other files) and the full repo test suite briefly to be safe.
Assistant
All good. Let's view the final diff.
Assistant
No lint errors. Let's also run typecheck for the package to be safe.
Assistant
Typecheck passes cleanly. Let's clean up the temporary chromium binary install artifacts aren't part of repo changes (they're outside the repo in cache dir, fine). Let's verify final git diff once more and ensure no stray files.
Assistant
Clean. Now let's go through the checklist.
Assistant
Now let's go through the checklist explicitly:
Checklist review:
-
If an error was reported in the issue description, I have successfully reproduced it.
- Yes. I wrote a reproduction test (
repro-focus.test.tsx) simulating the exact scenario from the issue: aDialog.Rootwith multiple triggers and a controlled "Open programmatically" button that setstriggerIdto a different trigger's id before opening. Running it against the original code confirmed focus moved to "Two" (thetriggerId's associated trigger) instead of the actual button that was clicked ("Open programmatically"), matching the reported bug exactly.
- Yes. I wrote a reproduction test (
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- Yes. After patching
FloatingFocusManager.tsxto prefer the element that was actually focused immediately before the floating element opened (when it's a valid, connected element outside the floating tree) over the "logical"domReference(activeTriggerElement), the same reproduction test passed — focus correctly returns to the "Open programmatically" button after closing the dialog.
- Yes. After patching
-
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.