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 understanding the issue. The problem is about focus management in Alert Dialog, Dialog, and Popover components. When a dialog is opened programmatically (via a button), and then closed, focus moves to the wrong trigger instead of returning to the button that triggered the programmatic open.
Let me first explore the repository structure to understand how it's organized.
Assistant
Let me explore the repository structure more to understand how it's organized and find the relevant files for Alert Dialog, Dialog, and Popover components.
Assistant
Let me explore the alert-dialog, dialog, and popover components to understand their structure.
Assistant
Now I need to understand the structure of these components, particularly how focus management works when dialogs are opened programmatically.
Assistant
Let me explore the dialog and alert-dialog components to understand how focus management works. The issue is about focus returning to the wrong trigger when a dialog is opened programmatically. I need to find where focus management is handled.
Assistant
Let me examine the key files related to focus management. The issue is about focus not returning to the correct trigger when dialogs are closed after being opened programmatically. Let me start by looking at the dialog component's store and root files to understand how focus management works.
Assistant
Let me look at the DialogStore.ts to understand how focus management works.
Assistant
Now I need to understand the
setOpenTriggerStatefunction and how focus is managed. Let me look at the popups utility file.Assistant
Let me search for the popups utility file.
Assistant
The popups utility is in
../../utils/popupsrelative to the dialog directory. Let me look for it there.Assistant
Let me find the popups utility file.
Assistant
Let me search for the popup utilities more specifically - they're imported as
../../utils/popupsfrom the dialog/store/DialogStore.ts file.Assistant
The
popupsdirectory exists at/base-ui/packages/react/src/utils/popups. Let me explore it.Assistant
Now I need to understand the focus management. Let me look at the key files, especially
popupStoreUtils.tssince that likely contains thesetOpenTriggerStatefunction and focus management logic.Assistant
Now I understand the
setOpenTriggerStatefunction. Let me look at the store.ts to understand howactiveTriggerIdandactiveTriggerElementare managed.Assistant
Now let me look at how focus is managed when closing. Let me find the focus manager or related components.
Assistant
Now I understand the focus management. Let me look at the DialogRoot component to understand how the context is set up.
Assistant
Let me look at the render implementation for dialog root:
Assistant
Let me look at the DialogHandle and how programmatic opening works:
Assistant
Now I'm starting to understand the issue. Let me look at how the focus is returned when closing. The issue is:
- When a dialog is opened programmatically (via
DialogHandle.open()), thetriggerparameter isundefined - In
DialogStore.setOpen(), when opening with no trigger,setOpenTriggerStatesetsactiveTriggerIdandactiveTriggerElementtonull - When closing, the
returnFocuslogic inFloatingFocusManagerreturns focus to theactiveTriggerElement(which isnull) ordomReference(which would be a different trigger element)
The key issue is: when opening programmatically, the code should track what element had focus at the time of opening so that focus can be returned there when closing.
Let me look at
createChangeEventDetailsto understand how the trigger is passed:- When a dialog is opened programmatically (via
Assistant
Now let me look at the DialogTrigger component to understand how triggers work. The issue seems to be that when opening programmatically (via
DialogHandle.open()without a triggerId), the dialog should track the currently focused element (which would be the "open programmatically" button) so that focus can be returned there when the dialog closes.Let me look at the trigger implementation.
Assistant
Now I have a good understanding of the issue. Let me look at how the floating context and reference element work:
Assistant
Let me understand better how
domReferenceis used inFloatingFocusManager:Assistant
Let me look at where
domReferenceis set in the floating context:Assistant
Let me understand how the reference element is set in the floating context:
Assistant
Now let me look at the FloatingRootStore more thoroughly to understand how referenceElement/dragReferenceElement work:
Assistant
Now I understand the issue. Let me look at the test files to see if there are any existing tests related to this focus behavior:
Assistant
Let me look at the test file for detached triggers to understand the existing test cases and the specific scenario.
Assistant
Let me look at how the floating context is synced to understand where
domReferenceElementcomes from:Assistant
Now I have a clear understanding of the issue. Let me trace through the flow:
- When a dialog is opened programmatically (via
DialogHandle.open()with no triggerId oropenWithPayload()),eventDetails.triggerisundefined - In
setOpenTriggerState, whenopen=trueandtriggeris undefined, it setsactiveTriggerElement = null - When the dialog closes,
FloatingFocusManager.getReturnElement()returns focus based ondomReference(which isactiveTriggerElement), and falls back togetPreviouslyFocusedElement() - But
getPreviouslyFocusedElement()tracks elements that had focus before entering the floating element, not the element that was focused when opening programmatically
The fix should capture the currently focused element when opening programmatically, so focus can be returned to it when the popup closes.
Let me look at the
activeElementutility to use it properly:- When a dialog is opened programmatically (via
Assistant
Now I have a clear understanding of the issue. Let me also look at the Popover and AlertDialog stores to see if the fix needs to be applied in multiple places.
Assistant
Now let me look at the Popover component to understand if it has the same issue:
Assistant
Let me also look at the PopoverHandle:
Assistant
Assistant
The user wants me to fix an issue where focus doesn't return to the correct trigger when opening a dialog/popover/alert-dialog programmatically. I was in the middle of investigating the codebase when the command got cut off. Let me continue with my investigation and implement the fix.
Assistant
Let me continue investigating the fix. I need to understand how to get the active element in the popupStoreUtils.
Assistant
Let me look at the
activeElementutility:Assistant
Now I have a clear understanding of the codebase. Let me implement the fix. The issue is in the
setOpenTriggerStatefunction - when opening a popup without a trigger (programmatically), it should capture the currently focused element so that focus can be returned to it when the popup closes.Assistant
Let me now implement the fix. I need to modify the
setOpenTriggerStatefunction in/base-ui/packages/react/src/utils/popups/popupStoreUtils.tsto capture the currently focused element when opening without a trigger.Assistant
Let me see the exact lines of the
setOpenTriggerStatefunction.Assistant
Now I understand the issue. When a popup/dialog is opened programmatically (via
handle.open(null)orhandle.openWithPayload()), thetriggerparameter isundefined, sosetOpenTriggerStatesetsactiveTriggerElement = null. This means when the popup closes, focus has no reference to return to (or falls back to a wrong element).The fix is to capture the currently focused element when opening without an explicit trigger. Let me implement this fix.
Assistant
The file is not open in the editor. I need to use the --file option instead.
Assistant
The changes have been applied. Let me verify the file looks correct:
Assistant
The user wants me to verify the changes I made to the file. Let me check the file content to make sure the changes are correct.
Assistant
The changes look good. Let me verify the fix is working correctly by running the existing tests for the dialog component and also checking if there are any related tests for focus management.
Assistant
Let me try running the tests with a shorter timeout or find the test files:
Assistant
Let me try a different approach to run the tests.
Assistant
All dialog tests pass. Let me also run the popover and alert-dialog tests to make sure the fix doesn't break anything:
Assistant
All tests pass. Let me also run the popup store utils tests specifically to verify the changes:
Assistant
All tests pass. Let me verify the fix one more time by looking at the complete diff:
Assistant
The fix looks good. Let me also think about edge cases and run a broader set of tests to make sure nothing is broken.
The key edge cases are:
- When opening with an explicit trigger: The
triggerparameter is provided, sogetActiveElement()is not called. Existing behavior is preserved. - When closing:
openisfalse, so the existing logic of keeping the previous value is preserved. - When opening without a trigger (programmatic): We now capture
document.activeElementas the active trigger element. - If
document.activeElementis<body>: This could happen if no element is focused when opening programmatically. The fix would capture the body, which isn't ideal but is also not harmful. - SSR safety: The try/catch block handles cases where
documentmight not be available.
Let me run a broader set of tests to confirm the fix doesn't break anything:
- When opening with an explicit trigger: The
Assistant
All tests pass. Let me also run the complete test suite for the menu component (which uses similar popup patterns) to make sure the fix doesn't cause any regressions:
Assistant
All tests are passing. Let me provide a summary of my changes and submit:
Summary of Changes
Issue: When a dialog, alert dialog, or popover is opened programmatically (via
handle.open(null)orhandle.openWithPayload()), closing it would return focus to the wrong element instead of the element that triggered the programmatic open.Root Cause: The
setOpenTriggerStatefunction inpopupStoreUtils.tswas settingactiveTriggerElementtonullwhen opening without an explicit trigger. This caused the focus management system to not know which element to return focus to when the popup closed.Fix: Modified
setOpenTriggerStateto capture the currently focused element (document.activeElement) when opening without an explicit trigger. This ensures that when the popup closes, focus is returned to the element that had focus when the programmatic open was triggered.Assistant
The submit command needs to be called without arguments. Let me try again.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.