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 focus-restoration logic and relevant tests for Alert Dialog, Dialog, and Popover. Then I’ll reproduce the controlled programmatic-opening behavior, implement a shared/general fix where appropriate, and run the focused test suites.
- Assistant
There is already a detached-trigger test file covering a programmatic button, so I’ll inspect that test and the root/trigger implementations. I’ll also check repository guidance and current working-tree state before editing.
- Assistant
The popup components delegate focus restoration to shared floating focus-management infrastructure, while the active trigger is selected through popup state. I’ll inspect those props, state transitions, and detached-trigger tests around focus to identify whether the correct fix belongs in shared event-details/state handling rather than three separate components.
- Assistant
The likely failure is that controlled
triggerIdintentionally sets the “active trigger” to (for example) Delete, and Floating UI consequently returns focus there, even though focus was on an external programmatic opener. I’ll inspect how opening events preserve their originating element and how the shared floating context chooses its return-focus target, including behavior when the origin is removed. - Assistant
FloatingFocusManager already records the element focused immediately before opening and has fallback logic for removed elements. I’ll read the exact priority ordering. The fix may simply require avoiding the active trigger as the Floating UI reference for focus return when the opening was controlled/programmatic, while retaining it for positioning and ARIA.
- Assistant
The default return target prioritizes
domReferenceover the previously focused element, explaining the bug:triggerIdmakes Delete the DOM reference. A robust solution should pass an explicit return target captured at the controlled closed→open transition, but only when the origin is not the selected trigger, and preserve FocusManager’s disconnected-origin fallback. I’ll examine controlled prop synchronization timing and existing utility support for finding the next DOM position after removal. - Assistant
A global reversal would risk changing established mouse/Safari behavior where clicking a trigger does not focus it; the reference must remain the default in genuine trigger openings. I’ll inspect FocusManager tests and opening metadata (
reason, native event, trigger) to distinguish an external controlled opener from a normal trigger interaction without introducing a public API. - Assistant
The open-change event carries the native interaction target for genuine trigger presses, while a controlled prop transition does not dispatch such an opening event. I’ll verify the FloatingRoot event shape and whether FocusManager can retain the opening event’s trigger; if so, the safest behavior is: genuine trigger event → reference, controlled programmatic opening → currently focused origin, with existing adjacent-tabbable fallback if that origin disappears.
- Assistant
FloatingRootStore.dataRef.current.openEventis retained for interaction-driven openings and is undefined for direct controlled state changes. This gives the exact distinction needed inside FocusManager without changing component APIs. I’ll inspect variable availability and the removed-origin requirement: existing FocusManager’s global focus history alone falls back to the reference when an explicit origin disconnects, so I may need to preserve the origin’s tab-order location with the same focus guards used for removed references. - Assistant
I’ll implement the distinction within FloatingFocusManager’s existing return-focus effect. For a direct/programmatic opening, it will capture the focused origin and the tabbable elements following it. On close it will return to the origin if connected, otherwise the first still-connected subsequent control; interaction-driven openings retain the existing reference-first behavior. Explicit
finalFocusremains authoritative. - Assistant
Python is unavailable in the environment, so no edit was applied. I’ll use the repository’s non-interactive
editcommand instead, then inspect the resulting diff. - Assistant
The second line-range edit used stale line numbers after the first insertion and corrupted the nearby conditional. I’ve identified the affected block and will replace it atomically with the complete intended implementation before proceeding.
- Assistant
The corrected block is now structurally sound. I’ll format it, run the focused FloatingFocusManager suite plus Dialog and Popover detached-trigger suites in Chromium (these tests are browser-only), and then run TypeScript validation. This will also reveal whether the shared change affects existing trigger-driven focus behavior.
- Assistant
The validation command did not run because Corepack attempted an interactive pnpm download prompt. I’ll disable that prompt explicitly and run formatting first, then execute each focused suite separately to stay within command time limits.
- Assistant
Formatting passed. Chromium tests are blocked only because the Playwright browser binary is missing, not by code failure. Per repository guidance I’ll install the required Chromium binary, then rerun the suites.
- Assistant
Chromium is installed successfully. I’ll now run the shared focus-manager suite and both detached-trigger suites; Alert Dialog shares DialogRoot’s implementation, and I’ll run its root suite afterward as an additional regression check.
- Assistant
Both detached-trigger browser suites pass (39 tests), and the shared browser tests pass. Because most FocusManager behavior tests are intentionally jsdom-only, I’ll run that suite in jsdom as well, followed by AlertDialogRoot, TypeScript, and targeted ESLint.
- Assistant
All executed tests pass, including 49 jsdom FocusManager tests and 42 Alert Dialog browser tests. TypeScript found one nullability issue in locating the origin in the tabbable list. I’ll add the required null guard, reformat, then rerun TypeScript and ESLint.
- Assistant
The literal replacement tool inserted escaped
\\ntext rather than newlines. I’ll correct that single line usingedit, then rerun the checks. - Assistant
TypeScript correctly notes that
activeElementis a generalElement, while the list is narrowed to focusable elements. Equality comparison viafindIndexavoids an unsafe cast and preserves the intended semantics. I’ll make that change and rerun validation. - Assistant
TypeScript now passes. ESLint requires the newly read
dataRefin the layout effect dependency list; it is a stable context ref, so adding it is correct and does not cause extra effect cycles. I’ll add it, rerun targeted lint, and inspect the final diff for logic and unintended changes. - Assistant
Reviewing the disconnected-origin edge case revealed that the captured “subsequent” list can include focus guards or controls inside the newly mounted popup (especially when the opener is the page’s last control). Those must never become return targets. I’ll filter captured fallback candidates to exclude the floating subtree and Base UI focus guards, then re-run type/lint and the focused FocusManager tests.
- Assistant
I’ll apply the exclusion at return time so it reflects current connectivity: candidates must be connected, outside both the floating wrapper and focus element, and not a focus guard. This preserves ordinary controls after the removed origin while preventing focus from being sent into a closing popup.
- Assistant
The disconnected fallback is now safe, and TypeScript, ESLint, formatting, and all 49 applicable jsdom FocusManager tests pass. I’ll add a concise rationale comment, perform one final diff/status review, and rerun the two issue-specific Chromium suites after the final code shape.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.