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 files for the issue.
- Assistant
Let me explore the repository structure more systematically.
- Assistant
Let me explore the packages directory to find the relevant components.
- Assistant
Let me explore the packages/react directory to find the alert-dialog, dialog, and popover components.
- Assistant
Now let me examine the AlertDialog, Dialog, and Popover components to understand their focus management behavior:
- Assistant
Let me look at the dialog implementation to understand how focus management works.
- Assistant
Let me look at the dialog root implementation to understand focus management.
- Assistant
Let me examine the DialogRoot.tsx to understand how focus management is handled.
- Assistant
Let me check the popup utilities file for focus management:
- Assistant
Let me look at the popupStoreUtils to understand focus management:
- Assistant
Let me look at the popup store to understand how focus return works:
- Assistant
Let me check the dialog popup component for focus return behavior:
- Assistant
Let me examine the DialogPopup.tsx to understand how focus is managed when the dialog closes:
- Assistant
Now let me look at the FloatingFocusManager to understand how focus return works:
- Assistant
Let me look at the FloatingFocusManager to understand how it handles focus return. The issue is that when a dialog is opened programmatically (not via a trigger), focus should return to the element that was focused when the dialog was opened, but it's returning to the default trigger instead.
- Assistant
Now let me look at how the floating root context is set up and what
domReferencerefers to: - Assistant
Let me look at how the synced floating root context works:
- Assistant
Now let me look at how the trigger data is forwarded and how the active trigger is set in the store:
- Assistant
Let me check the docs examples to understand the "controlled mode with multiple triggers" scenario:
- Assistant
Let me look at the alert-dialog examples directory to find the "controlled mode with multiple triggers" example.
- Assistant
Let me look at those demo files.
- Assistant
Let me look at the demo files.
- Assistant
Now I see the issue. When the user clicks "Open programmatically",
setTriggerId('alert-trigger-2')is called but the "Open programmatically" button is not a registered trigger. So when the dialog closes, focus returns to thealert-trigger-2element (the "Delete" button) instead of the "Open programmatically" button. Let me look at how the root component processes thetriggerIdprop: - Assistant
Let me look at
useRenderDialogRootto understand howtriggerIdis processed: - Assistant
Now let me look at how the store handles
triggerIdPropand how theactiveTriggerElementis set. Let me look at the DialogStore: - Assistant
Now I need to understand the
FloatingFocusManagerlogic more carefully. Let me look at howdomReferenceis determined: - Assistant
Let me look more closely at the
getReturnElementfunction and understand how it works: - Assistant
Let me understand the flow better. Let me look at how
openMethodis tracked: - Assistant
Now I need to understand how focus return works. Let me look at the key section of
FloatingFocusManagerthat handles the return focus logic: - Assistant
Let me look at how
triggerIdPropis used in the store: - Assistant
Let me look more carefully at how
openMethodis tracked in the dialog: - Assistant
Let me look at how the controlled
openprop updates the store state: - Assistant
Now let me look at how
FloatingFocusManagerusesopenInteractionTypeto understand the flow better: - Assistant
Let me understand the issue more concretely. The problem is in
getReturnElementfunction of FloatingFocusManager. When the dialog is opened programmatically (not by a trigger click),domReferencepoints to the active trigger element (e.g., the "Delete" button), but focus should return to the "Open programmatically" button which was the previously focused element.Let me check the existing tests:
- Assistant
Now let me understand the core issue. Let me look at how the focus return mechanism determines where to return focus. Let me look at the
getReturnElementfunction more closely: - Assistant
Now I understand the core issue. When the dialog is opened programmatically (not by clicking a trigger), the
domReferencepoints to the active trigger element (e.g., "Delete" button), but focus should return to the "Open programmatically" button which was previously focused. ThegetReturnElementfunction prioritizesdomReferenceovergetPreviouslyFocusedElement().Let me check how
openInteractionTypeis tracked to understand if we can distinguish programmatic opens: - Assistant
Now I understand the issue. The
openMethodisnullwhen a dialog is opened programmatically (not via a trigger click). Let me also check how the Popover handles this similarly: - Assistant
Let me look at the DialogHandle to understand how imperative open works:
- Assistant
Let me check the DialogHandle to understand how it sets the open method:
- Assistant
Let me also check how tooltip and menu handle this:
- Assistant
Let me check the tooltip and menu for openInteractionType usage.
- Assistant
Let me look at the
usePopupRootSynchook that resetsopenMethod: - Assistant
Now I understand the issue clearly. Let me trace through what happens:
- When the "Open programmatically" button is clicked,
triggerId='alert-trigger-2'andopen=trueare set - The dialog opens with
activeTriggerElementbeing the "Delete" button (becausetriggerIdPropoverridesactiveTriggerId) domReferenceinFloatingFocusManagerpoints to the "Delete" button- When the dialog closes,
getReturnElement()returnsdomReference(the "Delete" button) - But
getPreviouslyFocusedElement()would correctly return the "Open programmatically" button
The problem is in
getReturnElement()inFloatingFocusManager. When the dialog is opened programmatically (not via a trigger click),openMethodisnull, so we should prefergetPreviouslyFocusedElement()overdomReference.Let me check the alert-dialog root to see how it works:
- When the "Open programmatically" button is clicked,
- Assistant
Let me examine the full
FloatingFocusManagercomponent more carefully to understand the focus return logic: - Assistant
Now I have a clear understanding of the issue. Let me trace through the problem:
- When "Open programmatically" is clicked,
triggerId='alert-trigger-2'andopen=trueare set - The
triggerIdPropoverridesactiveTriggerIdin the selector, making the active trigger 'alert-trigger-2' - The
activeTriggerElementgets set to the "Delete" button's DOM element - When dialog closes,
getReturnElement()returnsdomReference(synced fromactiveTriggerElement= "Delete" button) - But the "Open programmatically" button is what was focused before the dialog opened
The fix needs to be in
FloatingFocusManager'sgetReturnElement()function. When the dialog was opened programmatically (not by a trigger click),openInteractionTypeisnull, and we should prefergetPreviouslyFocusedElement()overdomReference.Let me also check the
closeTypeRefto understand how it's set: - When "Open programmatically" is clicked,
- Assistant
Now I have a clear understanding of the issue. The problem is in the
getReturnElement()function inFloatingFocusManager.tsx. When a dialog is opened programmatically (not by a trigger click),openMethodisnull, anddomReferencepoints to the active trigger element (e.g., the "Delete" button), but the focus should return to the previously focused element (the "Open programmatically" button).Let me implement the fix:
- Assistant
Now let me look at the specific area I need to modify:
- Assistant
Now I understand the issue. The problem is in the
getReturnElement()function inFloatingFocusManager.tsx. When a dialog is opened programmatically (by clicking a non-trigger button like "Open programmatically"),openMethodisnull(since only trigger buttons set it). ThedomReferencepoints to theactiveTriggerElement(the "Delete" button based on thetriggerIdprop), not the "Open programmatically" button that was actually focused.The fix should modify
getReturnElement()so that when the popup was opened programmatically (i.e.,openInteractionTypeisnull), we prefergetPreviouslyFocusedElement()overdomReference.Let me apply the fix:
- Assistant
Wait, I need to reconsider. The
openInteractionTypehas a default of''(empty string), and!''istruewhich would incorrectly trigger the new code path for components that don't passopenInteractionType. Let me refine the check: - Assistant
Wait, I need to think about this more carefully. The
openInteractionTypeRef.currentis a ref that gets updated every render. By the time the effect cleanup runs (when the dialog closes),openMethodhas already been reset tonullbyusePopupRootSync. SoopenInteractionTypeRef.currentwould benullfor ALL dialogs, not just programmatically opened ones.I need to capture the
openInteractionTypevalue at the time the effect RUNS (when the dialog opens), not when it cleans up. Let me fix this: - Assistant
Let me see the full effect structure:
- Assistant
Now I realize there's a timing issue. The
openInteractionTypeRef.currentwill benullby the time the cleanup runs becauseusePopupRootSyncresetsopenMethodtonullwhen the dialog closes. I need to capture the value at the time the effect RUNS (when the dialog opens), not at cleanup time.Let me update the fix to capture the
openInteractionTypevalue at the time the effect runs: - Assistant
Now I need to update the
getReturnElementfunction to usecapturedOpenInteractionTypeinstead ofopenInteractionTypeRef.current: - Assistant
I see there's a duplicate block. Let me fix the file:
- Assistant
There's duplicate code. Let me fix this:
- Assistant
Good, the duplicate is removed. Now let me verify the full
getReturnElementfunction looks correct: - Assistant
Good. Now let me verify the full effect structure with the
capturedOpenInteractionTypevariable: - Assistant
The code looks correct now. Let me run the existing tests to verify the fix doesn't break anything:
- Assistant
Good, FloatingFocusManager tests pass. Now let me run the dialog tests:
- Assistant
All dialog tests pass. Let me run the alert-dialog and popover tests too:
- Assistant
Good, all alert dialog tests pass. Let me also run the popover tests:
- Assistant
All tests pass. Let me also run the menu tests since menus use focus management too:
- Assistant
Let me also check the AlertDialog popup component to see if it sets
finalFocus: - Assistant
Let me check the AlertDialog popup to understand the
finalFocusprop situation: - Assistant
Let me check how
finalFocusis handled in theDialogPopup: - Assistant
Let me check how AlertDialog.Popup works:
- Assistant
Let me check how AlertDialog handles its popup parts:
- Assistant
Let me check the index.parts.ts to see how AlertDialog maps its parts:
- Assistant
Let me check the
FloatingFocusManagerprops destructuring to see ifreturnFocushas a default: - Assistant
Good, I see that
returnFocusdefaults totruein FloatingFocusManager. So whenfinalFocusis not specified,returnFocuswill betrue, meaning focus should return.Now let me also run the drawer tests since it also uses
openInteractionType: - Assistant
All drawer tests pass. Now let me also run the tooltip tests to make sure nothing is broken:
- Assistant
All tests pass. Let me also verify the fix handles the
finalFocusprop in DialogPopup correctly: - Assistant
Now let me also run the combobox tests since it also uses
openInteractionType: - Assistant
All tests pass! Now let me also check the popover's detached triggers demo to make sure it's the same pattern:
- Assistant
Let me look at the popover detached triggers controlled demo to confirm the same pattern:
- Assistant
Good, I can confirm the same pattern exists in the popover demo. Let me now verify the full change I made:
- Assistant
The diff looks correct. However, I notice I accidentally removed a blank line. Let me fix that:
- Assistant
Good. Now let me verify the final diff:
- Assistant
The diff looks clean. Let me run all the relevant tests one more time to make sure everything still passes:
- Assistant
All 639 tests pass with 180 skipped (all skipped tests are JSDOM-only). Let me now also write a quick script to verify the fix works for the specific scenario described in the issue:
- Assistant
Let me check how to properly run tests in this repository:
- Assistant
Let me look at how existing tests are structured:
- Assistant
Let me look at how tests are structured in the existing test files for detached triggers:
- Assistant
Let me check if there are existing tests for focus return in the dialog tests:
- Assistant
Let me look at how focus return is tested in the existing detached-triggers test:
- Assistant
Let me check if there's an existing test for the specific scenario in the issue - opening programmatically and verifying focus returns correctly:
- Assistant
Let me look at the test that opens programmatically to see if it checks focus return:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.