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.
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 first inspect the repository structure and locate the shared focus-restoration logic and relevant tests for Alert Dialog, Dialog, and Popover. Then I’ll reproduce the behavior in the closest existing test harness, implement a general fix, and run targeted tests.
- Assistant
rgis unavailable, so I’ll usegrepandfindto identify the implementations and examples. - Assistant
The repository already has detached-trigger browser tests, including a Dialog “Open programmatically” case. I’ll inspect those tests and the root/trigger state management to determine why programmatic opens retain a stale trigger reference.
- Assistant
The existing test only verifies payload selection, not focus restoration. The likely distinction is between the “associated trigger” (needed for payload/ARIA/positioning) and the element that actually held focus when a controlled open occurred. I’ll inspect the store and FloatingFocusManager’s capture/return behavior before choosing the narrowest shared fix.
- Assistant
FloatingFocusManagerdefaults to the current floating DOM reference, which is deliberately set to the selected trigger. It does have a “previously focused element” fallback, but only uses it when the reference is disconnected. I’ll inspect how that previous element is recorded and how Popup computesfinalFocus; a component-level explicit return target may already be the intended extension point. - Assistant
Popover already passes the associated trigger as a focus-guard neighbor, but not as an explicit return target. The manager captures the active element at mount in a global weak-ref stack. I’ll inspect the mount timing and Dialog equivalent, then determine whether changing default return order to “previously focused element first” is safe or whether we need a per-open origin to avoid nested/stale global state.
- Assistant
A robust shared rule is available inside
FloatingFocusManager: when opening without a trigger interaction (openInteractionTypeis empty), return to the element focused at that specific manager mount rather than the associated positioning/ARIA trigger. For genuine trigger keyboard/pointer opens, preserve existing return-to-trigger behavior. I’ll inspect focus traversal utilities so removal of the origin can fall back to the next focusable element near its former DOM position, as requested, rather than silently returning to the unrelated associated trigger. - Assistant
The manager can capture both the focused origin and its next tabbable neighbor before focus enters the popup. I’ll implement this only for default (
boolean) return-focus behavior and only for programmatic opens. ExplicitfinalFocusremains authoritative, trigger-originated opens still return to their trigger, and initial opens from<body>retain the trigger fallback. If the captured origin is removed, the captured next tabbable element is used if still connected. - Assistant
Python is unavailable, so I’ll apply the same targeted edit with a Node script and inspect the diff.
- Assistant
Before creating any temporary reproduction, I’ll run the existing focused manager and component suites. These include programmatic open/close cases and should reveal compatibility issues in default focus semantics.
- Assistant
The test command was blocked by Corepack’s download confirmation rather than code. I’ll disable the prompt and run a smaller first batch with a longer allowance unavailable tool timeout fixed likely 10 sec. Tool timeout schema no timeout. commands max 10 sec. Could run background poll.
- Assistant
The targeted browser run is now executing non-interactively in the background. I’ll poll its status and inspect failures when complete.
- Assistant
Some detached-trigger tests were skipped because the command did not set
VITEST_ENV=chromium; the manager suite is still running. I’ll let it complete, then rerun true Chromium suites with the repository’s environment variable. - Assistant
The broad inference from an empty interaction type breaks an existing keep-mounted manager case, confirming it should be opt-in at the popup component layer. I’ll add an internal manager flag and enable it in Dialog and Popover only when their store reports no trigger open method. Alert Dialog shares DialogPopup, so all three affected components are covered while generic focus-manager semantics remain unchanged.
- Assistant
The automated dependency insertion matched an earlier effect instead of the return-focus effect. I’ll correct that dependency placement before testing; otherwise the earlier effect reruns unnecessarily and the intended effect risks stale configuration.
- Assistant
The dependency is now correct. I’ll first confirm the previously failing manager suite passes, then execute the detached-trigger suites under actual Chromium (where those tests are enabled).
- Assistant
I’ll poll the focused regression suite.
- Assistant
The manager suite now passes all 49 enabled tests. I’ll run Dialog, Popover, and Alert Dialog detached/multiple-trigger suites in Chromium.
- Assistant
I’ll poll the Chromium component suites.
- Assistant
Chromium tests could not launch because the Playwright browser binary is missing. Per the environment guidance, I’ll install the required Chromium binary and rerun; this changes only the external cache, not repository files.
- Assistant
I’ll poll the browser installation.
- Assistant
The download is progressing normally; I’ll wait for completion.
- Assistant
The headless shell is the final required artifact; I’ll poll once more.
- Assistant
Chromium installed successfully. I’ll rerun the three affected component suites.
- Assistant
I’ll poll the actual Chromium run.
- Assistant
All 81 enabled affected-component Chromium tests pass. I’ll inspect the documented DOM ordering to validate the removed-origin fallback against the actual example, then run lint/type checks.
- Assistant
The documented programmatic button follows the three triggers, so capturing its next tabbable neighbor at open provides the requested DOM-position fallback if it is removed (wrapping according to the library’s existing tabbable traversal). The root lint script expanded to all workspaces and exceeded the command window, so I’ll run direct lint on changed files and a background TypeScript build.
- Assistant
Direct lint passed with no findings. I’ll poll the package TypeScript check.
- Assistant
The TypeScript build is still running; I’ll wait for completion.
- Assistant
No diagnostics have appeared, but compilation has not exited yet. I’ll poll once more and then inspect the final diff/status.
- Assistant
One edge-case review found an important detail: because portaled popup controls are mounted before the focus-manager layout effect, the generic “next tabbable” may be a close button that disappears with the popup. I’ll refine the captured fallback to select the next tabbable element outside the popup subtree, preserving the originating control’s page context when the origin is removed.
- Assistant
The fallback now excludes popup descendants and safely handles empty/no-origin lists. I’ll rerun the manager regression suite and all affected Chromium suites after this edge-case refinement.
- Assistant
I’ll poll the combined final test run.
- Assistant
Final tests pass: 49 manager tests and 81 affected-component Chromium tests. I’ll run final lint/type checking and inspect repository status to ensure only intended source files changed and no build artifacts remain.
- Assistant
The final checks pass and only the three intended source files are modified. I’ll review the complete diff once for API scope, comments, and focus fallback behavior before submitting.
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
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-lunaCompare side by side →