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 start by understanding the repository structure and the issue. The issue is about focus management in Alert Dialog, Dialog, and Popover components when using controlled mode with multiple triggers. When closing the dialog using the "open programmatically" button, focus should return to that button, but instead it goes to a different trigger.
Let me explore the repository structure first.
Assistant
Let me explore the packages directory to understand the components.
Assistant
Let me explore the react package to find the relevant components.
Assistant
Let me find the Alert Dialog, Dialog, and Popover components.
Assistant
The issue is about focus management when closing popups/dialogs. Let me look at how the focus is managed. Let me first check if there's a shared popup mechanism
Assistant
Let me look at the Popup components and also check for AlertDialog.
Assistant
Let me examine the popup components, starting with DialogPopup.
Assistant
Let me try the open command without FILE parameter (it seems like it's positional).
Assistant
Let me read the files directly
Assistant
Let me look at the FloatingFocusManager component.
Assistant
Let me read the FloatingFocusManager to understand how
returnFocusworks.Assistant
Let me read the full file without truncation.
Assistant
Let me look at the key parts more carefully - the
getReturnElementfunction and the open/close handling.Assistant
Let me read the rest of the file.
Assistant
Let me look at the root components for Dialog, Alert Dialog, and Popover to understand how the trigger opening is handled.
Assistant
Let me look at the
useDialogRoot.tsto understand how triggers work.Assistant
Let me look at the popups utility file.
Assistant
Let me find the file.
Assistant
It seems like no files were found. Let me search more broadly.
Assistant
Let me search for the file more broadly.
Assistant
Let me see what's in
packages/react/src/utils/.Assistant
There's a
popupsdirectory. Let me look inside itAssistant
Let me read the popup-related utility files.
Assistant
Let me read the popup store file.
Assistant
Let me look at the trigger components to understand how opening is handled, particularly how
domReferenceElementis set.Assistant
Let me look at how
domReferenceElementis set for the FloatingRootContext.Assistant
Now I understand the issue. Let me look at the initial focus effect more closely and trace the timing of when
previouslyFocusedElementis captured.Assistant
Let me see the full return focus effect and the initial focus effect to understand the timing
Assistant
Assistant
Let me look at how the DialogRoot handles programmatic open and how
activeTriggerElementis managed.Assistant
Let me read
useRenderDialogRoot.tsx.Assistant
Let me look at the DialogStore.
Assistant
Let me look at the detached triggers test to understand the expected behavior better.
Assistant
Let me look at the relevant tests more broadly.
Assistant
Let me search for focus-related tests.
Assistant
Let me look at the test around line 644 and 680 in DialogPopup.test.tsx.
Assistant
Let me look at the test for
finalFocusreturning true.Assistant
Let me look at the PopoverPopup.tsx.
Assistant
Let me check the AlertDialog component.
Assistant
Let me check if AlertDialog has its own popup or reuses Dialog
Assistant
Let me look at the alert-dialog index to see its parts.
Assistant
Now I have a good understanding of the issue. Let me verify my theory by looking at how
domReferenceElementis updated.Assistant
Let me re-read the
useSyncedFloatingRootContext.tsmoreAssistant
Now I've confirmed the issue. The problem is in
useSyncedFloatingRootContext.tsat line 86. WhenreferenceElement(which comes fromactiveTriggerElement) is null (like when opening a dialog programmatically without a trigger),domReferenceElementis not updated and retains its previous value (pointing to the last-used trigger). This causesFloatingFocusManager.getReturnElement()to return the wrong trigger element instead of falling back togetPreviouslyFocusedElement().Let me apply the fix:
Assistant
Now I'll apply the fix.
Assistant
Let me use the bash_terminal to run the edit command
Assistant
The edit didn't work correctly. I need to fix the indentation and remove the leftover
ifblock. Let me view the current state and fix it properlyAssistant
I need to fix the edit. Let me replace the whole block properly.
Assistant
The fix looks correct now. Let me verify the change and run
Assistant
Now let me run the existing tests to make sure this fix doesn't break anything.
Assistant
All tests are failing, which seems like a pre-existing issue with the test environment. Let me check a specific error message to understand what's happening
Assistant
Let me check for test files and also see if the test failures are pre
Assistant
Let me see what error a specific test gives us
Assistant
The test failure is a pre-existing issue with the test environment (missing
@base-ui/utils/errormodule). Let me check if there's a way to build the project and verify the fix is correct by checking the TypeScript compilationAssistant
Let me check if the error module exists
Assistant
Let me try to verify my change by looking at the test setup and seeing if the packages need building
Assistant
Let me check the react package vitest config
Assistant
Let me check the shared config.
Assistant
Let me check the vite shared
Assistant
Let me check if the error module exists in the utils package
Assistant
Let me check the utils package.json for exports
Assistant
Let me check the test setup
Assistant
The test failures appear to be a pre-existing environment issue. Let me verify my change is correct by checking the TypeScript compilation and looking at the actual logic.
Assistant
Let me verify the consistency with
useFloatingRootContext.tswhich handles thisAssistant
My fix is consistent with the existing pattern used in
useFloatingRootContext.ts. Now let me also verify the final state of theAssistant
Let me check if there are any other usages of
useSyncedFloatingRootContextto make sure the change is safe across all components.Assistant
Let me check the MenuRoot usage to ensure consistency
Assistant
The change looks good. Let me also verify the fix handles the edge case where
domReferenceElementwas intentionally preserved during close animations. Let me check thesetOpenTriggerStatefunction behavior.Assistant
Let me see the full
setOpenTriggerStateAssistant
The fix looks correct and handles all edge cases properly. Let me verify the final state of the file one more time before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.