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.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /base-ui, current file: none) bash-$
- Assistant
I'll start by exploring the repository structure and understanding how focus is managed in Dialog, Alert Dialog, and Popover components.
- Assistant
Let me explore the packages and find focus-related code for these components.
- Assistant
Let me look at how focus is managed when dialogs close, and how triggers work with controlled mode and multiple triggers.
- Assistant
rg is not available, let me use grep.
- Assistant
There's a detached triggers test and focus-related popup utilities. Let me investigate those.
- Assistant
Let me look more carefully at how dialogs handle multiple triggers and focus return.
- Assistant
Let me inspect DialogHandle open method and how FloatingFocusManager tracks the previously focused element.
- Assistant
Let me check the docs example for controlled mode with multiple triggers.
- Assistant
Let me look at the demo to understand how it works.
- Assistant
The demo associates a trigger programmatically while the focused element is a plain button. I'll inspect how FloatingFocusManager chooses the return-focus target.
- Assistant
Let me look at the return focus logic and how previously focused element is stored.
- Assistant
The manager prefers
domReferencefor return focus. Next I'll check how Dialog setsfinalFocusand ties that to the active trigger. - Assistant
Let me see more of the file around returnFocus.
- Assistant
So finalFocus defaults to undefined, which means returnFocus is undefined in FloatingFocusManager... wait, returnFocus = true is the default when undefined is passed... Actually `returnFocus={finalFoc...
- Assistant
Let me also check the alert-dialog demo and any existing tests about this.
- Assistant
Let me dig into tests and recent focus-return handling more carefully.
- Assistant
Let me inspect how previously-focused elements are tracked and the full return-focus path.
- Assistant
Let me look at more of the return focus cleanup and related tests. Also check floating-ui upstream or change logs for any discussion.
- Assistant
Let me check how the floating root context binds
domReferenceand reopen/close flow tests. - Assistant
Let me look at the FloatingFocusManager tests for returnFocus behavior to understand expected preferences.
- Assistant
There's a test that nested elements always return to the reference. Let me examine that.
- Assistant
Let me read the unmount and detach-trigger tests related to focus return.
- Assistant
Let me check how controlled open updates the store and whether an event trigger is available.
- Assistant
There's a related comment about controlled focus. Let me keep reading.
- Assistant
Let me check payload and trigger association when controlled open is set.
- Assistant
I'll try preferring the previously focused element and run related tests to see what breaks.
- Assistant
There's a key case about preserving tabbable context next to a removed reference. Let me read that carefully.
- Assistant
Let me look for related issues, PRs, and how the docs e recommend handling this.
- Assistant
There's a related PR about return focus. Let me inspect that change.
- Assistant
Let me look at that PR's diff.
- Assistant
I'll prefer the previously focused (originating) element for return focus, falling back to the reference. That’s the WCAG-aligned behavior.
- Assistant
Need to navigate to the right line first.
- Assistant
Now I can apply the edit.
- Assistant
Preferring previously focused blindly would break nested dialogs. I'll only use it when it's outside the floating tree.
- Assistant
I need to add the getDefaultReturnElement helper next to getReturnElement.
- Assistant
I need to confirm the layout-effect order so previously focused isn't captured after initial focus moves inward.
- Assistant
Looking at the initial focus effect - does it move focus synchronously or in a microtask/raf?
- Assistant
Initial focus is deferred via microtask, so the originating control is still captured correctly. Next I'll create a temporary reproduction and run the focus-related tests.
- Assistant
I'll add a temporary test for the programmatic-open focus-return case, then run tests.
- Assistant
I'll temporarily add a focus-return test in the detached-triggers file to verify the fix, then remove it before submitting.
- Assistant
Let me see how skipIf isJSDOM is used for focus tests and add my tests after.line 226.
- Assistant
I accidentally added a weird Dialog.Trigger with createDialogHandle - that might break things. Let me fix the test to match the demo more closely.
- Assistant
The parent describe is already skipIf(isJSDOM), so my it.skipIf is redundant. Let me fix the broken test App.
- Assistant
There's an issue - when renders Dialog.Root with open={false}, the triggers are inside the children render prop which onlyr runs when open? LookingLOOKUP at_original \payload t...
- Assistant
Children alwaysrender, good. There's a potential issue: keys to work properly, when pressing Enter on open button which is a plain buttonリラ (not form), it might not fire click. User keyboard En...
- Assistant
The test timed out, likely due to browser startup. I'll retry with a longer timeout.
- Assistant
Piping to tail may be blocking. I'll run the tests without it.
- Assistant
Corepack is prompting interactively. I'll enable pnpm non-interactively and rerun.
- Assistant
Good. Now I'll run the tests.
- Assistant
There was a failure — I'll inspect the full test output.
- Assistant
Playwright browsers are missing — I'll install them next.
- Assistant
Playwright is ready. Running the tests again.
- Assistant
All 21 detached-trigger tests passed. Next I'll run FloatingFocusManager, Dialog, and Popover focus tests.
- Assistant
All passed. FloatingFocusManager skipped many in Chromium — I'll run those under jsdom as well.
- Assistant
Walking only the last stack entry can miss the real originating control (e.g. after a nested tooltip). I'll walk the stack for the last element outside the floating tree.
- Assistant
Updating getDefaultReturnElement to walk the stack for an outside candidate.
- Assistant
Updating getDefaultReturnElement to use the stack walk.
- Assistant
Re-running focus-related tests with the stack-walk change.
- Assistant
Removing the temporary test I added, since new tests aren't allowed in the submission.
- Assistant
Need to open the file first.
- Assistant
I'll restore the test file with git checkout.
- Assistant
Running nested menu/dialog tests once more to confirm no regressions.
- Assistant
Re-verifying the issue scenario quickly, then cleaning up.
- Assistant
Let me add the temp test one more briefly and run.
- Assistant
Run the TEMP test only.
- Assistant
The fix works. Removing the temporary test and submitting.
- Assistant
Reviewing the final change one more time before submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →