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 the issue. The problem is about focus management in Alert Dialog, Dialog, and Popover components. When a dialog is opened programmatically (via a button like "Open Programmatically"), closing the dialog should return focus to the button that opened it, but instead it's returning focus to a different button (like "Delete").
Let me start by exploring the repository structure to understand how it's organized.
Assistant
Let me explore the packages directory to find the Alert Dialog, Dialog, and Popover components.
Assistant
Let me look at the dialog component structure first, since it's likely shared across alert-dialog and popover.
Assistant
The user is pointing out that my function call had an incorrect argument name. I used
FILEinstead offile. Let me fix this and use the correct argument name.Assistant
It seems the
opencommand isn't working as expected. Let me try using bash commands to view the file content.Assistant
Now let me look at the popup utilities that handle focus and trigger management:
Assistant
Now let me look at the store to understand how the trigger tracking works:
Assistant
Now I need to understand how focus is restored when a dialog closes. Let me look at the floating-ui-react utilities for focus management:
Assistant
Let me look at the useDismiss hook since it handles focus restoration when dialogs are dismissed:
Assistant
Now let me look at how focus is restored when the dialog closes. Let me check the floating-ui-react components for focus-related functionality:
Assistant
Let me see more of the FloatingFocusManager file to understand how
domReferenceis set:Assistant
I can see the
FloatingFocusManagercomponent retrievesdomReferencefromstore.useState('domReferenceElement'). Let me look at the store to understand howdomReferenceElementis set:Assistant
Now I see the issue. The
domReferenceElementis set toreferenceElementin the store. ThisreferenceElementcomes from the Floating UI system and is typically set by the first trigger. Let me look at how the dialog popup uses this:Assistant
Now I see the issue. The
FloatingFocusManagerusesdomReferenceElementfrom the store to determine where to return focus. Let me check theuseSyncedFloatingRootContexthook to see how thedomReferenceElementis synced:Assistant
Now I understand the flow. The key insight is:
useSyncedFloatingRootContextsyncsreferenceElementfrom the popup store'sactiveTriggerElement- The
domReferenceElementin theFloatingRootStoreis set fromreferenceElementwhen it's a DOM element FloatingFocusManagerusesdomReferenceElementto restore focus
So the issue is that when a dialog is opened programmatically (not via trigger click), the
activeTriggerElementmight not be set correctly, or it gets overwritten. Let me look at the trigger component and howactiveTriggerElementis set:Assistant
Now let me look at the
useClickhook to see how it sets the trigger:Assistant
Now I see how the trigger works. The
useClickhook passes thetargetelement in thecreateChangeEventDetailswhen opening the dialog. Let me look at the store to see how the trigger element is tracked whensetOpenis called:Assistant
Now let me look at the
setOpenTriggerStatefunction more closely:Assistant
Now I'm starting to understand the issue. Let me trace through the flow:
- When a dialog is opened via a trigger button click, the
useClickhook callsstore.setOpen(true, details)with the target element as the trigger - The
setOpenTriggerStatefunction setsactiveTriggerIdandactiveTriggerElementbased on the trigger element passed - When the dialog closes,
FloatingFocusManagerreturns focus todomReferenceElementwhich is synced fromactiveTriggerElement
Now let me look at the test file for the detached triggers to understand the expected behavior better:
- When a dialog is opened via a trigger button click, the
Assistant
Let me search for tests related to focus restoration with multiple triggers:
Assistant
Let me look at the test for focus with controlled mode and multiple triggers:
Assistant
Let me look at the "Open programmatically" test case:
Assistant
Now I understand the problem. When a dialog is opened programmatically (using
setOpen(true)with atriggerId), theactiveTriggerElementmight not be set correctly because there's no actual click event that passes the trigger element.Let me look at how the trigger element is synchronized when using
triggerIdin controlled mode:Assistant
Now I need to understand how the
triggerIdPropaffects theactiveTriggerElement. Looking at the selectors in the store:Assistant
Looking at line 145-146, I see that
activeTriggerIdSelectorreturnsstate.triggerIdProp ?? state.activeTriggerId. This means whentriggerIdPropis set (controlled mode withtriggerId), the selector uses it. But theactiveTriggerElementselector on line 185-186 just returnsstate.activeTriggerElementdirectly - it doesn't look up the element fromtriggerIdProp.Now let me understand the issue better. The
useSyncedFloatingRootContextsyncsreferenceElementfrompopupStore.useState('activeTriggerElement'). So ifactiveTriggerElementisnullwhen opening programmatically,domReferenceElementwill also benullor stale, causing focus to go to the wrong element.Let me look at how
activeTriggerElementgets set whentriggerIdPropchanges:Assistant
The problem is becoming clearer. When opening programmatically with
triggerId, thetriggerIdPropis set, butactiveTriggerElementis not synced to the element corresponding to thattriggerId.Let me look at
useSyncedFloatingRootContextagain and trace the issue:Assistant
So the issue is clear now:
- When a dialog is opened programmatically with a
triggerId, thetriggerIdPropis set - The
activeTriggerIdSelectorusestriggerIdProp ?? activeTriggerId, soactiveTriggerIdis correctly set - However,
activeTriggerElementis not automatically updated whentriggerIdPropchanges - The
useSyncedFloatingRootContextsyncsreferenceElementfromactiveTriggerElement FloatingFocusManagerusesdomReferenceElementfor returning focus
The fix should be: When
triggerIdPropis set and there's a matching trigger element intriggerElements, we should updateactiveTriggerElementto point to that element.Let me look at how
triggerElementsis structured:- When a dialog is opened programmatically with a
Assistant
Now I understand the complete picture. The fix should sync
activeTriggerElementfromtriggerIdPropwhen it's set. The best place to do this is inuseImplicitActiveTriggeror in a similar synchronization hook.Let me look at how
useImplicitActiveTriggerworks and where the sync should happen:Assistant
I see the issue now.
useImplicitActiveTriggeronly setsactiveTriggerElementwhen there's noactiveTriggerIdand only one trigger. But whentriggerIdPropis set (controlled mode), theactiveTriggerIdselector returns the prop, butactiveTriggerElementis never synced.The fix should be: When the popup is open and
triggerIdPropis set (oractiveTriggerIdis set in general), we should ensureactiveTriggerElementis synced to the corresponding DOM element fromtriggerElements.Let me modify
useImplicitActiveTriggerto also sync theactiveTriggerElementwhentriggerIdPropchanges:Assistant
The fix needs to ensure that when
triggerIdProp(oractiveTriggerId) is set and the popup is open, theactiveTriggerElementis updated to point to the actual DOM element. Let me modifyuseImplicitActiveTrigger:Assistant
Now I understand the exact issue and where to fix it. The
useImplicitActiveTriggerhook handles setting the active trigger when there's only one trigger. However, when the popup is opened programmatically with atriggerId, theactiveTriggerIdis set viatriggerIdProp, but theactiveTriggerElementis never updated to match the corresponding DOM element.The fix should extend
useImplicitActiveTriggerto also syncactiveTriggerElementwhenactiveTriggerIdis set butactiveTriggerElementdoesn't match the corresponding element in thetriggerElementsmap.Let me write the fix:
Assistant
The edit command needs proper syntax. Let me use a different approach:
Assistant
Let me verify the change was applied correctly:
Assistant
The fix has been applied. Now let me run the existing tests to make sure I didn't break anything:
Assistant
The command is taking too long due to package installation. Let me check the test script configuration:
Assistant
I need to continue working on this issue. Let me check what test commands are available and try to run the tests for the dialog and related components.
Assistant
I need to continue working on fixing the focus issue. Let me try running the tests with pnpm directly since npm seems to be having issues.
Assistant
I should continue working on this issue. Let me try running the tests with pnpm directly since npm seems to be having issues.
Assistant
The pnpm installation is prompting for user input. Let me try a different approach - install pnpm first and then run tests:
Assistant
Now let me run the tests for the dialog component:
Assistant
The tests are failing because they need a browser (Playwright) to run. Let me check if there's a DOM-based test runner available:
Assistant
I see - the tests run with jsdom environment by default but with an optional browser mode. Let me try running the tests without the browser mode:
Assistant
The dialog popup tests pass. Now let me run the detached triggers tests which are more relevant to the issue:
Assistant
The tests pass but many are skipped. Let me also run the popup store utils tests:
Assistant
All tests pass. Now let me also run the tests for alert-dialog and popover since those are also mentioned in the issue:
Assistant
I should continue verifying the fix by running tests for the other affected components (popover) and making sure my change doesn't break anything.
Assistant
I need to continue verifying that my fix works correctly. Let me run more comprehensive tests to ensure the changes don't break existing functionality and also verify the fix for the focus issue.
Assistant
I need to continue working on this issue. Let me run the tests and verify the fix is working correctly.
Assistant
All dialog tests pass. Let me now verify that my fix handles the edge cases correctly. Let me check if there's a test case that specifically tests focus return with multiple triggers:
Assistant
I see that the tests for multiple triggers are skipped in JSDOM (
describe.skipIf(isJSDOM)). This is probably because focus tests need a real browser. Let me check if there are any focus tests that run in JSDOM:Assistant
Let me check if there are existing tests that verify focus returns to the correct trigger when using multiple triggers:
Assistant
Now let me create a test script to verify that my fix works correctly for the specific issue mentioned - focus returning to the correct trigger when opening programmatically:
Assistant
The test file needs to follow the naming convention. Let me rename it:
Assistant
The test has an import issue because of the dynamic require. Let me fix the test file:
Assistant
I should continue testing my fix by running the test I created.
Assistant
Let me analyze what's happened so far and what I need to do next:
-
I identified the issue: When a dialog/popover is opened programmatically with a
triggerId, theactiveTriggerElementwasn't being synced to the corresponding DOM element. -
I made a fix in
/base-ui/packages/react/src/utils/popups/popupStoreUtils.tsin theuseImplicitActiveTriggerfunction to syncactiveTriggerElementwhenactiveTriggerIdis set. -
I ran the existing tests:
- Dialog tests: All passed
- Popover tests: All passed
- Alert dialog tests: All passed
- popupStoreUtils tests: All passed
-
I created a test file (
test-focus-fix.test.tsx) that verifies the fix works - it passed!
Now I need to:
- Clean up the test file I created (since I shouldn't add new tests to the repository)
-
Assistant
I need to properly format my tool call using the bash_terminal function.
Assistant
Now let me run a broader set of tests to ensure my fix doesn't break any other components that use the popup utilities:
Assistant
All popup utility tests pass. Let me also run tests for other components that might use the popup utilities (like menu, select, tooltip) to ensure nothing is broken:
Assistant
All menu tests pass as well. Let me also run tests for select and tooltip to be thorough:
Assistant
All tests pass. Let me verify my changes one more time and then submit:
Assistant
Now let me verify the changes one more time and provide the summary for the checklist before submitting.
Assistant
The changes look good. Let me now submit my solution.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.