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 what we're working with and find the relevant components mentioned in the issue: Alert Dialog, Dialog, and Popover components.
The issue is about focus management when using "Open programmatically" buttons in controlled mode with multiple triggers. When closing a dialog after opening it via a programmatic button, focus should return to that button rather than moving to a different control.
Let me first explore the repository structure.
Assistant
This is a Base UI repository. Let me explore the packages directory to find the relevant components.
<tool_call> <function=bash_terminal> <parameter=command> ls -la /base-ui/packages </tool_call>
Assistant
Let me run the bash command properly to list the packages directory.
Assistant
Let me explore the react package which likely contains the Alert Dialog, Dialog, and Popover components.
<tool_call> <function=bash_terminal> <parameter=command> ls -la /base-ui/packages/react </tool_call>
Assistant
The command wasn't executed properly. Let me run it again.
Assistant
Let me explore the src directory to find the Alert Dialog, Dialog, and Popover components.
<tool_call> <function=bash_terminal> <parameter=command> ls -la /base-ui/packages/react/src </tool_call>
Assistant
I need to use the bash_terminal tool to execute the command. Let me do that properly.
Assistant
Now I can see the relevant components. Let me explore the dialog, alert-dialog, and popover components to understand the focus management logic.
Assistant
Let me explore the dialog store directory since focus management is often handled through a store. Also, let me look at the root component since that likely manages the overall state.
Assistant
Let me look at the DialogRoot store and the dialog root implementation to understand how focus is managed when dialogs are closed.
Assistant
Let me look at the useDialogRoot hook which handles the logic:
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/dialog/root/useDialogRoot.ts </tool_call>
Assistant
Let me properly execute the cat command to view the useDialogRoot.ts file.
Assistant
Now let me look at the store to understand how focus is managed when dialogs are closed. The issue mentions that when closing a dialog opened programmatically, focus should return to the programmatic trigger.
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/dialog/store/DialogStore.ts </tool_call>
Assistant
Let me run the bash command to see the DialogStore.ts file.
Assistant
The issue is about focus management when dialogs are closed. Looking at the code, I see in the
setOpenmethod in DialogStore.ts:if (!nextOpen && eventDetails.trigger == null && this.state.activeTriggerId != null) { // When closing the dialog, pass the old trigger to the onOpenChange event // so it's not reset too early (potentially causing focus issues in controlled scenarios). eventDetails.trigger = this.state.activeTriggerElement ?? undefined; }This code tries to pass the old trigger when closing, but there's still an issue with focus management. Let me look at the popups utility to understand how triggers work and how focus is managed.
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/utils/popups/index.ts </tool_call>
Assistant
I need to run the command properly to see the popups index file.
Assistant
Let me look at the useTriggerFocusGuards.ts file since it sounds related to focus management.
Assistant
Now let me look at the popupTriggerMap.ts and popupStoreUtils.ts to understand how triggers are managed and how focus is returned:
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/utils/popups/popupTriggerMap.ts </tool_call>
Assistant
The command wasn't executed properly. Let me try again.
Assistant
Now let me look at popupStoreUtils.ts which handles the trigger state management:
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/utils/popups/popupStoreUtils.ts </tool_call>
Assistant
Let me run the bash command to see the popupStoreUtils.ts file.
Assistant
Now I need to understand how focus is handled when dialogs close. Let me look at the floating-ui-react to see how the dismiss utility handles focus. The
dismissfunction is used to manage reference (trigger) and trigger props. Let me search for how the focus is returned when closing a dialog.<tool_call> <function=bash_terminal> <parameter=command> ls -la /base-ui/packages/react/src/floating-ui-react </tool_call>
Assistant
Let me run the command to explore the floating-ui-react directory.
Assistant
Let me look for the dismiss function:
<tool_call> <function=bash_terminal> <parameter=command> ls -la /base-ui/packages/react/src/floating-ui-react/hooks </tool_call>
Assistant
Let me run the bash command properly to list the hooks directory.
Assistant
Let me look at the useDismiss.ts file to understand how focus is managed when dialogs close.
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/floating-ui-react/hooks/useDismiss.ts </tool_call>
Assistant
The command is being executed properly now. Let me see the content of useDismiss.ts.
Assistant
The file is quite long. Let me focus on the key part - how the close is called and whether it passes the trigger. I noticed that in
closeOnEscapeKeyDown, it callsstore.setOpen(false, eventDetails)without passing the trigger. Let me look at the createChangeEventDetails to understand the eventDetails structure.Let me also look at the createBaseUIEventDetails file to understand how event details work:
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/internals/createBaseUIEventDetails.ts </tool_call>
Assistant
Let me run the bash command properly to see the createBaseUIEventDetails file.
Assistant
Now I understand the event details structure. The
createChangeEventDetailsfunction accepts atriggerparameter.Looking back at the DialogStore.ts
setOpenmethod:if (!nextOpen && eventDetails.trigger == null && this.state.activeTriggerId != null) { // When closing the dialog, pass the old trigger to the onOpenChange event // so it's not reset too early (potentially causing focus issues in controlled scenarios). eventDetails.trigger = this.state.activeTriggerElement ?? undefined; }This code preserves the old trigger when closing the dialog if no trigger is provided. However, the issue is that when the dialog is opened programmatically,
activeTriggerIdmight be set to that programmatic trigger, and when it's closed (e.g., by pressing Escape), theeventDetails.triggerisundefinedbecause no event was associated with the close.Assistant
The command wasn't executed properly. Let me try again.
Assistant
Let me continue exploring to find where the focus is managed when the dialog closes. I need to understand how the trigger props work and how focus is returned to the trigger.
<tool_call> <function=bash_terminal> <parameter=command> ls /base-ui/packages/react/src/floating-ui-react/components </tool_call>
Assistant
Let me run the bash command properly to see the floating-ui-react components.
<tool_call> <function=bash_terminal> <parameter=command> ls /base-ui/packages/react/src/floating-ui-react/components </tool_call>
Assistant
The command isn't executing. Let me try a different approach - using the bash_terminal tool directly.
Assistant
Now I can see that the output is going through properly. Let me look at the FloatingFocusManager which is likely responsible for focus management when dialogs close.
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/floating-ui-react/components/FloatingFocusManager.tsx </tool_call>
Assistant
The command is not being executed properly. Let me try a simpler command to see if the tool is working.
Assistant
Now I understand the FloatingFocusManager better. The key part is the
getReturnElement()function that decides where to return focus when the popup closes. The issue is that when the dialog is opened programmatically and then closed, the focus should return to the programmatic trigger button, but it seems like it's returning to a different button.Let me search for how the trigger props are handled. Looking at the code, I see that
domReferenceis used to determine where to return focus. ThedomReferencecomes from the context'sreferenceElement.Let me look at how the DialogRoot uses the FloatingFocusManager and how the reference element is set.
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/dialog/popup/DialogPopup.tsx </tool_call>
Assistant
The command is being executed. Let me continue to view the DialogPopup file.
Assistant
The key is the
FloatingFocusManagercomponent. It receivescontext,modal,closeOnFocusOut,initialFocus,returnFocus, andrestoreFocusprops. The focus is returned based on thereturnFocusprop, which by default istrueand returns focus todomReference.The issue is that
domReferencecomes from the context'sreferenceElement, which is set by thetriggerelement. Let me look at how the trigger props are applied. Let me search for howactiveTriggerPropsandinactiveTriggerPropsare used.<tool_call> <function=bash_terminal> <parameter=command> grep -r "activeTriggerProps" /base-ui/packages/react/src/dialog --include=".ts" --include=".tsx" </tool_call>
Assistant
The grep command should be executed properly. Let me wait for the output.
Assistant
Let me now look at how the trigger props work and how the reference element is set:
Assistant
The grep command returned exit code 1, which means it didn't find any matches. Let me look at the FloatingRootStore to understand how referenceElement is managed:
Assistant
I see that
domReferenceElementis set toreferenceElement. Let me look at how the trigger elements manage their registration and how the reference element is set in the FloatingRootContext.Let me look at the FloatingFocusManager again to understand where
domReferencecomes from:Assistant
Now I understand the structure. The
domReferenceElementcomes fromstore.useState('domReferenceElement'), which comes from the FloatingRootStore.Let me look at how the Dialog trigger works and how the referenceElement is set when using the "Open programmatically" button.
Let me search for how triggers register themselves:
Assistant
Let me check the dialog/trigger directory to see what files are there:
Assistant
Let me look at the DialogTrigger file to understand how triggers work.
Assistant
I see that the DialogTrigger uses
useTriggerDataForwardingto register itself. This function updates the store with the trigger element.Now let me understand how the focus is managed. Looking at the code in
FloatingRootStore.ts:constructor(options: FloatingRootStoreOptions) { const { syncOnly, nested, onOpenChange, triggerElements, ...initialState } = options; super( { ...initialState, positionReference: initialState.referenceElement, domReferenceElement: initialState.referenceElement as Element | null, }, ... ); ... }The
domReferenceElementis set frominitialState.referenceElement. But how does this get updated when triggers register?Let me look at the FloatingRootContext to see how the reference element is managed:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "referenceElement" /base-ui/packages/react/src/floating-ui-react/hooks/useSyncedFloatingRootContext.ts </tool_call>
Assistant
Let me wait for the grep output to come through properly.
Assistant
Let me now look at the useSyncedFloatingRootContext.ts file to understand how the referenceElement is synced:
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/floating-ui-react/hooks/useSyncedFloatingRootContext.ts </tool_call>
Assistant
The grep command is being executed. Let me now view the useSyncedFloatingRootContext.ts file to understand how the referenceElement is synced.
Assistant
Now I understand the flow. The
referenceElementis synced frompopupStore.useState('activeTriggerElement'), which is the trigger element that opened the popup.The issue is that when the dialog is opened programmatically using a button, the trigger is registered and becomes the
activeTriggerElement. But when the dialog closes (e.g., by pressing Escape), the focus should return to theactiveTriggerElement, not to some other button.Let me now look at how the DialogPopup uses to understand the focus return logic:
Assistant
The command is being executed. Let me continue to look at the test file.
Assistant
I need to search for the
DialogRoot.detached-triggers.test.tsxfile since it's specifically testing the controlled mode with multiple triggers scenario:<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/dialog/root/DialogRoot.detached-triggers.test.tsx </tool_call>
Assistant
The command is being executed. Let me wait for the output to see the test file content.
Assistant
Let me continue exploring the code to understand the focus management issue. I'll first look at the Popover and Alert Dialog components to see how they handle focus.
Assistant
Now I have a good understanding of the code structure. Let me look at the alert-dialog component as well to see the structure:
<tool_call> <function=bash_terminal> <parameter=command> ls /base-ui/packages/react/src/alert-dialog </tool_call>
Assistant
The command is being executed. Let me wait for the output.
Assistant
Let me look at the alert-dialog root to understand the structure:
<tool_call> <function=bash_terminal> <parameter=command> ls /base-ui/packages/react/src/alert-dialog/root/ </tool_call>
Assistant
The command is still being executed. Let me wait for the output.
Assistant
Assistant
The command is being executed. Let me wait for the output.
Assistant
Let me look at the Popover handle to see if there's a similar pattern:
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/popover/store/PopoverHandle.ts </tool_call>
Assistant
The command is being executed. Let me wait for the output.
Assistant
Assistant
Let me run the tests to see the current state:
Assistant
Let me just implement the fix based on my analysis. The issue is that
forceUnmountclearsactiveTriggerElementandactiveTriggerIdbefore the cleanup has a chance to return the focus. The fix is to not clear these values in .Assistant
The command is still being executed. Let me wait for the output.
Assistant
Now I can see the exact code. The issue is on lines 267-268 where
activeTriggerIdandactiveTriggerElementare cleared. This happens before theFloatingFocusManagercleanup has a chance to return the focus.Let me implement the fix by removing the clearing of
activeTriggerIdandactiveTriggerElementfromforceUnmount.<tool_call> <function=bash_terminal> <parameter=command> cat -n /base-ui/packages/react/src/utils/popups/popupStoreUtils.ts | head -290 | tail -40 </tool_call>
Assistant
Let me run the bash command to see the exact lines around the forceUnmount function.
Assistant
Now I can see the exact code. The issue is that
activeTriggerIdandactiveTriggerElementare set tonullin theforceUnmountfunction (lines 267-268). This happens before theFloatingFocusManagercleanup has a chance to return the focus.The fix is to remove the clearing of
activeTriggerIdandactiveTriggerElementfromforceUnmount. However, I need to be careful because these values should be cleared eventually to avoid memory leaks or stale data.Looking at the code, the
activeTriggerIdandactiveTriggerElementare updated when a new trigger is used to open a new popup (viasetOpenTriggerState). So the stale values will be overwritten when a new trigger is used.But if no new trigger is used, the stale values will remain in the store. This might cause issues if the application state is reset.
Assistant
The command is being executed. Let me wait for the output and then implement the fix.
Assistant
Now I'll implement the fix by removing the clearing of
activeTriggerIdandactiveTriggerElementfrom theforceUnmountfunction. This will preserve the trigger element reference until after theFloatingFocusManagercleanup has a chance to return focus to it.Assistant
Now let me comment out the
activeTriggerElement: nullline as well.Assistant
Let me verify the changes were applied correctly:
Assistant
Assistant
Let me wait for the TypeScript check to complete:
Assistant
The TypeScript errors are related to test files not being built, not to my changes. Let me check if there are any errors specifically related to the file I modified.
<tool_call> <function=bash_terminal> <parameter=command> cd /base-ui && npx tsc --noEmit -p packages/react/tsconfig.json 2>&1 | grep -i "popupStoreUtils" | head -20 </tool_call>
Assistant
The command is being executed. Let me wait for the output to check if there are any TypeScript errors related to my changes.
Assistant
Assistant
The command is still running. Let me wait for the output.
Assistant
The changes look correct. Now let me also check if there are any other components (like Popover or Alert Dialog) that might need similar fixes. Let me also verify that the
DialogStore.setOpenmethod properly preserves the trigger when closing.Assistant
The
DialogStore.setOpenmethod properly preserves the trigger when closing. Now let me check the Popover store as well to make sure it has the same logic.Assistant
Let me also check the Alert Dialog store to make sure it has the same logic:
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/alert-dialog/handle.ts </tool_call>
Assistant
The command is still being executed. Let me wait for the output.
<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/alert-dialog/handle.ts </tool_call>
Assistant
Let me check if Alert Dialog uses the same store as Dialog or if it has its own:
Assistant
Assistant
Now let me also check if there are any similar issues in the Popover store. Looking at the Popover store, I see that it uses the same
setOpenTriggerStatefunction frompopupStoreUtils.ts. So my fix should apply to Popover as well.But let me also check if the Popover store has any additional logic that might affect the focus management.
Looking at the
PopoverStore.setOpenmethod, I see that it has special handling forclosePressevents:if ( !nextOpen && eventDetails.reason === REASONS.closePress && eventDetails.trigger == null && activeTriggerId != null ) { eventDetails.trigger = this.context.triggerElements.getById(activeTriggerId) ?? this.select('activeTriggerElement') ?? undefined; }Assistant
Let me search for focus-related tests in the Dialog component:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "focus" /base-ui/packages/react/src/dialog/root/*.test.tsx | head -30 </tool_call>
Assistant
The command is still running. Let me wait for the output to see if there are any focus-related tests in the Dialog component.
Assistant
The command is still running. Let me wait for the output.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "focus" /base-ui/packages/react/src/dialog/root/*.test.tsx | head -30 </tool_call>
Assistant
I need to run the command properly to check for focus-related tests. Let me run a simpler command to search for focus tests.
Assistant
I see that there are some focus-related tests, but none of them specifically test the scenario described in the issue (opening a dialog programmatically and then closing it to return focus to the programmatic trigger).
Let me check if there's a test for the scenario in the detached-triggers test file:
<tool_call> <function=bash_terminal> <parameter=command> grep -A 50 "open programmatically" /base-ui/packages/react/src/dialog/root/DialogRoot.detached-triggers.test.tsx | head -60 </tool_call>
Assistant
Let me search for the test that tests opening a dialog programmatically to understand the expected behavior.
Assistant
The command didn't find any matches because it's case-sensitive or the text is different. Let me search more broadly:
<tool_call> <function=bash_terminal> <parameter=command> grep -n -i "programmatic" /base-ui/packages/react/src/dialog/root/DialogRoot.detached-triggers.test.tsx | head -20 </tool_call>
Assistant
I need to search for the test that tests opening a dialog programmatically. Let me run a more comprehensive search.
Assistant
I found the test at line 179. Let me look at it to understand the scenario:
Assistant
Assistant
Let me check the git status to see the current state of my changes.
Assistant
Let me verify the final state of the change before submitting:
Assistant
Now let me think about whether my fix is complete. The issue is about focus management when dialogs are closed after being opened programmatically. My fix removes the clearing of
activeTriggerIdandactiveTriggerElementfrom theforceUnmountfunction, which preserves these values until the popup unmounts. This allows theFloatingFocusManagerto have access to the trigger element when returning focus.However, I should also consider if there are any edge cases or potential issues with this fix:
-
Memory leaks: If
activeTriggerElementis not cleared, it might cause a memory leak if the trigger element is not garbage collected. However, the trigger element is already referenced elsewhere in the DOM, so it should be garbage collected when the trigger is unmounted. -
: If the is not cleared, it might cause stale values in the store. However, the is updated when a new trigger is used to open a new popup, so stale values should be overwritten.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.