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 to understand the structure and locate the relevant components. Let's first see what's in the root.
- Assistant
Let's explore the repository structure more systematically.
- Assistant
Let's look at the packages directory structure to find the relevant components.
- Assistant
Let me explore the packages/react directory to find components related to Alert Dialog, Dialog, and Popover.
- Assistant
Let me explore the alert-dialog, dialog, and popover components to understand the structure and find the relevant code.
- Assistant
Let me look at the focus management logic in the dialog/alert-dialog/popover components. The issue is about focus returning to the wrong trigger when using "Open Programmatically" buttons. Let me examine the relevant code.
- Assistant
Let me look at the dialog root to understand how focus is managed when closing the dialog.
- Assistant
Let me look at the relevant root hooks for each component and understand focus management:
- Assistant
Let me look at the utilities that manage popup focus behavior, specifically the
useImplicitActiveTriggerfunction and related utilities: - Assistant
Let me find where the popups utilities are.
- Assistant
Let me check the popups directory.
- Assistant
Let me look at the relevant files to understand how focus management works.
- Assistant
Let me look at the store.ts file since it's likely where focus management happens.
- Assistant
Now let's look at the popupStoreUtils.ts file to understand how focus management works:
- Assistant
Now let me understand how focus is returned to the trigger when the dialog closes. Let me look at the dialog trigger and close components:
- Assistant
Now I need to understand how focus is managed when the dialog closes. Let me search for focus management related code.
- Assistant
Let me look at the DialogPopup component to understand how focus is managed when the dialog closes.
- Assistant
Let me look at the DialogRoot component to understand how focus management works when the dialog closes:
- Assistant
Now let me look at how the focus is managed when the dialog closes. Let me search for focus management in the floating-ui-react integration.
- Assistant
Let me examine the FloatingFocusManager to understand how it handles focus return when closing:
- Assistant
Let me continue reading the FloatingFocusManager to understand how it handles focus return when closing.
- Assistant
Let me search for where focus is returned when the dialog closes:
- Assistant
Let me look at the section around line 737 and line 781 to understand how returnFocus is handled:
- Assistant
Now I understand the issue! Let me look at how the
domReferenceis set when opening programmatically. Let me check the alert dialog and popover components: - Assistant
Let me look at the test file for detached triggers to understand the pattern:
- Assistant
Now let me look at the documentation examples to understand the pattern:
- Assistant
Let me search for the relevant examples in the docs folder.
- Assistant
Let me search for the example files more broadly.
- Assistant
Let me look at the alert dialog example:
- Assistant
Need to quote the path with parentheses.
- Assistant
Let me look at the example to understand the pattern. Now let me understand how the focus return works. The key is in the FloatingFocusManager's
getReturnElement()function: - Assistant
Now I understand the problem. Let me trace the focus flow:
- When "Open programmatically" button is clicked,
open=trueandtriggerId='alert-trigger-2'are set - The
FloatingRootContext.domReferenceElementgets set to the "Delete" trigger's DOM element (the one matchingtriggerId) FloatingFocusManagersaves the "Open programmatically" button aspreviouslyFocusedElement- On close,
getReturnElement()returnsdomReference(the "Delete" button) instead of thepreviouslyFocusedElement(the "Open programmatically" button)
The issue is in
getReturnElement()- it always prefersdomReferenceoverpreviouslyFocusedElement. For programmatic opens, we should prefer thepreviouslyFocusedElement.Let me look at how
openMethodis tracked to understand if there's already a mechanism to distinguish programmatic opens: - When "Open programmatically" button is clicked,
- Assistant
Let me look at the
useOpenMethodTriggerPropsthat's used in DialogTrigger: - Assistant
Let me look at this file.
- Assistant
Now let me look at the Popover component to see how it handles this:
- Assistant
Now I understand the issue. Let me look at the
getReturnElement()function in detail: - Assistant
Let me look at how focus is returned and check for related tests:
- Assistant
Let me search for tests related to focus return behavior in the dialog or alert dialog tests.
- Assistant
Let me look at the focus return test to understand how it currently works.
- Assistant
Let me now understand the core problem better. I need to look at how the
domReference(the trigger element used for return focus) is set when a popup is opened programmatically: - Assistant
Now I understand the issue clearly. The
getReturnElement()function inFloatingFocusManageralways prefersdomReference(which is the trigger element associated withtriggerId) overpreviouslyFocusedElement. When a dialog is opened programmatically via a separate button that setstriggerIdandopen, the focus returns to the trigger element instead of the actual button that was clicked.Let me first create a reproduction test to confirm the bug:
- Assistant
Let me check the existing test for focus return behavior:
- Assistant
Now I understand the problem clearly. Let me look at the key area: in
getReturnElement(), whenreturnFocusistrue, it always returnsdomReference(the trigger matchingtriggerId) if it's connected. But when a dialog is opened "programmatically" (via a separate button settingopen={true}andtriggerId), focus should return to the previously focused element (the "Open programmatically" button), not the trigger element.The key insight is: when
openInteractionTypeisnull(meaning the popup wasn't opened via a trigger click), we should preferpreviouslyFocusedElementoverdomReference.Let me now implement the fix:
- Assistant
Let me navigate to the
getReturnElementfunction: - Assistant
Let me look at the full
getReturnElementfunction and the surrounding cleanup code: - Assistant
Now I have a clear understanding of the issue. Let me look at the full flow more carefully to understand how the
previouslyFocusedElementrelates todomReference: - Assistant
Now I have a clear understanding of the issue. Let me trace the problem:
- When the "Open programmatically" button is clicked, it sets
open=trueandtriggerId='alert-trigger-2' - The dialog store's
activeTriggerElementgets set to the "Delete" button's DOM element (the trigger matchingtriggerId) - This becomes
domReferenceinFloatingFocusManager - When closing,
getReturnElement()returnsdomReference(the "Delete" button) instead of the "Open programmatically" button
The fix: In
getReturnElement(), when the popup was opened programmatically (i.e.,openInteractionTypeisnull), preferpreviouslyFocusedElementoverdomReference. - When the "Open programmatically" button is clicked, it sets
- Assistant
Now I have a clear understanding of the issue. Let me trace through the problem:
- When "Open programmatically" button is clicked, it sets
triggerIdandopenstate externally - The
activeTriggerElementin the store gets set to the trigger matchingtriggerId(e.g., the "Delete" button) - This becomes
domReferenceinFloatingFocusManager - When closing,
getReturnElement()returnsdomReference(the "Delete" button) instead of the element that was actually focused when the popup opened (the "Open programmatically" button)
The key insight: when the popup opens,
previouslyFocusedElement(a local variable in the effect closure) correctly captures the element that was focused at that time. For trigger clicks,previouslyFocusedElementequalsdomReference. For programmatic opens, they differ.The fix should check: if
previouslyFocusedElementdiffers fromdomReference, preferpreviouslyFocusedElement(since it represents the actual originating control). - When "Open programmatically" button is clicked, it sets
- Assistant
Now I have a thorough understanding of the issue. Let me implement the fix. The problem is in
getReturnElement()- whenreturnFocusistrue, it always returnsdomReference(the trigger element matchingtriggerId). But when opened programmatically,previouslyFocusedElement(captured in the effect closure) is the actual "Open programmatically" button that should receive focus.The fix: prefer
previouslyFocusedElementoverdomReferencewhen they differ andpreviouslyFocusedElementis still connected. - Assistant
Now let me implement the fix:
- Assistant
I see there are duplicate lines. Let me fix that:
- Assistant
I see the issue - I accidentally removed the
return resolveRef(...)line. Let me fix this properly: - Assistant
Good. Let me verify the full
getReturnElementfunction looks correct: - Assistant
Good, the fix looks clean. Now let me run the existing tests to verify nothing is broken:
- Assistant
Let me check how tests are run in this repository:
- Assistant
Let me check the vitest config and how to run specific tests.
- Assistant
Let me run just the dialog tests to verify my change doesn't break anything.
- Assistant
All dialog tests pass. Let me run the alert-dialog and popover tests too:
- Assistant
Good, alert dialog tests pass. Let me run the popover tests:
- Assistant
There's a test failure in the popover tests. Let me look at that test:
- Assistant
Let me check if this test was already failing before my change by reverting temporarily:
- Assistant
Let me run the popover test without my changes to see if it was already failing.
- Assistant
Good, the test was passing before my change. Let me restore my changes and investigate why it fails:
- Assistant
I see the test failure. The issue is that
previouslyFocusedElementmight not be the trigger in all cases - it depends on timing. Let me think about this differently. Let me look at the test more carefully and understand whatpreviouslyFocusedElementwould be: - Assistant
Let me look more carefully at the test. The issue might be timing-related. Let me check what happens during the popover's close:
- Assistant
Let me look at the order of effects in FloatingFocusManager to understand the timing:
- Assistant
Let me look at the initial focus effect at line 655 to understand the order:
- Assistant
I see - the issue is that
previouslyFocusedElementis captured in the effect closure at the time the effect runs, but the ordering of effects matters. Let me look at the initial focus effect more carefully. ThepreviouslyFocusedElementin the return focus effect (line 727) is captured when the effect runs, which is AFTER the initial focus effect (line 655) has already queued a microtask to move focus. But since it'squeueMicrotask, focus hasn't actually moved yet.Wait, actually the issue might be more subtle. The
previouslyFocusedElementis a local variable inside the layout effect closure. In the trigger-click case, this should be the trigger element. AnddomReferenceshould also be the trigger element. SopreviouslyFocusedElement !== domReferenceshould befalse, and we'd fall through to the existingdomReferencecheck.Let me re-examine why the Popover test fails. Let me check what
domReferenceis: - Assistant
Let me look at the focus guard mechanism used by Popover:
- Assistant
Looking at this more carefully, I realize the issue might be more subtle. Let me rethink my approach. The problem with comparing
previouslyFocusedElement !== domReferenceis that it could affect cases where they happen to be different for reasons other than programmatic open.Let me take a different approach - use
openInteractionTypeRefto detect programmatic opens: - Assistant
Now let me run the tests again:
- Assistant
Good, popover tests pass. Let me run the dialog tests again:
- Assistant
Good, dialog tests pass too. Let me also run the FloatingFocusManager tests:
- Assistant
Let me also run the alert-dialog and other popup-related tests:
- Assistant
All tests pass. Now let me create a reproduction script to verify the fix works for the specific issue:
- Assistant
All 3 reproduction tests pass. Let me clean up the temporary test file and run a broader test:
- Assistant
Let me run a broader set of tests to make sure nothing else is broken:
- Assistant
Let me also run the combobox and autocomplete tests since they use floating elements:
- Assistant
Let me also run the navigation-menu tests:
- Assistant
All tests pass. Let me review the final change:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.