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
I'll start by exploring the repository structure and understanding how focus is managed for dialogs and popovers when opened programmatically.
- Assistant
Let me explore the packages structure and find the dialog/popover related code for focus management.
- Assistant
Let me look at the focus management related code and how the triggers with controlled mode work.
- Assistant
I'll use grep instead since rg isn't available.
- Assistant
I'll inspect the detached-triggers tests and search for how focus is returned when dialogs close.
- Assistant
I'll inspect DialogPopup return focus handling and how the store tracks the active trigger.
- Assistant
I'll read FloatingFocusManager and DialogStore to see how the previous focus element is captured and restored.
- Assistant
Let me look at the return focus logic more carefully, around line 700-850.
- Assistant
Continue reading return focus logic.
- Assistant
I'll look at the docs example and DialogHandle to see how programmatic open sets the dreaded active trigger.
- Assistant
Let me look at the detached triggers controlled demo.
- Assistant
I'll check DialogPopup and how FloatingFocusManager receives the reference element and finalFocus.
- Assistant
Let me continue reading the FloatingFocusManager usage in DialogPopup.
- Assistant
I'll check PopoverPopup's returnFocus setup and look for existing tests covering this scenario.
- Assistant
There's
previousFocusableElementprop. Let me also look at how previously focused element is tracked. - Assistant
Need to specify recursive grep properly.
- Assistant
I need to check effect ordering and how enqueueFocus interacts with capturing the previous focus target.
- Assistant
I'll read FloatingFocusManager's returnFocus tests and look for recent related bug fixes.
- Assistant
I'll inspect the previous return-focus fix and any multi-trigger focus tests.
- Assistant
I'll examine focus-related detached-trigger tests and the Alert Dialog controlled demo.
- Assistant
I'll create a temporary test to reproduce the focus-return bug with the programmable open button.
- Assistant
I'll check how dialog tests close the dialog and assert focus, then draft a temporary repro.
- Assistant
I'll check FloatingFocusManager tests that assume focus returns to the reference, and inspect related edge cases.
- Assistant
I'll check tests for returning focus when the reference itself isn't focusable.
- Assistant
I'll inspect how opening sets the active trigger and whether focus is moved during that process.
- Assistant
I'll inspect how the floating reference is updated from the active trigger.
- Assistant
I'll apply the FloatingFocusManager fix to prefer the previously focused element, then verify with a temporary reproduction test.
- Assistant
I'll jump to the return-focus logic and apply the fix.
- Assistant
Now I can apply the replace.
- Assistant
I need to use the effect-local previously-focused element so nested dialogs don't all return focus to the same global stack entry.
- Assistant
I need to wire
originFocusElementfrom the locally captured previously-focused element on mount. - Assistant
I'll define
originFocusElementfrom the element that was focused when the popup opened. - Assistant
I'll add a temporary test to reproduce the issue and confirm the fix.
- Assistant
I'll check how browser tests are run in this repository.
- Assistant
I'll temporarily add a focus-return test for the programmatic-open path, then run it.
- Assistant
I'll insert a temporary repro test after the existing programmatic-open test.
- Assistant
I'll check which describe the test is under and run it.
- Assistant
I'll run the focused chromium test for the temporary programmatic focus-return case.
- Assistant
The chromium run timed out — I'll check the browser setup and try a more targeted approach.
- Assistant
Chromium is available now — rerunning the focused test.
- Assistant
The run is hanging with no output — I'll inspect the vitest setup and try a direct invocation.
- Assistant
I'll invoke vitest directly to avoid the pnpm bootstrap delay.
- Assistant
I'll set the environment variables directly and invoke vitest.
- Assistant
The repro passes. Next I'll run related FloatingFocusManager and dialog focus tests to check for regressions.
- Assistant
I'll also run alert-dialog and menu focus-related tests.
- Assistant
I'll temporarily revert the return-focus priority to confirm the temporary test fails on the old behavior.
- Assistant
I'll briefly restore the old preference order to confirm the temporary test fails without the fix.
- Assistant
Next I'll run the temporary test against the reverted logic.
- Assistant
Confirmed the old behavior—restoring the fix and removing the temporary test.
- Assistant
I'll remove the temporary test next.
- Assistant
I'll jump to the temporary test and remove it.
- Assistant
I'll restore the test file from git instead of editing it by hand.
- Assistant
I'll do a final verification of the focus-related tests, then submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.