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 to understand the codebase.
Assistant
I'll start by exploring the repository structure to understand the codebase.
Assistant
Let me explore the packages directory to find the relevant components.
Assistant
Let me look at the dialog root and trigger components, which likely share logic with alert-dialog and popover.
Assistant
Let me look at the popup utilities which seem to contain the shared logic for focus management.
Assistant
Now let me look at the store and focus management logic.
Assistant
Let me look at how focus return works when closing the dialog.
Assistant
Let me look at the focus return logic in the popup viewport and popup components.
Assistant
Let me search for focus restoration logic when closing dialogs.
Assistant
Let me look at the docs example for controlled mode with multiple triggers.
Assistant
Let me look at how the dialog and popover popups use the FloatingFocusManager.
Assistant
Let me look at the detached-triggers test file and check the existing test for focus behavior.
Assistant
Let me check the git log and CHANGELOG for any related context.
Assistant
Let me look at the most recent commit, which is about focus return.
Assistant
Assistant
Assistant
No direct mention. Let me look at the FloatingFocusManager more fully, particularly the
getReturnElement, and thedomReferencederivation.Assistant
Let me view the focus return logic in detail.
Assistant
Let me look at how the dialog store's
setOpenworks and howopenMethodis determined.Assistant
Let me look at existing focus return tests to understand expected behavior.
Assistant
Let me look at the popover popup and how it uses FloatingFocusManager, plus the popover trigger.
Assistant
Now let me create a reproduction test to confirm the bug. First, let me check the test setup.
Assistant
Let me create a reproduction test based on the existing detached-triggers test pattern.
Assistant
The tests are skipped because of JSDOM. Let me check the vitest config for browser environment.
Assistant
Let me try running with a real browser environment (chromium) to reproduce.
Assistant
Let me try the chromium browser environment.
Assistant
The browser flag didn't enable it. Let me check how browser tests are enabled. The
getBrowserConfig()readsprocess.env.VITEST_ENV. So I needVITEST_ENV=chromium. Let me try.Assistant
Let me install the playwright chromium browser.
Assistant
I've reproduced the bug. After closing the dialog opened programmatically, focus moves to the "Delete" trigger button instead of the "Open programmatically" button.
Now let me examine the exact code in the FloatingFocusManager to implement the fix.
Assistant
Let me view the exact lines around the "Track return focus targets" effect.
Assistant
Let me check the
activeElementfunction andcontainstypes.Assistant
Now I understand the types. Let me implement the fix. First, let me open the FloatingFocusManager file.
Assistant
Now let me add a ref to track the originating control element.
Assistant
Now let me update the "Track return focus targets" effect to capture the originating control. Let me view the current lines.
Assistant
My edit replaced the wrong lines and removed the
previouslyFocusedElementdeclaration. Let me fix this.Assistant
Now let me modify
getReturnElementto prefer the originating control.Assistant
Let me verify the
getNodeNameimport is available.Assistant
Good,
getNodeNameis imported. Now let me run the repro test.Assistant
The repro test passes. Let me extend the test to cover Escape close, Popover, and the fallback case (originating control removed).
Assistant
All 4 tests pass. Now let me run the existing Dialog and Popover tests to ensure I haven't broken anything.
Assistant
All 198 tests passed, but there are 19 unhandled errors. Let me check if these errors are pre-existing by running on the original code.
Assistant
The original code didn't show those 19 errors, so my change introduced them. Let me examine line 383 in the current file.
Assistant
Let me check the ref declarations to ensure I didn't accidentally break something.
Assistant
I see a problem — my first edit may have removed
isPointerDownRef. Let me check.Assistant
Confirmed —
isPointerDownRefis used but no longer declared. My first edit accidentally removed it. Let me restore it.Assistant
Now
isPointerDownRefis restored. Let me re-run the dialog tests to confirm no errors.Assistant
No errors now. Let me re-run my repro test and then run the broader test suites (Popover, Menu, Select, Tooltip, Alert Dialog).
Assistant
All passing. Let me run the remaining components that use FloatingFocusManager: Tooltip, Drawer, Combobox, Autocomplete, and the floating-ui-react tests.
Assistant
All passing. Let me look at the existing test about a detached trigger unmounting, which relates to the "control no longer available" edge case.
Assistant
This "detached dialog trigger unmounts" test covers the "control no longer available" case. It passed in my earlier run. Let me verify by running it specifically, plus run the jsdom (default) tests for the affected areas to ensure no regressions there.
Assistant
Let me run the default (jsdom) tests for the floating-ui-react components and popups to ensure no regressions there.
Assistant
All tests pass in both environments. Now let me consider the "control no longer available" edge case more carefully with a quick test, then clean up. Let me add a temporary test for the origin-removed fallback case.
Assistant
The fallback works. Now let me clean up the temporary test files and review my final diff.
Assistant
The diff is clean and minimal. Let me run a TypeScript type-check on the modified file to ensure no type errors.
Assistant
These are pre-existing build-output errors (TS6305), not related to my change. Let me find the proper type-check command.
Assistant
The typecheck is
tsc -b tsconfig.json(build mode). The TS6305 errors are because thebuild-testsoutput dir exists from a previous build but isn't in sync. Let me just run the build typecheck which uses the build config. Let me runtsc -bfrom the packages/react. Actually, let me just verify there are no NEW type errors in my file specifically by running tsc with the build config and ignoring the TS6305 noise.Let me run
npx tsc -b packages/react/tsconfig.build.jsonto type-check source only.Assistant
No type errors in the source build. Let me confirm by viewing the full build output.
Assistant
No type errors. Let me run eslint on the modified file to ensure no lint issues.
Assistant
No lint errors. Let me verify the Alert Dialog uses the same Dialog popup/FloatingFocusManager (so my fix covers it too).
Assistant
Alert Dialog reuses DialogPopup (which uses FloatingFocusManager), so my fix covers it. I already ran the alert-dialog tests in chromium and they passed.
Let me do a final review of the complete modified sections.
Assistant
Let me empirically verify my change doesn't alter focus behavior when switching triggers while a (non-modal) dialog is open — an edge case where
origincould differ from the active trigger.Assistant
Let me compare with the original code to ensure my change doesn't alter this behavior.
Assistant
Both original and modified pass identically — my change doesn't alter the trigger-switch behavior. Let me clean up the temporary test file.
Assistant
Let me do a final chromium run on the remaining popup-based components I haven't yet verified.
Assistant
All passing (1445 passed, 110 skipped, no errors). Let me do a final verification that all the original refs are intact in the final file.
Assistant
All original refs are intact, and my new
returnFocusOriginRefis properly added. The fix is complete.Let me review the final diff one more time 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.