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
Now I want to understand how focus is returned to the trigger when the dialog closes. Let me look at how focus management works, particularly around the trigger element and the open programmatically scenario.
Let me look at the dialog popup component and how focus is managed there.
Assistant
Now let me look at how focus is returned to the trigger. Let me search for where focus is returned on close.
Assistant
Now let me look at how focus is returned to the trigger element. Let me look for the focus management on close, particularly in the dialog popup and the useFocusReturn or similar.
Assistant
Let me see the top part of the FloatingFocusManager that was truncated.
Assistant
Let me look at
useSyncedFloatingRootContextto understand how the reference element is synced.Assistant
Let me look at the docs examples for controlled mode with multiple triggers to confirm my understanding.
Assistant
Let me check the alert-dialog demo, since the issue mentions the Alert Dialog example specifically.
Assistant
Let me check which components use FloatingFocusManager and with what returnFocus settings.
Assistant
Let me look at the popover controlled demo and the PopoverPopup to understand the popover case.
Assistant
Let me look at PopoverPopup.tsx to understand the popover focus behavior.
Assistant
Let me look at the full FloatingFocusManager to understand effect ordering, especially the initial focus effect.
Assistant
Let me run the existing FloatingFocusManager tests to get a baseline before making changes.
Assistant
Let me examine the existing tests related to return focus to understand the expected behavior.
Assistant
Let me look at the "preserves tabbable context next to reference element if removed" test, since it tests the case where the originating control is removed - which is relevant to the edge case mentioned in the issue ("If the control is no longer available, focus should move next to that originating control's position in the DOM").
Assistant
Let me look at the hover-related return focus tests, since hover-triggered popovers are a concern.
Assistant
Let me look at the hover + esc key return focus tests.
Assistant
Let me look at the outside press return focus tests.
Assistant
Let me check how
domReferenceElementis set in the FloatingRootStore and synced.Assistant
Let me look at the "does not insert fallback element when return element is falsy" test and a few others.
Assistant
Let me search for existing tests related to multiple triggers and programmatic open / focus return.
Assistant
This is getting complex. Let me write a reproduction test to observe the actual current behavior. Let me first check the test setup and how Dialog tests work.
Assistant
Let me create a temporary reproduction test to observe the actual current behavior.
Assistant
The "Close" button isn't found because the Backdrop is intercepting? Actually, the issue is
screen.getByText('Close')failed - the Close button isn't rendered. Maybe the dialog didn't open. Let me look - the issue might be that with the backdrop, the click on trigger doesn't open. Or maybe BASE_UI_ANIMATIONS_DISABLED needs a different setup. Let me look at how existing DialogPopup tests handle this. They useawait user.click(trigger)and thenscreen.getByText('Close'). Let me check the "should focus the trigger by default when closed" test - it works. The difference: my test has<Dialog.Backdrop />before trigger, and the trigger is inside. Hmm, same as the working test.Wait, the error is "Close" not found. Let me check - maybe the dialog closed immediately? Or maybe my structure is wrong. Actually in the working test, the structure is identical. Let me re-run but check more carefully. Actually maybe the issue is the
waitForimport oruser. Let me just use the exact structure from the passing test but add the programmatic button.Let me rewrite the repro test to match the controlled multi-trigger demo.
Assistant
useris undefined. ThecreateRenderer()returns{ render }not{ render, user }in this context? Let me check the createRenderer signature. The detached-triggers test usedconst { user } = await render(...). Sorenderreturns an object withuser. Let me fix.Assistant
So
usercomes from the render result. Let me fix the test.Assistant
I've reproduced the bug: focus goes to "Delete" (the active trigger) instead of "Open programmatically" (the originating control).
Now let me look at the exact
getReturnElementcode to make the fix.Assistant
Let me check if there are tests for non-focusable references clicked by mouse while focus is elsewhere.
Assistant
Let me search for nested dialog focus-return tests that might be affected.
Assistant
Let me look at the dialog finalFocus tests around lines 480-700 to understand expected behaviors.
Assistant
Let me look at the tests around lines 620-710.
Assistant
The nested case is a concern with the global list approach. Let me search for nested dialog focus-return tests.
Assistant
I've decided on a robust approach using a local ref to capture the originating control (the element that had focus when the popup opened), rather than relying on the global list (which has ordering issues with nested dialogs). Let me view the full "Track return focus" effect to place the code correctly.
Assistant
Let me look at the start of effect #1 where
addPreviouslyFocusedElementis called.Assistant
Let me find where the refs are declared to add the new ref.
Assistant
Now let me implement the fix. First, add the ref:
Assistant
No python3. Let me use the editor tools. Let me open the file.
Assistant
Now let me make the edits using the replace command.
Assistant
Now let me add the capture logic in effect #1.
Assistant
Now let me update the boolean branch in
getReturnElement.Assistant
Now let me run the repro test and the FloatingFocusManager tests.
Assistant
The repro test now passes and all FloatingFocusManager tests pass. Let me run the broader test suites for the affected components.
Assistant
All passing. Let me run the menu, select, combobox, and drawer tests too, since they also use FloatingFocusManager.
Assistant
All passing. Let me extend my repro test to cover the alert-dialog and popover scenarios and the Escape close path mentioned in the issue.
Assistant
Let me check
usePopupViewportto understand whether the popup/FloatingFocusManager remounts when the active trigger changes.Assistant
The content key is applied to inner viewport children, not the FloatingFocusManager, so the ref persists. Let me run tooltip, preview-card, and navigation-menu tests to be safe.
Assistant
All passing. Let me run the full floating-ui-react test suite since I modified a core file.
Assistant
Let me run the popups utils tests and then the full react test suite to ensure nothing else broke.
Assistant
Now let me clean up my temporary repro test file before submitting.
Assistant
Let me review the final diff.
Assistant
Let me run a TypeScript typecheck on the modified file to ensure no type errors.
Assistant
These are pre-existing build-artifact errors (TS6305), not related to my change. Let me filter for any errors mentioning my file or actual type errors.
Assistant
No actual type errors—only pre-existing TS6305 build-artifact errors. Let me confirm
getNodeNameandcontainsare already imported (they're used elsewhere in the file).Assistant
Let me check the DrawerPopup's FloatingFocusManager usage to ensure my change is consistent there.
Assistant
Let me run the full react test suite to be fully thorough, since I modified a core shared file.
Assistant
The entire react test suite passes (6103 passed, 0 failed). Let me do a final review of the complete diff.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.