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
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me look at the store and the focus handling. The issue is about returning focus to the originating control. Let me look at the store.
Assistant
Now let me look at the focus management - where focus is returned to a trigger after closing.
Assistant
Now let me look at the FloatingFocusManager to understand how returnFocus works.
Assistant
Let me look at the docs examples for the controlled mode with multiple triggers.
Assistant
So the scenario: There are multiple triggers. The "Open programmatically" button sets
triggerIdto 'alert-trigger-2' (the Delete button) and opens the dialog. So the triggerId points to the Delete button, which becomes the active trigger. When closing, focus returns to the Delete button, not the "Open programmatically" button.The issue is that when opening programmatically, the focus is on the "Open programmatically" button, but the return focus goes to the active trigger (Delete button).
The FloatingFocusManager uses which is the floating reference element (the active trigger). When is (boolean), it returns to (the active trigger), not the previously focused element.
Assistant
So
domReferenceis thedomReferenceElementfrom the store. This is the floating reference element. In the case of multiple triggers, the reference element is the active trigger element (the Delete button). So returnFocus=true returns to it.Now, when opening programmatically, the actual element that had focus is the "Open programmatically" button. The FloatingFocusManager records
previouslyFocusedElement(the active element when the dialog mounts).Let me look at how
domReferenceElementis set in the floating store, and the relationship to activeTriggerElement.Assistant
Assistant
Let me look at the detached triggers tests to understand the edge cases.
Assistant
Let me look at the focus tests in the detached triggers test, especially around lines 600-744.
Assistant
Let me look at the focus-related tests in the dialog root test file.
Assistant
Let me look at how Popover uses FloatingFocusManager and its focus handling.
Assistant
Let me read the full FloatingFocusManager to understand the previouslyFocusedElement tracking and the cleanup logic fully.
Assistant
Let me check the order of the layout effects (initialFocus vs return focus tracking).
Assistant
Let me look at the FloatingFocusManager tests to understand return focus behavior expectations.
Assistant
Let me look at the nested dialog return focus test.
Assistant
Let me look at the Dialog test component used in FloatingFocusManager tests.
Assistant
Let me check some specific tests that might be affected.
Assistant
This uses defaultOpen=true. At mount, activeElement = body. The popup's initialFocus moves focus to removeButton (first tabbable). previouslyFocusedElement = body (excluded). When closing via outside press (click body), returnFocus. domReference = trigger? Wait, TestDialog - let me check what trigger it has. Actually this test uses defaultOpen and probably has a trigger. Let me check TestDialog.
Actually, this test doesn't check final focus after close (it checks popup focus during, then that dialog is gone). Let me not worry too much.
Let me now also check the FloatingFocusManager tests for defaultOpen-like scenarios. The "App" component opens on click, not defaultOpen.
Let me now also check the test at line 545-550 ("returns focus to reference on outside press when preventScroll is supported"):
- click reference. Effect starts: previouslyFocused = reference. domReference = reference.
Assistant
The Navigation component uses hover to open. Let me look at the Navigation component to understand how it uses FloatingFocusManager (likely with disabled on hover, so the tracking effect doesn't capture).
Assistant
Assistant
AlertDialog reuses Dialog. So the fix in FloatingFocusManager covers all three (Dialog, AlertDialog, Popover).
Now let me write a reproduction test to confirm the issue before fixing. Let me create a temporary test that mimics the programmatic open scenario.
Assistant
Let me create a temporary reproduction test file in the dialog folder.
Assistant
The detached-triggers tests are
describe.skipIf(isJSDOM)so they only run in chromium. Let me run the repro test in chromium.Assistant
The chromium browser tests might take long to download/setup. Let me try jsdom first to see if it at least runs (focus behavior in jsdom might be limited but let's see). Actually, the detached triggers tests skip jsdom. Let me try jsdom anyway to see if the test infra works.
Assistant
Vitest might be hanging because of the browser project config requiring playwright. Let me check the vitest config to understand how to run unit tests.
Assistant
The chromium environment requires playwright browser. It might be downloading. Let me check if the browser is available. Let me check if there's a cached chromium for playwright.
Assistant
No chromium installed. Let me check if playwright can install it.
Assistant
Chromium headless shell installed. Now let me retry running the repro test.
Assistant
Hmm, still timing out. Maybe it needs the full chromium browser (not just headless shell). Or maybe the
--project @base-ui/reactfilter doesn't match. Let me check the partial output without tail to see what's happening. Let me redirect to a file.Assistant
The test ran and confirmed the bug: focus went to the "Delete" button (trigger-1) instead of the "Open programmatically" button. The vitest was in watch mode (timeout). I need to use
vitest run.Now let me apply the fix to FloatingFocusManager.
Assistant
Now let me verify the activeTriggerElement is set synchronously during the open.
Assistant
Now let me run the repro test with the fix applied.
Assistant
The repro test passes. But I'm worried about the mid-open focus steal. Let me add an assertion to the repro test that focus is inside the dialog while open.
Assistant
Focus is inside the dialog while open AND returns to the programmatic button on close. Now let me run the broader existing test suites to make sure I didn't break anything.
Assistant
Only 3 passed (48 skipped are jsdom-only). Let me run the FloatingFocusManager tests in jsdom.
Assistant
One test failed: "supports keepMounted behavior". Let me look at this test.
Assistant
Let me check whether this test passed before my change by temporarily reverting.
Assistant
I found the issue: the keepMounted test reopens the floating while focus is inside it (via
fireEvent.clickwhich doesn't focus), sopreviouslyFocusedbecomes the floating's child. I need to exclude elements that are inside the floating tree. Let me refine the fix.Assistant
Now let me re-run the FloatingFocusManager tests in jsdom.
Assistant
Now let me re-run the repro test and then the broader Dialog/Popover/AlertDialog test suites.
Assistant
Now let me run the Dialog, AlertDialog, and Popover test suites in both environments.
Assistant
One Popover test failed: PopoverRoot.test.tsx:802. Let me look at that test.
Assistant
This test: "closes a nested combobox popup when tabbing backward to the trigger". Let me look at the Combobox's FloatingFocusManager config.
Assistant
Let me look at how
inputInsidePopupis determined for the Combobox.Assistant
Let me run this specific test on the original code to confirm it's my change that broke it.
Assistant
Let me run the test with my change and get the full output.
Assistant
When run in isolation with my change, it passes. The full-suite failure suggests test ordering/state pollution. Let me re-run the full popover suite.
Assistant
Interesting! Popover alone passes (407 passed). The earlier failure was when running dialog + alert-dialog + popover together. Let me re-run the combined suite to check consistency.
Assistant
Now it passes (766 passed)! So the earlier failure was flaky. Let me check if this flakiness is pre-existing by running the combined suite on the original code.
Assistant
The repro test (untracked) stayed during stash, so on original only the repro fails. The combobox test passed on original in all 3 runs. So my change might introduce combobox flakiness. Let me remove the repro test and run the combined suite with my change multiple times.
Assistant
Let me check if the combobox flakiness is pre-existing on the original code by running the combined suite multiple times (repro already deleted).
Assistant
The combobox test ALSO fails flakily on the ORIGINAL code (run 5). So the flakiness is PRE-EXISTING, not introduced by my change. Good. Now let me run the broader test suite. Let me run the full @base-ui/react project in jsdom first.
Assistant
All jsdom tests pass (6103 passed, 0 failed). Now let me recreate the repro test and run the chromium full project suite.
Assistant
Now let me run the chromium full project suite (this includes the repro test and all browser-only tests).
Assistant
Only my repro test failed in the full chromium run (it passed in isolation). Let me see the failure details.
Assistant
The repro test failed because
findByRole('dialog')couldn't find the dialog — the tab-loop approach is flaky in the full suite (Enter didn't trigger the button). This is a test-harness issue in my repro test, not the fix. Let me make the repro test robust by focusing the button directly.Assistant
Now let me run the full chromium suite again with the robust repro test.
Assistant
All chromium tests pass (6691 passed, 0 failed). Let me check the
containsutility to make sure my usage is safe with potential nullfloating.Assistant
contains(parent, child)acceptsElement | null | undefinedand handles null. Socontains(floating, previouslyFocusedElement)is safe even if floating is null. Good. Let me view the full function to be sure it returns false for null parent.Assistant
The full jsdom (6103 passed) and chromium (6691 passed) suites pass. Let me verify the Popover scenario specifically, since it has a different
returnFocus/finalFocusdefault. Let me check PopoverPopup's finalFocus default.Assistant
PopoverPopup passes
returnFocus={finalFocus}where finalFocus defaults to undefined, which triggers the FloatingFocusManager default oftrue. So my fix applies to Popover too. Let me write a quick Popover repro to confirm.Assistant
The popover repro passes. Let me confirm it FAILS on the original code to verify the bug existed for Popover.
Assistant
Confirmed: the popover repro FAILS on original (focus goes to Delete) and PASSES with my fix. The fix works for Dialog, AlertDialog (via Dialog), and Popover.
Now let me review my final change and clean up the temporary repro tests.
Assistant
Let me run eslint and prettier on the modified file to ensure no lint/format issues.
Assistant
Both eslint and prettier pass. Now let me delete the temporary repro test files (per instructions, I must not add new tests to the repository).
Assistant
Now only the FloatingFocusManager.tsx change remains. Let me do a final run of the most affected suites in both environments after cleanup.
Assistant
All affected component suites pass in both environments (jsdom: 2650 passed, chromium: 2934 passed), with no new failures. The fix is complete and the temporary repro tests have been removed.
Let me do a final review of the complete diff 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.