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
I’ll first inspect the repository structure and locate the shared focus-restoration logic and relevant component tests. Then I’ll reproduce the multiple-trigger behavior in an existing or temporary test, implement the smallest shared fix, and run focused test suites plus type/lint checks as appropriate.
- Assistant
ripgrepis unavailable, so I’ll usegrep/find. I’ll inspect the dialog and popover roots, triggers, popup focus handlers, and their tests to determine whether the bug lies in trigger bookkeeping or the shared floating focus manager. - Assistant
The three components all route through
FloatingFocusManager, so a shared correction is likely. I’ll read its mount/unmount focus capture and the popup store’s active-trigger updates, then inspect detached/multiple-trigger tests for intended semantics. - Assistant
The store intentionally leaves no active trigger when a controlled popup with multiple triggers is opened without a
triggerId. In that case the focus manager appears to fall back to a registered reference, explaining the wrong “Delete” button. I’ll inspect the exact return-target algorithm and the programmatic test fixture to identify where to preserve the actual active element and how disconnected-origin fallback is currently handled. - Assistant
getReturnElement()currently always prefersdomReferenceover the element focused immediately before the popup mounted. That is exactly backwards for a programmatic opener. The effect already captures the correct localpreviouslyFocusedElement; using the global history only also risks nested overlays affecting the result. I’ll inspect tabbable helper signatures and existing focus-manager return-focus tests before changing the priority and adding a robust fallback for a removed origin. - Assistant
There are already tests for preserving tab order when the reference is removed, so I’ll read those fully. They may show an existing placeholder/marker mechanism that should be applied to the actual originating control rather than inventing new fallback logic.
- Assistant
The removed-reference tests only verify that subsequent Tab continues in document order; they do not directly focus the next control. To satisfy the stated expectation, the focus manager can snapshot the next tabbable control while the origin still exists.
Before editing, I’ll verify how
domReferenceis assigned in controlled multiple-trigger mode. This determines whether the fix belongs in generic focus restoration (preferred) or in popup trigger state. - Assistant
The popup passes default
finalFocusas boolean semantics (“trigger or previously focused element”), and the root can leave the floating reference pointing at a registered trigger even though an unrelated button initiated the controlled opening. Therefore the generic default should choose the locally captured origin first, while explicitfinalFocusmust retain its current behavior. I’ll implement that distinction and snapshot the next tabbable sibling for the disconnected-origin case. Then I’ll run the focus-manager and component popup tests in jsdom and Chromium if available. - Assistant
Python is not installed, so no source changes were made. I’ll apply the same targeted edit using the repository’s non-interactive editor commands.
- Assistant
Only the import applied; the editor’s exact replacement missed the later blocks after line-number shifts. I’ll use line-range edits after inspecting the current numbered text.
- Assistant
I’ll now replace the exact line ranges, preserving explicit
finalFocusfallback behavior and changing only the default boolean branch. - Assistant
A review reveals one important edge case: when nothing meaningful was focused,
activeElementis<body>. Treating body as the origin would causegetFirstTabbableElement(body)to focus the page’s first control rather than the popup reference. I’ll exclude body from the origin branch, matching the existing focus-history behavior, then run tests. - Assistant
The source replacement likely completed, but Corepack blocked before tests with an interactive package-manager download prompt (which this environment cannot answer). Since dependencies and binaries are already installed, I’ll verify the edit and invoke local binaries directly, avoiding Corepack.
- Assistant
The focused suite reproduced a regression in keep-mounted mode: when focus management becomes enabled after the popup is already open, the “previously focused” element is actually popup content, not an origin. The fix should only prefer a captured element outside the floating element. I’ll encode that guard; this also protects effect re-runs while open. Then I’ll rerun the same suite.
- Assistant
The guard variable was added, but the branch replacement failed because Prettier had already expanded the condition across lines; consequently the old condition remained and the test still failed. The line-range edit also accidentally removed the first line of a nearby comment. I’ll correct both precisely, inspect the result, and rerun just the failing test first.
- Assistant
I’ll restore the full outside-press comment and use the computed outside-origin guard in the boolean return branch.
- Assistant
The failing keep-mounted case now passes. The implementation captures the external origin before initial focus moves into the popup, prefers it only for default boolean restoration, and retains the registered reference fallback when no valid external origin exists. I’ll rerun all four focused suites, then run TypeScript and lint for the changed file.
- Assistant
All focused jsdom tests passed (144 passed, 27 browser-only skipped), and TypeScript, ESLint, formatting, and diff checks succeeded. Since the reported examples rely on browser-only multiple-trigger behavior, I’ll run the dialog, popover, and alert-dialog detached-trigger suites in Chromium as an additional integration check.
- Assistant
Chromium tests could not start because the Playwright browser binary is absent; this is an environment limitation, not a test failure. The repository has Playwright installed, so I’ll attempt a direct local browser download once. If networking prevents it, I’ll retain the successful jsdom/type/lint verification and clearly account for the limitation.
- Assistant
The browser installation succeeded. I’ll rerun the browser-only detached-trigger integration suites now.
- Assistant
The Chromium integration suites pass (81 passed, 1 skipped). I’ll perform a final diff review for accidental changes, confirm only the intended source file is modified, and reassess edge cases (body focus, keep-mounted popups, explicit final focus, disconnected origin) before submission.
- Assistant
The final diff is limited to
FloatingFocusManager. The existing browser fixture matches the report: its “Open programmatically” button sets a different registered trigger ID, while the popup’s default focus restoration previously preferred that trigger. The new priority returns to the actual pre-open active element. I’ll add a concise rationale comment so future changes do not reintroduce the ordering bug, then do one final static check and submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.