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
Assistant
Let me understand the issue:
- In Alert Dialog, Dialog, and Popover components, when there are multiple triggers and the dialog is opened programmatically (not via a trigger click), focus should return to the button that opened the dialog when it closes.
- Currently, focus returns to a different trigger (e.g., the "Delete" button) instead of the "Open Programmatically" button.
Let me explore the repository structure first to understand how it's organized and find the relevant source code.
Assistant
Let me explore the structure more, especially looking for dialog, alert-dialog, and popover components.
Assistant
Let me look at the key files. The focus management likely happens in dialog/popup or dialog/root or dialog/store. Let me explore these directories.
Assistant
Let me look at the popup component for dialog as that's where focus management likely happens.
Assistant
Let me look at the DialogPopup.tsx to understand focus management.
Assistant
Let me use the correct format.
Assistant
Now let me look at the FloatingFocusManager to understand how focus is returned when closing.
Assistant
Let me understand how
domReferenceis set and how the store tracks triggers.Assistant
Now let me look at the
popupsutility file, specificallysetOpenTriggerStateandPopupTriggerMapAssistant
Let me find where popups utils are.
Assistant
Let me find the actual file path referenced in the import.
Assistant
Let me look at the popupStoreUtils.ts file
Assistant
Let me now look at the store file to understand PopupTriggerMap and the state structure.
Assistant
Now let me look at the Dialog trigger component to understand how triggers register themselves
Assistant
Now let me understand how
domReferenceis determined in the FloatingFocusManager. Let me look at the FloatingRootStoreAssistant
Let me look at how
domReferenceis used in FloatingFocusManager more closely.Assistant
Let me search for how domReferenceElement gets updated throughout the codebase.
Assistant
Let me look at the synced floating root context and how referenceElement/domeference is set
Assistant
Let me look at the DialogRoot component to understand how
setOpenworksAssistant
Let me look at
useRenderDialogRootto understand how the dialog opens.Assistant
Let me look at
useDialogRootto understand the interactions.Assistant
Let me find the file.
Assistant
It's a .ts file, not .tsx. Let me read it.
Assistant
Let me look at the
useClickhook to understand how the trigger sets up the open event.Assistant
Let me look at how
returnFocusRefis initialized inFloatingFocusManagerAssistant
Let me check the test file for DialogPopup to understand expected
Assistant
Let me look at the relevant tests around finalFocus.
Assistant
Let me look at the initial focus effect in FloatingFocusManager more
Assistant
Let me look at the initial focus effect.
Assistant
Let me find where
addPreviouslyFocusedElementis called.Assistant
Let me look at the context around line 735 where
addPreviouslyFocusedElementis called.Assistant
Now I have a clear understanding of the issue. Let me look at the relevant section of
FloatingFocusManager.tsxaround thegetReturnElementfunction and make the fix.The problem is in the
getReturnElementfunction: whenreturnFocusistrue(the default), it prefersdomReference(the trigger element) overgetPreviouslyFocusedElement()(the element that was focused before the dialog opened). When the dialog is opened programmatically,domReferencegets incorrectly set to one of the trigger elements (not the programmatic button), so focus returns to the wrong element.Let me look at the exact code and apply the fix:
Assistant
Now I'll apply the fix. Let me edit the
getReturnElementfunction to prefer the previously focused element (the originating control) overdomReference(trigger element):Assistant
Good, the edit looks correct. Let me now verify the change by running the relevant tests. Let me first check if TypeScript compilation works
Assistant
These TS errors are pre-existing build configuration issues, not related to our change. Let me run the relevant tests.
Assistant
Already in the right directory. Let me run the tests
Assistant
Let me find how to properly run tests in this project
Assistant
Let me check the root and find how to run tests
Assistant
Let me try running from the root directory
Assistant
Let me try a different approach
Assistant
Let me check the vitest config and test setup
Assistant
Let me try running vitest from the root
Assistant
Let me look at the failing test to understand what it expects
Assistant
Let me look at the full test context to understand the flow
Assistant
I need to reconsider my approach. Let me revert the change to
FloatingFocusManagerand instead fix the root cause:useTriggerDataForwardingincorrectly claims the active trigger when the popup is opened programmatically.Assistant
The edit seems to have duplicated content. Let me check the current state and fix it.
Assistant
Let me fix the duplicated lines
Assistant
There's still a duplicate line. Let me remove it.
Assistant
Let me now look at how
useTriggerDataForwardingis used and think about the fix more carefully. Let me also check the popover component.Assistant
Let me look at how the popover positioner uses the reference element
Assistant
Let me run the test again to confirm the original code passes
Assistant
Let me look at how the controlled open state works.
Assistant
Let me look at how anchor positioning handles missing reference element
Assistant
Let me see the full context around anchor positioning reference usage
Assistant
Now I have a clear understanding. The root cause is in
useTriggerDataForwardingin/base-ui/packages/react/src/utils/popups/popupStoreUtils.ts. When a popup is open andactiveTriggerIdis null, the first trigger that registers claims the active status, even when the popup was opened programmatically (not via a trigger).This auto-claim is needed when there's exactly one trigger (e.g.,
defaultOpenscenario), but when there are multiple triggers, it incorrectly assigns the active trigger to whichever registers first.Let me implement the fix:
Assistant
Let me look at the relevant section and apply the fix.
Assistant
Let me see the rest of the function
Assistant
Now I understand the issue clearly. The fix should be in
useTriggerDataForwarding: only auto-claim the active trigger when there's exactly one trigger. When there are multiple triggers, the first one that registers shouldn't automatically become the active trigger.Let me apply the fix
Assistant
I see there's a duplicate closing brace. Let me fix it by removing the extra one.
Assistant
I need to remove the extra closing brace at line 182.
Assistant
Good, the code looks correct now. Let me now run the FloatingFocusManager tests again to make sure nothing is broken by my change.
Assistant
All 49 FloatingFocusManager tests pass. Let me also run the dialog popup tests.
Assistant
All dialog popup tests pass. Let me also run the popover tests to make sure they still pass.
Assistant
All tests pass. Let me also run the alert-dialog tests
Assistant
Let me try running the skipped multiple trigger tests to see if they validate the fix
Assistant
The multiple trigger tests are skipped (likely require a different environment). Let me check the test file to see why.
Assistant
The multiple trigger tests are skipped in JSDOM. My fix is minimal and correct. Let me now run a broader set of tests to make sure nothing is broken.
Assistant
All tests pass. Let me check the final diff of my changes.
Assistant
I notice there's an unintentional whitespace change in FloatingFocusManager.tsx. Let me revert that
Assistant
Let me verify the final diff only contains the intended
Assistant
Let me run the key tests one more time to confirm everything passes
Assistant
All 144 tests pass across 4 test files. The fix is clean and correct.
Let me now go through the checklist:
Checklist
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.