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 analyze this issue. The problem is about accessibility in the Base UI library - when using "Open programmatically" buttons to open Alert Dialog, Dialog, or Popover components, focus returns to the wrong trigger after closing the dialog.
Let me start by exploring the repository structure to understand how it's organized and find the relevant files.
Assistant
Let me explore the packages directory to find the Alert Dialog, Dialog, and Popover components.
Assistant
Let me explore the packages directory to find the relevant components.
Assistant
Good. Let me explore the structure of these components and also look at the docs to understand how they implement the "controlled mode with multiple triggers" examples.
Assistant
The user is getting an error because I used the wrong format for the open command. Let me use the correct bash_terminal command format with the open command.
Assistant
Let me look at the
useRenderDialogRoothook, which seems to be the core implementation:Assistant
Let me look at the popups utilities and the DialogStore, since that's where focus management is likely happening:
Assistant
Now let me look at the store to understand how focus is managed:
Assistant
Now let me look at the focus management. Let me search for focus-related code in the popup utilities:
Assistant
Now let me look at the Dialog store to understand how focus restoration is handled:
Assistant
Now let me look at the Dialog trigger to see how triggers work:
Assistant
...
Assistant
Now let me look at how focus restoration is handled when the dialog closes. Let me search for focus-related code:
Assistant
The
FloatingFocusManager.tsxlooks like the key file for focus management. Let me look at it:Assistant
Let me see the middle part of the file that was truncated:
Assistant
Now I understand the focus management. The key is in
getReturnElement()which returns thedomReferenceelement. Let me check how thedomReferenceElementis set in the floating context. The issue is that when using multiple triggers with a handle-based approach ("Open programmatically"), thedomReferenceElementmight not be the actual element that triggered the dialog.Let me look at how the FloatingRootContext works:
Assistant
Now I see the key part:
domReferenceElementis set inFloatingRootStoreand initialized toreferenceElement. Let me check how it gets updated:Assistant
Now let me look at
useSyncedFloatingRootContext.tsto see howdomReferenceElementis synced:Assistant
Now I see the key issue! The
domReferenceElementin theFloatingRootStoreis synced fromactiveTriggerElementfrom the popup store. Let's look at how theactiveTriggerElementis set and track where the bug might be.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 the dialog is opening (
open = true), theactiveTriggerElementis set to the trigger that opened it. But the issue is that when a dialog is opened programmatically (via a handle), theeventDetails.triggermight not be the correct element.Let me look at the docs example to understand the exact issue:
Assistant
...
Assistant
Let me look at one of these demo files to understand the pattern:
Assistant
Now I understand the issue. When clicking "Open programmatically", the code sets
triggerIdto'alert-trigger-2'(the "Delete" button), but the actual element that triggered the dialog is the "Open programmatically" button. When the dialog closes, focus should return to the "Open programmatically" button, but instead it returns to "alert-trigger-2" (the Delete button).The problem is:
- The "Open programmatically" button is a plain HTML button (not an
AlertDialog.Trigger) - It sets
triggerIdto'alert-trigger-2'to control which trigger is "active" for ARIA purposes - When the dialog closes, focus returns to the
activeTriggerElementwhich is the element registered for'alert-trigger-2'(the Delete button) - But the actual element that was focused before opening was the "Open programmatically" button
Let me look at how the focus return is handled:
- The "Open programmatically" button is a plain HTML button (not an
Assistant
Let me examine where the
activeTriggerElementgets updated whentriggerIdis changed programmatically:Assistant
Let me look at how
useControlledPropworks and how the trigger ID syncs with the active trigger element:Assistant
Answer switch (q)
Assistant
I made an error by not properly invoking a tool call. Let me continue investigating the issue by looking at the
useControlledPropimplementation and how theactiveTriggerElementgets updated when thetriggerIdPropchanges.Assistant
Now I understand the issue. Let me trace through the code to understand what happens:
- When "Open programmatically" button is clicked,
triggerIdPropis set to'alert-trigger-2'andopenis set totrue - The selector
activeTriggerIdSelectorreturnstriggerIdProp ?? activeTriggerId='alert-trigger-2' - In
useTriggerDataForwarding, when the "Delete" trigger (id='alert-trigger-2') registers, it seesactiveTriggerId === triggerIdis true, so it setsactiveTriggerElementto the Delete button - When the dialog closes, focus returns to
domReferenceElementwhich is synced fromactiveTriggerElement(the Delete button)
The bug is that
useTriggerDataForwardingusesstore.select('activeTriggerId')which includestriggerIdProp, butactiveTriggerElementshould only be set when a trigger is actually clicked.Let me look at the test files to understand the expected behavior:
- When "Open programmatically" button is clicked,
Assistant
Assistant
I made a tool call error - I need to use the bash_terminal function properly.
Assistant
Assistant
I need to use the bash_terminal function to run the grep command. Let me fix the tool call.
Assistant
Assistant
I keep forgetting to use the bash_terminal function properly. Let me make sure I use it correctly.
Assistant
Now I understand the issue clearly. The problem is in
useTriggerDataForwardinginpopupStoreUtils.ts- when a trigger's id matchestriggerIdProp(set programmatically), it incorrectly sets itself asactiveTriggerElement, causing focus to return to the wrong element.Let me implement the fix. First, I'll add a new selector that returns the raw
activeTriggerId(without consideringtriggerIdProp), then modifyuseTriggerDataForwardingto only setactiveTriggerElementwhen the trigger was actually clicked.Assistant
I'll add a new selector for the raw internal
activeTriggerIdto the store, then updateuseTriggerDataForwardingto only setactiveTriggerElementwhen the trigger was actually clicked.Assistant
I'll add a new selector
rawActiveTriggerIdthat returns the internalactiveTriggerIdwithout consideringtriggerIdProp.Assistant
Now I need to:
- Add a new selector
rawActiveTriggerIdthat returns juststate.activeTriggerId - Modify
useTriggerDataForwardingto checkrawActiveTriggerIdbefore settingactiveTriggerElement
Let me first add the new selector to
store.ts:- Add a new selector
Assistant
The user is continuing the conversation. Let me check what happened with my previous sed command and verify the changes were applied correctly.
Assistant
The sed command didn't work correctly - it inserted the new selector in the wrong place, breaking the
activeTriggerElementselector. Let me revert and fix this properly.Assistant
Let me revert that and make the changes more carefully using proper editing.
Assistant
Good, the file is reverted. Now let me make the changes more carefully using the edit command.
Assistant
I need to fix the issue where focus moves to the wrong trigger when using "Open programmatically" buttons. Let me add the new selector properly to
store.ts.Assistant
Let me verify the change was applied correctly this time.
Assistant
I'll continue implementing the fix. I've added the
rawActiveTriggerIdselector tostore.ts. Now I need to modifyuseTriggerDataForwardinginpopupStoreUtils.tsto only setactiveTriggerElementwhen the trigger was actually clicked.Assistant
Now I need to modify
useTriggerDataForwardingto only setactiveTriggerElementwhen the trigger was actually clicked. I'll check againstrawActiveTriggerIdinstead of the combinedactiveTriggerIdselector.Assistant
Now I need to modify
useTriggerDataForwardinginpopupStoreUtils.tsto checkrawActiveTriggerId(the actual clicked trigger) instead ofactiveTriggerId(which includestriggerIdProp) when deciding whether to setactiveTriggerElement.Assistant
I need to modify
useTriggerDataForwardingto userawActiveTriggerIdwhen deciding whether to setactiveTriggerElement. Let me make the changes:- In the
registerTriggercallback, checkrawActiveTriggerIdinstead ofactiveTriggerIdwhen deciding to setactiveTriggerElement - In the
useIsoLayoutEffect, also checkrawActiveTriggerId
- In the
Assistant
Python is not available. Let me use the edit command instead to make the changes.
Assistant
Let me make the changes using sed:
Assistant
Good, I added the
rawActiveTriggerIdline. Now I need to update the condition to check if the trigger was actually clicked before settingactiveTriggerElement:Assistant
I need to update the store.update call to only set
activeTriggerElementwhenwasTriggerClickedis true. Let me make that change:Assistant
I see that my sed command replaced all occurrences, including the one in the second
ifblock. The second block shouldn't usewasTriggerClickedbecause it's handling the case where no active trigger is established yet - in that case, the first registered trigger should claim the element. Let me fix this:Assistant
I need to fix the second block - it should always set
activeTriggerElementsince this is the case where no active trigger exists yet and the first trigger should claim itself:Assistant
Good, the second block is now correct. Now I need to also fix the
useIsoLayoutEffectblock that runs whenisMountedByThisTriggerchanges. Let me check that section:Assistant
I need to also fix the
useIsoLayoutEffectblock. I'll add a check forrawActiveTriggerIdbefore settingactiveTriggerElement:Assistant
I see I deleted too much. Let me restore the file and try again more carefully:
Assistant
Let me start fresh with a cleaner approach using the edit command with heredoc syntax:
Assistant
Let me make the changes more carefully using a single comprehensive edit:
Assistant
The user sent an empty message. Let me continue with the fix I was working on. I need to replace the relevant section in
popupStoreUtils.tswith my corrected code.Assistant
Let me verify the changes were applied correctly:
Assistant
I see there's an extra closing brace on line 205. Let me fix that:
Assistant
The changes look good now. Let me verify both files have been modified correctly by checking the diff:
Assistant
The diff looks correct. Now let me verify that the code compiles and run the existing tests to make sure I haven't broken anything.
Assistant
Let me try running the tests to verify my fix works correctly for the dialog/alert-dialog/popover components.
Assistant
The test is taking too long. Let me try a different approach - I'll check if there's a simpler way to run tests or verify the code:
Assistant
user.committable_instructionsWhat I need to do:
- Continue with verifying the fix I've made to the focus management issue
- The test timed out, so I should try a different approach to verify the changes
- Check if the TypeScript compiles correctly using the build config
- Then run the relevant tests
Let me try using the build TypeScript config which should be faster, and then try running just the specific test file.
Assistant
The TypeScript errors are all pre-existing build configuration issues (missing .d.ts files from the utils package), not related to my changes. Let me verify my changes are syntactically correct by checking just the modified files:
Assistant
The TypeScript errors are all related to missing dependencies and build artifacts, not to my actual code changes. These are pre-existing issues with running TypeScript outside the proper build context. The important thing is that my changes to
store.tsandpopupStoreUtils.tsdon't introduce any new type errors.Let me verify my changes are syntactically correct by checking the files directly and then try running the actual tests:
Assistant
Let me verify my changes look correct and then run the existing tests to make sure I haven't broken anything.
Assistant
The code changes look correct. Let me try running the existing tests to verify nothing is broken:
Assistant
The detached triggers tests passed. Let me also run the popupStoreUtils tests and the main DialogRoot tests:
Assistant
All tests pass! Let me run more related tests to make sure my changes don't break anything:
Assistant
...
Assistant
The previous command was successful - all Alert Dialog tests pass. Let me continue running more tests to verify the fix doesn't break anything else. I should run the Popover tests as well since the issue mentions all three components (Alert Dialog, Dialog, and Popover).
Assistant
All tests pass! Let me now run the menu tests as well since the menu component also uses
useTriggerDataForwardingand the popup utilities:Assistant
All tests pass. Let me also run the tooltip tests since they also use the popup utilities:
Assistant
All tests are passing. Let me verify the final diff and submit the changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.