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 understand the issue:
-
The issue is about accessibility - when using "Open Programmatically" buttons in Alert Dialog, Dialog, and Popover components, focus moves to the wrong trigger after closing the dialog.
-
The expected behavior is that focus should return to the control that opened the dialog, not a different button.
Let me start by exploring the repository structure to understand how it's organized.
-
Assistant
Let me explore the packages directory structure to find the relevant components.
Assistant
Let me explore the alert-dialog, dialog, and popover components to understand how focus is managed:
Assistant
The AlertDialog uses
useRenderDialogRoot. Let me look at the Dialog component:Assistant
Let me read the key files for understanding focus management:
Assistant
Now let me look at the popup utilities that handle the focus:
Assistant
Let me look at the popup store utilities, especially focusing on focus restoration:
Assistant
Now let me look at the store to understand how triggers and focus are managed:
Assistant
Now let me look at the floating-ui-react library, which handles the focus management:
Assistant
Let me look at the useDismiss hook since that's where focus restoration typically happens:
Assistant
Now let me look at where focus restoration happens when the popup closes. Let me look at the floating root context and how focus is managed:
Assistant
Let me look at the FloatingFocusManager component:
Assistant
This is very helpful. The key code is in
getReturnElement():if (typeof resolvedReturnFocusValue === 'boolean') { if (domReference?.isConnected) { return domReference; } return getPreviouslyFocusedElement() || null; }So focus returns to
domReference. Let me see howdomReferenceis set:Assistant
Now I see that
domReferenceElementis set fromreferenceElement. Let me check how thereferenceElementis synced. Let me look atuseSyncedFloatingRootContext:Assistant
Now I understand the flow. The
referenceElementcomes frompopupStore.useState('activeTriggerElement'). This is theactiveTriggerElementin the popup store. Now I need to understand how theactiveTriggerElementis set when the dialog is opened programmatically.Let me look at the handle and how "Open Programmatically" buttons work:
Assistant
Now I can see the issue! When
open(triggerId)is called, it gets the trigger element and passes it as thetriggerparameter increateChangeEventDetails. But when focus restoration happens, it usesdomReferenceElementwhich is synced fromactiveTriggerElementin the store.Let me check the dialog store to see how the
activeTriggerElementis updated:Assistant
Now I see the issue! Let me trace the flow:
-
When
handle.open(triggerId)is called, it:- Gets the
triggerElementfromthis.store.context.triggerElements.getById(triggerId) - Passes this element as
eventDetails.triggertosetOpen
- Gets the
-
In
setOpen, it callssetOpenTriggerState(updatedState, nextOpen, eventDetails.trigger) -
Looking at
setOpenTriggerStateinpopupStoreUtils.ts:export function setOpenTriggerState( state: Partial<PopupStoreState<unknown>>, open: boolean, trigger: Element | undefined, ) { const triggerId = trigger?.id ?? null; // If a popup is closing, the `trigger` may be undefined. // We want to keep the previous value so that exit animations are played and focus is returned correctly. if (triggerId || open) { state.activeTriggerId = triggerId; state.activeTriggerElement = trigger ?? null; } }
So when opening with
handle.open(triggerId), theactiveTriggerIdshould be set to the trigger'sid. But the trigger might not have an attribute set directly...-
Assistant
`
Assistant
Let me look at the detached triggers test file to understand the expected behavior:
Assistant
Let me continue reading the test file to understand how the focus behavior is tested.
Assistant
There's only one test with focus in the detached triggers test file. Let me search for focus tests in the main DialogRoot test file and also look for how focus restoration is tested.
Assistant
Let me search more specifically for tests related to focus returning to triggers or programmatically opened dialogs.
Assistant
The grep didn't find anything with "programmatically". Let me search for the handle tests or focus return tests.
Assistant
Let me search for focus-related tests with detached triggers. I also want to look at how the
open()method works with trigger IDs and how the active trigger is tracked.The issue is: when using "Open Programmatically" button (which calls
handle.open(triggerId)), focus should return to the button that opened the dialog, not a different trigger.Let me look at the
useSyncedFloatingRootContextmore carefully - thereferenceElementcomes fromactiveTriggerElementin the popup store. When the handle'sopen()method is called, it should set theactiveTriggerElement.Let me trace this more carefully by looking at how the trigger ID lookup works.
Assistant
Let me look at
useImplicitActiveTriggermore closely - it seems like it might be overwriting the trigger that was set programmatically:Assistant
Let me look at the docs examples for the "Controlled mode with multiple triggers" pattern:
Assistant
Assistant
I need to properly call the bash_terminal function to search for focus-related tests.
Assistant
Let me look at the test case that checks focus return behavior with detached triggers to understand the expected behavior better.
Assistant
Let me look at the detached triggers test file more to see if there are any focus tests with multiple triggers:
Assistant
Let me create a test script to reproduce the issue first.
Assistant
Let me look at the FloatingFocusManager test to understand how focus return works, and then create a reproduction test for the issue.
Assistant
Let me understand the structure better and look at the actual test utilities:
Assistant
Let me continue exploring the codebase to understand how the focus restoration flow works:
Assistant
Let me trace through the code to understand the issue better. I need to understand how focus is restored when a dialog closes. Let me look at the key areas:
- When a dialog opens programmatically via
handle.open(triggerId)or via controlled mode withtriggerIdprop - What element is tracked as the "active trigger"
- What element focus returns to when the dialog closes
Let me look at the actual flow more carefully.
- When a dialog opens programmatically via
Assistant
Let me trace through the code more carefully to understand what happens when focus is restored. The key is the
domReferencevariable inFloatingFocusManager.tsx. Let me look at how it's used when returning focus.Assistant
Assistant
I need to use the bash_terminal command to read the file content.
Assistant
Let me look at how the DialogRoot syncs its props to the store:
Assistant
Let me look at
useRenderDialogRootto understand how the props are synced to the store.Assistant
Now I understand the issue! Let me trace through the code flow to confirm my understanding:
- When the "Open programmatically" button is clicked, it sets
triggerIdandopenstate - The
triggerIdprop syncs totriggerIdPropin the store - The
activeTriggerIdselector returnstriggerIdProp ?? activeTriggerId - When
triggerIdPropmatches a trigger's ID, that trigger'suseTriggerDataForwardingeffect runs and setsactiveTriggerElement activeTriggerElementgets synced todomReferenceElement- When the dialog closes, focus returns to
domReference(the wrong element)
The issue is that
triggerIdPropis meant for ARIA purposes, but it's also being used to setactiveTriggerElementwhich controls focus restoration.Let me look at the
useTriggerDataForwardingfunction more carefully:- When the "Open programmatically" button is clicked, it sets
Assistant
Now I understand the issue. Let me trace through the code:
- When "Open programmatically" is clicked,
triggerIdis set to 'alert-trigger-2' andopenis set totrue triggerIdsyncs totriggerIdPropin the store- The
activeTriggerIdselector returnstriggerIdProp ?? state.activeTriggerId, so it returns 'alert-trigger-2' - The "Delete" trigger's
isMountedByTriggerbecomes true (becauseactiveTriggerIdSelector === triggerId) - The
useIsoLayoutEffectinuseTriggerDataForwardingsetsactiveTriggerElementto the "Delete" button - When the dialog closes, focus returns to the "Delete" button via
domReferenceElement
The problem is that
isMountedByTriggerusesactiveTriggerIdSelector, which includestriggerIdProp. So whentriggerIdPropis set (for ARIA purposes), it also setsactiveTriggerElement(for focus management).The fix is to separate ARIA purposes from focus management:
isMountedByTriggershould only use (actual trigger that opened the dialog)
- When "Open programmatically" is clicked,
Assistant
The current shell prompt shows we're in the repository root. Let me continue with my analysis and fix the issue.
The issue is now clear:
- When
triggerIdis set programmatically (for ARIA purposes), it causesactiveTriggerElementto be set to that trigger's element - This means focus returns to that trigger instead of the element that actually opened the dialog
The fix should be to separate ARIA tracking from focus tracking. Let me look at the selectors more carefully and implement the fix.
- When
Assistant
Now I have a clear understanding of the issue. Let me trace through what happens:
- When
triggerIdis set programmatically (for ARIA purposes),activeTriggerIdSelectorreturnstriggerIdPropinstead ofstate.activeTriggerId - This causes
isMountedByTriggerto be true for that trigger - Which causes
activeTriggerElementto be set to that trigger's element - Focus then returns to that trigger when the dialog closes (instead of the element that actually opened it)
The fix is to separate ARIA tracking from focus tracking.
isMountedByTriggershould only usestate.activeTriggerId(actual trigger that opened the dialog), notactiveTriggerIdSelector(which includestriggerIdPropfor ARIA purposes).Let me implement the fix:
- When
Assistant
Now I fully understand the issue. The problem is that
isMountedByTriggerusesactiveTriggerIdSelectorwhich returnsstate.triggerIdProp ?? state.activeTriggerId. WhentriggerIdPropis set programmatically (for ARIA purposes), it also affects focus management because triggers see themselves as "mounted" and setactiveTriggerElement.The fix is to separate ARIA tracking from focus tracking:
isMountedByTriggershould only checkstate.activeTriggerId(nottriggerIdProp)registerTriggerinuseTriggerDataForwardingshould also only checkstate.activeTriggerId
Let me implement this fix:
Assistant
I need to fix the
isMountedByTriggerselector to only usestate.activeTriggerId(for actual focus tracking) instead ofactiveTriggerIdSelector(which includestriggerIdPropfor ARIA purposes). The key insight is thattriggerIdPropis only for ARIA relationships (aria-expanded, aria-controls), not for focus management.Assistant
Let me first open the file, then make the edit.
Assistant
I need to use the edit command with the --file option instead.
Assistant
I understand the issue now. The problem is that
isMountedByTriggerusesactiveTriggerIdSelectorwhich returnsstate.triggerIdProp ?? state.activeTriggerId. WhentriggerIdis set programmatically (for ARIA purposes), it causes the trigger to think it's the one that mounted the popup, which then setsactiveTriggerElementincorrectly for focus management.I need to fix two things:
- The
isMountedByTriggerselector should only checkstate.activeTriggerId(nottriggerIdProp) - The
registerTriggercallback inuseTriggerDataForwardingshould also only checkstate.activeTriggerId
Let me also check what
store.select('activeTriggerId')returns inuseTriggerDataForwarding:- The
Assistant
Now I need to understand the full picture. The
activeTriggerIdselector in the store returnsstate.triggerIdProp ?? state.activeTriggerId. This means:- When a trigger is clicked,
state.activeTriggerIdis set to the trigger's ID - When
triggerIdprop is set programmatically,state.triggerIdPropis set - The selector prefers
triggerIdPropoveractiveTriggerId
The problem is that when
triggerIdPropis set (for ARIA), it affects focus management.Let me implement the fix:
- Create a separate selector that only uses
state.activeTriggerId(for focus management) - Use this new selector in
isMountedByTrigger - Also need to update
useTriggerDataForwardingto use the internalactiveTriggerIdinstead of the selector
Let me first look at the exact changes needed:
- When a trigger is clicked,
Assistant
I now understand the issue. The
store.select('activeTriggerId')returns the selector which includestriggerIdProp. This means when a trigger registers whiletriggerIdPropis set (but not for focus management), it incorrectly updatesactiveTriggerElement.The fix needs to:
- Add a selector that only uses
state.activeTriggerId(nottriggerIdProp) for focus management - Use this new selector in
isMountedByTrigger - Update
useTriggerDataForwardingto use the internal state directly for focus management
Let me implement this fix:
- Add a selector that only uses
Assistant
Let me verify the change was applied correctly:
Assistant
The change was applied. Now I need to:
- Export
internalActiveTriggerIdSelectorso it can be used inuseTriggerDataForwarding - Update
useTriggerDataForwardingto use the internal selector instead ofstore.select('activeTriggerId') - Export
isMountedByTriggerForFocusSelectorand use it where needed
Let me first check the current state of the file:
- Export
Assistant
Let me see the current state of the file to make sure the changes are correct. I need to fix the placement of the new selectors and also update the exports and usage.
Assistant
The file got corrupted. Let me restore it and make the changes properly:
Assistant
Let me now make the proper fix. I need to:
- Add a new internal selector that only uses
state.activeTriggerIdfor focus management - Update
isMountedByTriggerto use the internal selector for focus management - Update
useTriggerDataForwardingto use the internal state directly
Let me think about this more carefully. The issue is:
activeTriggerIdSelectorreturnsstate.triggerIdProp ?? state.activeTriggerId(for ARIA purposes)isMountedByTriggerusesactiveTriggerIdSelectorwhich includestriggerIdProp- When
triggerIdprop is set programmatically, it causes triggers to think they're the active one - This sets
activeTriggerElementincorrectly
The fix should be:
- Create a new selector
internalActiveTriggerIdthat only usesstate.activeTriggerId - Use this new selector for focus management (in
isMountedByTriggeranduseTriggerDataForwarding) - Keep
activeTriggerIdSelectorfor ARIA purposes
- Add a new internal selector that only uses
Assistant
Now let me think more carefully about the fix. The issue is:
isMountedByTriggeris used byuseTriggerDataForwardingto determine if a trigger should setactiveTriggerElementisMountedByTriggerusesactiveTriggerIdSelectorwhich includestriggerIdPropfor ARIA purposes- When
triggerIdprop is set programmatically, triggers think they're active and setactiveTriggerElementincorrectly
The fix should be to separate the concerns:
isTriggerActive(for ARIA - aria-expanded, aria-controls): useactiveTriggerIdSelector(includestriggerIdProp)isMountedByTrigger(for focus management - activeTriggerElement): should only usestate.activeTriggerId
But wait, I need to think about backward compatibility. External code might be using
isMountedByTrigger. Let me check if it's exported...Looking at the selectors,
isMountedByTriggeris part ofpopupStoreSelectorswhich is exported. But is it used externally?Let me search for usage of
isMountedByTrigger:Assistant
Let me understand what
isMountedByTriggeris used for:- It's defined in
store.tsline 205 - It's used in
popupStoreUtils.tsline 148
Now let me check how it's used in
popupStoreUtils.ts:- It's defined in
Assistant
I see the issue now. Both
isMountedByThisTriggerandregisterTriggeruse theactiveTriggerIdSelectorwhich includestriggerIdProp. This causes the problem:- When
triggerIdprop is set programmatically (for ARIA purposes),activeTriggerIdSelectorreturns that value - The
registerTriggercallback checks ifactiveTriggerId === triggerIdand if true, setsactiveTriggerElement - The
isMountedByThisTriggeralso becomes true whentriggerIdprop is set - This causes the wrong element to be set as
activeTriggerElement
The fix should:
- Update
isMountedByTriggerto only usestate.activeTriggerId(nottriggerIdProp) - Update
registerTriggerto usestate.activeTriggerIddirectly (not through the selector)
Let me implement this fix properly:
- When
Assistant
Now let me make the fix. I need to:
- Change
isMountedByTriggerto usestate.activeTriggerIddirectly instead ofactiveTriggerIdSelector(state) - Update
useTriggerDataForwardingto checkstate.activeTriggerIddirectly instead of usingstore.select('activeTriggerId')
Let me start with the fix to
store.ts:- Change
Assistant
Let me properly use the replace command to fix the
isMountedByTriggerselector in store.ts.Assistant
I need to use the proper heredoc syntax for the replace command. Let me fix the
isMountedByTriggerselector to usestate.activeTriggerIddirectly instead ofactiveTriggerIdSelector(state).Assistant
Let me navigate to line 202 to see the exact content of the file at that location.
Assistant
I need to make a tool call. Let me use the replace command properly to fix the
isMountedByTriggerselector.Assistant
The issue is that the replace command requires the text to be in the "displayed lines" which seems to be limited. Let me scroll down to get the relevant lines into view first.
Assistant
The scroll_down command didn't work. Let me try using a different approach - I'll use sed to make the replacement directly on the file.
Assistant
The sed command was executed successfully. Let me verify that the change was applied correctly by checking the file content.
Assistant
The grep only shows one line, let me check more context around that line to verify the change was applied correctly.
Assistant
The change to
store.tswas applied successfully. Now I need to also updatepopupStoreUtils.tsto usestate.activeTriggerIddirectly instead ofstore.select('activeTriggerId')which uses the selector that includestriggerIdProp.Let me check the relevant code in
popupStoreUtils.ts.Assistant
I need to make a tool call. Let me check the popupStoreUtils.ts file to see if I need to make additional changes there.
Assistant
I need to fix two places in
popupStoreUtils.tswherestore.select('activeTriggerId')is used. These should usestore.state.activeTriggerIddirectly to avoid thetriggerIdPropselector affecting focus management.Assistant
I need to fix the
popupStoreUtils.tsfile. There are two places wherestore.select('activeTriggerId')is used, and both should usestore.state.activeTriggerIddirectly instead of the selector, since the selector includestriggerIdPropwhich is meant for ARIA purposes, not focus management.Let me use sed to make these changes.
Assistant
Now let me fix the second occurrence at line 229.
Assistant
I need to make a tool call. Let me fix the second occurrence in popupStoreUtils.ts at line 229.
Assistant
The bash history expansion is interfering with the
!character. Let me use single quotes instead to avoid this issue.Assistant
I need to verify the changes were applied correctly to popupStoreUtils.ts. Let me check the file content to confirm both replacements were made.
Assistant
Both changes were applied successfully. Now let me verify there are no other places that need similar fixes and then run the tests to ensure the changes work correctly.
Assistant
There's one more occurrence in
MenuSubmenuTrigger.tsx. Let me check if this needs to be fixed as well.Assistant
This code in
MenuSubmenuTrigger.tsxis settingactiveTriggerIdandactiveTriggerElementwhen the menu is open but no active trigger has been established yet. This is the same pattern as inuseTriggerDataForwarding. However, looking at the logic:- It checks if
store.select('activeTriggerId') == null(meaning no active trigger is set) - If so, it sets the submenu trigger as the active trigger
In this case, using
store.select('activeTriggerId')(which includestriggerIdProp) might actually be intentional - if atriggerIdprop is set, we should use that for ARIA purposes. But for focus management, we should only setactiveTriggerId(the internal state) if there's no other active trigger.Let me think about this more carefully:
- The
MenuSubmenuTriggeris settingactiveTriggerId(internal state), not
- It checks if
Assistant
Let me fix this occurrence too.
Assistant
I need to verify the change was applied correctly to MenuSubmenuTrigger.tsx.
Assistant
The grep returned exit code 1, which means the pattern was not found - this confirms the change was applied successfully. Let me verify by checking the actual content of the file at that location.
Assistant
The change was applied successfully. Now let me verify all changes are correct by checking for any remaining occurrences of the pattern that should be fixed.
Assistant
Good - there are no more occurrences of
store.select('activeTriggerId')in the codebase. All the changes have been applied successfully.Let me now summarize the changes made and verify they are correct:
-
store.ts: ChangedisMountedByTriggerto usestate.activeTriggerIddirectly instead ofactiveTriggerIdSelector(state)which includestriggerIdProp -
popupStoreUtils.ts: Changed two occurrences to usestore.state.activeTriggerIddirectly instead ofstore.select('activeTriggerId') -
MenuSubmenuTrigger.tsx: Changed to usestore.state.activeTriggerIddirectly instead ofstore.select('activeTriggerId')
Now let me run the existing tests to make sure the changes don't break anything.
-
Assistant
The test command timed out. Let me try a different approach - I'll check if there are any specific tests related to the changes I made and run them with a shorter timeout, or just verify the changes are syntactically correct first.
Assistant
The test command is taking too long. Let me try a simpler approach - I'll check if the TypeScript compilation passes first, then try running a specific test file.
Assistant
The TypeScript errors are about build artifacts, not about my changes. Let me verify my changes are syntactically correct by checking the specific files I modified.
Assistant
The changes to
store.tslook correct. Let me verify the other files and then run a quick test to validate the fix.Assistant
Let me view the actual diff to confirm the changes are correct.
Assistant
The changes look correct. Let me summarize what was changed:
Summary of Changes
The issue was that when using "Open Programmatically" buttons in Alert Dialog, Dialog, and Popover components, focus was returning to the wrong trigger after closing the dialog. This happened because:
- The
activeTriggerIdselector was returningstate.triggerIdProp ?? state.activeTriggerId - When
triggerIdprop was set programmatically (for ARIA purposes), it would affect focus management by settingactiveTriggerElementto the wrong element - Focus restoration uses
activeTriggerElementto determine where to return focus
Files Modified
-
packages/react/src/utils/popups/store.ts: ChangedisMountedByTriggerselector to usestate.activeTriggerIddirectly instead ofactiveTriggerIdSelector(state)which includestriggerIdProp -
: Changed two locations to use directly instead of
- The
Assistant
I've completed the fix for the accessibility issue where focus moves to the wrong trigger when using "Open programmatically" buttons. Let me submit the changes now.
Assistant
Let me try the submit command again properly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.