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
I'll start by exploring the repository structure to understand the codebase and locate relevant components. Let's first get an overview.
- Assistant
Let's explore the structure more and find components related to alert-dialog, dialog, and popover. Let's search for files containing "alert-dialog", "dialog", and "popover".
- Assistant
Let me explore the packages directory to understand the structure better.
- Assistant
Let me look at the packages/react directory to understand the structure better.
- Assistant
Now let's examine the issue more closely. The issue is about focus management when closing dialogs/popovers that were opened programmatically. The focus should return to the element that triggered the opening, but instead it's going to a different element.
Let me start by examining the alert-dialog component:
- Assistant
Let's look at the alert-dialog component structure:
- Assistant
Let me look at the AlertDialogRoot component to understand how focus is managed:
- Assistant
Now let's look at the dialog component to understand the focus management logic:
- Assistant
Let's look at the dialog root component to understand how focus is managed:
- Assistant
Let's look at the useRenderDialogRoot hook:
- Assistant
Now let's look at the useDialogRoot hook to understand how focus is managed when the dialog closes:
- Assistant
Let me find the right file.
- Assistant
The file is
.tsnot.tsx. Let me open it. - Assistant
Let me scroll down to see the rest of the file and also look at the popup utilities:
- Assistant
Now let me look at the utility functions mentioned in the imports, particularly
usePopupInteractionPropswhich seems to be responsible for managing focus: - Assistant
Let me check what's in the utils directory.
- Assistant
Let me check the popups directory:
- Assistant
Let me look at the popups index.ts file:
- Assistant
Let me look at the store.ts file:
- Assistant
Let me see the rest of the store file:
- Assistant
Let me continue reading the store file and then look at the popupStoreUtils.ts:
- Assistant
Now let me look at the popupStoreUtils.ts file:
- Assistant
Let me continue reading the popupStoreUtils.ts file:
- Assistant
Let me continue reading the popupStoreUtils.ts file:
- Assistant
Let me continue reading to see the full function:
- Assistant
Now let me search for focus-related code in the codebase to understand how focus is managed when the popup closes:
- Assistant
Let me look at how the Dialog Popup and Popover components handle focus management, specifically the
FloatingFocusManager: - Assistant
Let me continue reading to see how the FloatingFocusManager is configured:
- Assistant
Now let me look at the FloatingFocusManager component to understand how it handles focus when closing: Kinda long, let me focus on the returnFocus logic:Now I need to find the floating-ui-react's
FloatingFocusManager. Let me look at that: - Assistant
Let me look at FloatingFocusManager.
- Assistant
Let me open the FloatingFocusManager component:
- Assistant
Let me search for the key focus restoration logic:
- Assistant
Let me look at the key focus restoration logic around line 781:
- Assistant
Let me look at the
domReferenceElement- how is it set in the store context: - Assistant
Let me look at the
FloatingRootStoreand how it manages thedomReferenceElement: - Assistant
Let me look at the FloatingRootStore:
- Assistant
Let me see the rest of the FloatingRootStore:
- Assistant
Now let me look at how
setOpenis called in the dialog store: - Assistant
Let me continue reading the DialogStore:
- Assistant
Now let me look at the trigger component to see how the trigger is associated with the dialog:Now I need to understand how the
domReferenceis set in theFloatingRootStore. Let me look atuseSyncedFloatingRootContext: - Assistant
Let me look at
useSyncedFloatingRootContext: - Assistant
Now I understand the flow. Let me look at how the trigger component works to understand how
activeTriggerElementis set: - Assistant
Now let me look at the examples/docs for the controlled mode with multiple triggers to understand the issue better:
- Assistant
Let me look at the detached triggers test:
- Assistant
Let me search for the specific controlled mode with multiple triggers example in the docs:
- Assistant
Let me look at the docs for the controlled mode with multiple triggers example:
- Assistant
Let me search for "open programmatically" in the docs:
- Assistant
Let me look at the dialog experiments file to understand the exact pattern:
- Assistant
The path has parentheses, I need to escape it or quote it.
- Assistant
Let me scroll down to see the "Controlled mode with multiple triggers" example.
- Assistant
Let me now look at the docs examples for the issue - specifically the "controlled mode with multiple triggers" demos:
- Assistant
Let me look at the alert dialog demos:
- Assistant
Let me look at the alert dialog detached-triggers-controlled demo:
- Assistant
Let me search for where
addPreviouslyFocusedElementis called in the FloatingFocusManager: - Assistant
Let me look at the context where
addPreviouslyFocusedElementis called: - Assistant
Now I have a good understanding of the issue. Let me trace through the flow:
- When a user clicks "Open programmatically" button,
setOpen(true)is called - Since no
eventDetails.triggeris provided (it's not aDialogTrigger),setOpenTriggerStatedoesn't updateactiveTriggerId/activeTriggerElement - But the controlled component explicitly sets
triggerIdto "alert-trigger-2" - This makes
domReferencepoint to the "Delete" button - When closing,
getReturnElement()returnsdomReference(the "Delete" button) instead of the "Open programmatically" button
Let me look at the DialogStore and how trigger state is managed:
- When a user clicks "Open programmatically" button,
- Assistant
Now let me understand the full picture. Let me look at how
triggerIdprop is handled when opening programmatically: - Assistant
Now let me check which other components also use
setOpenTriggerState: - Assistant
Let me look at the Popover store:
- Assistant
Now I understand the full picture. The core issue is that when a dialog is opened programmatically (via an "Open programmatically" button, not a trigger), the
activeTriggerElementin the store gets set to the trigger matching thetriggerIdprop, not to the element that actually initiated the open.When closing,
FloatingFocusManager.getReturnElement()returnsdomReferencewhich is synced fromactiveTriggerElement. This means focus goes to the trigger button (e.g., "Delete") instead of the "Open programmatically" button.Let me look at how the flow works end-to-end:
- Assistant
Let me understand how the
addPreviouslyFocusedElementmechanism works. Let me check the timing of when this is called: - Assistant
Now I have a clear understanding of the issue. Let me trace the flow:
- User clicks "Open programmatically" →
setOpen(true)is called witheventDetails.trigger = undefined setOpenTriggerStatedoesn't updateactiveTriggerId/activeTriggerElementbecause the trigger element is not provided- However, the controlled
triggerIdprop setstriggerIdPropin the store useTriggerDataForwardingin the trigger component updatesactiveTriggerElementto the trigger's DOM element (e.g., "Delete" button)useSyncedFloatingRootContextsyncsactiveTriggerElementtodomReferenceElementin the FloatingRootStore- When closing,
FloatingFocusManager.getReturnElement()returnsdomReference(the "Delete" button) instead of the "Open programmatically" button
The root cause is that
setOpenTriggerStatedoesn't capture the originating element when the dialog is opened programmatically. Let me also check how the triggerId prop affects the store: - User clicks "Open programmatically" →
- Assistant
Let me look at how
openMethodflows from the DialogPopup to the FloatingFocusManager: - Assistant
Now I understand the full picture. Let me trace the focus restoration flow:
- When dialog opens,
addPreviouslyFocusedElementcaptures the "Open programmatically" button useTriggerDataForwardingsetsactiveTriggerElementto the trigger's DOM elementuseSyncedFloatingRootContextsyncs this todomReferenceElementin FloatingRootStore- On close,
getReturnElement()returnsdomReference(the trigger's element) instead of the "Open programmatically" button
The key insight is that
openMethodisnullwhen opened programmatically vs having a value when opened by a trigger click. Let me verify this: - When dialog opens,
- Assistant
Now I have a clear picture. The
openMethodin the store isnullfor programmatic opens and has a value (like 'keyboard') for trigger clicks. This is passed asopenInteractionTypetoFloatingFocusManager. I can use this to determine the correct return focus target.Let me now implement the fix. The issue is in
FloatingFocusManager.tsxwheregetReturnElement()always returnsdomReferencewhen it's connected, ignoring thepreviouslyFocusedElementfor programmatic opens. - Assistant
Let me look at the existing tests for detached triggers to understand the expected behavior:
- Assistant
Let me search for tests related to focus management in the detached triggers tests:
- Assistant
Let me search for tests related to focus management in the detached triggers tests:
- Assistant
Now I need to understand the
openMethodtracking mechanism: - Assistant
Now I understand the complete flow. Let me check how the Popover popup passes
openInteractionType: - Assistant
Good. Let me also check AlertDialog's popup:
- Assistant
Let me check how AlertDialog uses the DialogPopup:
- Assistant
The AlertDialog doesn't have its own FloatingFocusManager. It uses Dialog.Popup under the hood through the store mechanism. Let me look at how AlertDialog popup works.
- Assistant
Let me look at the AlertDialog popup:
- Assistant
Looking at the index parts:
- Assistant
Now I have a complete understanding of the issue. The AlertDialog reuses DialogPopup, which passes
openMethod(from the store) to FloatingFocusManager asopenInteractionType. When a dialog is opened programmatically,openMethodstaysnullbecause no trigger click handler fires.The fix is in
FloatingFocusManager.tsx'sgetReturnElement()function. Currently whenreturnFocusistrue(default), it always returnsdomReference(the trigger element) if connected. For programmatic opens, it should preferpreviouslyFocusedElement(the actual element that initiated the open).Let me implement the fix:
- Assistant
I see there's an extra closing brace on line 813. Let me fix that:
- Assistant
I see the extra closing brace on line 813. Let me remove it:
- Assistant
Let me fix the extra brace issue using replace instead.
- Assistant
Good, the extra brace is removed. Let me verify the full
getReturnElementfunction: - Assistant
The code looks correct. Now I need to make sure
openInteractionTypeRefis included in the effect's dependency array: - Assistant
Good, the dependency array looks fine -
openInteractionTypeRefis a ref object which is stable. The.currentvalue will be read at the timegetReturnElement()is called (during cleanup), which is correct.Now let me run the existing tests to make sure nothing is broken:
- Assistant
There's a test failure. Let me look at the failing test:
- Assistant
I see the issue. The default value for
openInteractionTypeis''(empty string), which is also falsy. My condition!openInteractionTypeRef.currentwould also trigger for cases whereopenInteractionTypeis not passed at all (like in this raw floating-ui test). I need to specifically check fornull: - Assistant
Let me navigate to line 803 first.
- Assistant
- Assistant
Now let me run the test again:
- Assistant
All 49 tests pass now. The
=== nullcheck correctly differentiates between:openInteractionTypenot passed (defaults to'') → old behavior preservedopenInteractionTypeexplicitlynull(programmatic open) → new behavior (prefer previously focused element)
Let me now run the dialog-related tests:
- Assistant
Let me also run the dialog root tests:
- Assistant
Let me also run the alert dialog tests:
- Assistant
All alert dialog tests pass. Let me run the popover tests:
- Assistant
All tests pass. Now let me write a reproduction script to verify my fix works:
- Assistant
Now let me verify the fix is correct by running more tests, including the full dialog and popover test suites:
- Assistant
All dialog tests pass. Let me also run the tooltip tests since FloatingFocusManager is used for tooltips too:
- Assistant
All tooltip tests pass. Let me also run the preview-card tests:
- Assistant
All tests pass. Let me also run the menu tests to be thorough:
- Assistant
All tests pass. Let me now verify my final change by reviewing it:
- Assistant
Let me verify the full diff of my changes:
- Assistant
The change is clean and minimal. Let me also clean up the temporary test file:
- Assistant
Let me run the select tests too since they also use popup focus management:
- Assistant
All tests pass. Let me review the final change one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.