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 accessibility in the Alert Dialog, Dialog, and Popover components. When using "Open Programmatically" buttons to open these dialogs, closing them moves focus to the wrong trigger instead of the one that originally triggered 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 relevant components (Alert Dialog, Dialog, Popover).
Assistant
Now let me explore the alert-dialog, dialog, and popover components to understand their structure:
Assistant
The AlertDialogRoot uses
useRenderDialogRootfrom the Dialog component. Let me look at the Dialog component structure:Assistant
Let me examine the key files related to the dialog root:
Assistant
Now let me look at the DialogStore and the popup utilities, since they seem to handle focus restoration:
Assistant
Now let me look at the popup utilities since they seem to handle the focus and trigger state:
Assistant
Now let me look at the popup store to understand how triggers are tracked:
Assistant
Now I need to understand how the focus restoration works when closing the dialog. Let me look at the floating-ui-react module, specifically the FloatingRootStore:
Assistant
Let me look at the useDismiss hook that handles the escape key and focus restoration:
Assistant
Now let me look at the FloatingFocusManager that handles focus restoration:
Assistant
This is the key file. I can see that the focus restoration happens in
getReturnElement()and it usesdomReferencefor returning focus. Let me look at howdomReferenceis set:Assistant
I see that
domReferencecomes fromstore.useState('domReferenceElement'). Let me look at how thedomReferenceElementis set in the FloatingRootStore:Assistant
Let me look at the
useSyncedFloatingRootContextwheredomReferenceElementis synced with thereferenceElement:Assistant
Now I see a critical part! The
referenceElementis synced frompopupStore.useState('activeTriggerElement')- so thedomReferenceElementin the floating store is set from theactiveTriggerElementin the popup store.Let me trace back how
activeTriggerElementis set. Let me look at the dialog trigger component:Assistant
Now let me look at the test file for the detached triggers to understand the expected behavior better:
Assistant
Let me search for the test that deals with focus restoration:
Assistant
"total_tokens": 60319, "progress": "MODERATE" }
Assistant
Let me continue exploring the test file to understand the focus behavior better.
Assistant
Let me search for tests related to the focus return behavior when closing the dialog programmatically.
Assistant
Let me look at the test around line 212 which seems to be testing the "Open programmatically" behavior - this is exactly what the issue is about.
Assistant
Now I understand the issue better. Let me look at how the focus is tracked and restored. The problem is that when opening programmatically, the
activeTriggerIdis set to one of the Dialog.Trigger IDs, so focus returns to that trigger instead of the "Open Programmatically" button.Let me look at the
setOpenTriggerStatefunction more closely:Assistant
Assistant
I need to use the proper tool call format to read the file.
Assistant
Now I understand the structure. The
DialogPopupusesFloatingFocusManagerwithreturnFocus={finalFocus}which defaults totrue. The fix needs to be inFloatingFocusManager.tsxin thegetReturnElementfunction.Let me look at the specific section of the code that needs to be modified:
Assistant
Now I can see the issue clearly. The problem is in lines 796-801 where if
domReference?.isConnectedis true, it returnsdomReferencewithout checking if it's the same element that was actually focused when the popup opened. Let me implement the fix.Assistant
Now I'll implement the fix. The issue is in the
getReturnElementfunction where it returnsdomReferencewithout checking if it's the same as the element that was actually focused when the popup opened.Assistant
The edit command found the text at line 796. Let me navigate to that location first and then try the replace again.
Assistant
The search text is at line 796 but with different whitespace. Let me look at the exact content more carefully and use a proper replace command.
Assistant
The file is currently open as FloatingFocusManager.tsx. The sed command shows the exact lines with their whitespace. I can see the text uses 8 spaces for indentation (4 + 4 for the nested if inside a function). Let me use the edit command with the correct indentation.
Assistant
The file is now open and I can see the exact lines I need to modify. Lines 796-802 contain the code that needs to be changed. Let me apply the edit using the edit command with the correct line range and replacement text.
Assistant
I need to run tests to validate my fix. Let me first find and run the relevant tests for the FloatingFocusManager and the dialog/alert-dialog/popover components.
Assistant
I should run the FloatingFocusManager tests and also the dialog-related tests to make sure my fix doesn't break anything and properly addresses the issue. Let me start by running the tests.
Assistant
The test command timed out. Let me try a different approach - maybe use a more specific test command or check if there's a faster way to run tests.
Assistant
The tests are timing out. Let me try running with vitest directly and see if there's a faster way, or I can check the package.json for available test scripts.
Assistant
Let me try running the test with a different approach - using vitest directly with the --run flag and maybe limiting the output.
Assistant
I need to investigate the failing test to understand what scenario it's testing. Let me look at that specific test case.
Assistant
The test is failing because in the "supports keepMounted behavior" test, when the user presses Escape to close the floating element, focus should return to the reference element, but instead it's going to the child element.
Let me look at the full test to understand what it's testing.
Assistant
I see the issue now. The test fails because
previouslyFocusedElementmight be an element inside the floating element (like the child button). I need to add a check to ensure we only preferpreviouslyFocusedElementwhen it's outside the floating element. Let me update my fix:Assistant
The edit looks good. Now let me run the FloatingFocusManager tests again to verify that my fix resolves the failing test while still addressing the issue.
Assistant
All tests pass now. Let me also run the dialog-related tests to ensure the fix doesn't break any existing behavior for Dialog, Alert Dialog, and Popover components.
Assistant
All dialog tests pass. Let me also run the alert-dialog and popover tests.
Assistant
All popover tests pass as well. Let me also run the DialogRoot.detached-triggers tests which specifically test the multiple trigger scenarios.
Assistant
All tests pass. Now let me create a simple test script to verify that my fix actually resolves the issue described - focus returning to the correct trigger when opening programmatically.
Assistant
I need to continue with a tool call. Let me verify the fix works by looking at the test that specifically tests returning focus behavior.
Assistant
Let me verify my changes by reviewing the diff:
Assistant
The diff looks correct. Let me verify my fix handles all the edge cases by reviewing the logic:
- Normal trigger click: When user clicks a Dialog.Trigger directly,
domReference === previouslyFocusedElement, so it returnsdomReference✓ - Programmatic open: When user clicks "Open Programmatically" button,
previouslyFocusedElementis that button (not inside floating), so it returns that button ✓ - KeepMounted behavior: If
previouslyFocusedElementis inside the floating element (e.g., from a re-run of the effect), the check!contains(floating, previouslyFocusedElement)prevents returning focus to an element inside the popup ✓ - No previously focused element: Falls back to
domReferenceif connected ✓
Let me also run a broader set of tests to ensure I haven't broken anything else:
- Normal trigger click: When user clicks a Dialog.Trigger directly,
Assistant
All tests pass. Let me now provide a summary of my changes and submit.
Summary
I fixed an accessibility issue where focus was returning to the wrong trigger when closing dialogs opened programmatically.
Problem
When using "Open Programmatically" buttons to open Dialog/AlertDialog/Popover components, closing them moved focus to a different control (one of the Dialog.Trigger elements) instead of the button that actually triggered the dialog.
Root Cause
In
FloatingFocusManager.tsx, thegetReturnElement()function always returneddomReferencewhen it was connected to the DOM, without checking if it was the same element that was focused when the popup opened. When a dialog is opened programmatically via atriggerIdprop,domReferenceis set to one of the Dialog.Trigger elements, not the button that was clicked.Fix
Modified
getReturnElement()in/base-ui/packages/react/src/floating-ui-react/components/FloatingFocusManager.tsxto:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.