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 the relevant components. Let's first get an overview.
- Assistant
Let me explore the repository structure more to understand how it's organized.
- Assistant
Let me look at the packages directory to find the components mentioned in the issue.
- Assistant
Let me explore the react package to find the AlertDialog, Dialog, and Popover components.
- Assistant
Let me look at the docs examples for the "controlled mode with multiple triggers" to understand the issue better.
- Assistant
Great! I found the relevant demo files. Let me examine them to understand how the "Open programmatically" functionality works. Let's start with the AlertDialog one.
- Assistant
Let me look at the detached triggers controlled demos and the underlying component implementations.
- Assistant
The parentheses in the path are causing issues. Let me quote the path.
- Assistant
Let me find the exact path structure.
- Assistant
Let me look at the demo file.
- Assistant
Let me read the index.ts file first, then look at one of the implementations.
- Assistant
Now let me look at the actual implementation to understand how the "Open programmatically" functionality works. Let me examine the AlertDialog component implementation.
- Assistant
Now I can see the issue. When the "Open programmatically" button is clicked, it sets
triggerIdto'alert-trigger-2'(the Delete button). This means when the dialog closes, focus is returned to the element with idalert-trigger-2instead of the "Open programmatically" button itself.Let me look at the AlertDialog component source code.
- Assistant
Let me examine the AlertDialog.Root component to understand how it handles focus management.
- Assistant
Let me examine the AlertDialogRoot.tsx file.
- Assistant
I see that AlertDialogRoot uses
useRenderDialogRoot. Let me check that hook to understand how it handles focus management. - Assistant
Let me examine useRenderDialogRoot.tsx first, then useDialogRoot.ts to understand the focus management logic.
- Assistant
Now let me look at useDialogRoot.ts to understand how focus is managed when closing.
- Assistant
Now I need to understand how focus is managed when closing. Let me look at the
useOpenStateTransitionshook and theuseImplicitActiveTriggerhook which might be responsible for focus management. Also, let me look at theusePopupInteractionPropsto understand how focus returns to the trigger. - Assistant
Let me look at the popupStoreUtils.ts file which likely contains the focus management logic.
- Assistant
Now I need to understand how focus is returned when the dialog closes. Let me look at the focus management in the floating-ui-react integration.
- Assistant
Let me look at the DialogStore to understand how setOpen works and how triggerId is handled.
- Assistant
Let me examine the DialogStore.
- Assistant
Now I understand the issue. When the "Open programmatically" button is clicked, the demo sets
triggerIdto'alert-trigger-2'(the Delete button). This means that when the dialog closes,activeTriggerIdis set to'alert-trigger-2', so focus returns to the Delete button instead of the "Open programmatically" button.The problem is that the
triggerIdprop in the controlled mode is being used as theactiveTriggerId, but when a button opens the dialog programmatically (by setting open state directly), we need to track which element actually initiated the open action, not just which triggerId the developer passed.Let me look at how the
triggerIdprop is used and howactiveTriggerIdis set. Let me also look at the FloatingFocusManager to understand how focus is returned. - Assistant
Let me search for the focus return logic in FloatingFocusManager, especially how it determines which element to focus on close.
- Assistant
Let me read more of the FloatingFocusManager to understand how it handles focus return.
- Assistant
Now I understand the issue better. Let me look at how the trigger is tracked for focus return when the popup closes. The key is in how
setOpenis called and howactiveTriggerElementis used. - Assistant
Let me understand how focus is returned on close. Let me look at the popup interaction props and focus management in the FloatingFocusManager.
- Assistant
Let me look at the focus return logic in FloatingFocusManager more closely.
- Assistant
Let me understand the flow better. Let me look at how the trigger element is communicated to the floating context and how focus is returned. The key is in the
domReferenceused in the focus manager. - Assistant
Let me look at the full store.ts to understand how
activeTriggerElementis used. - Assistant
Now let me understand how the
activeTriggerElementis used for focus return. Let me look at theuseSyncedFloatingRootContextfunction. - Assistant
Let me look at useSyncedFloatingRootContext.ts
- Assistant
Let me now look at how the DialogRoot handles the
triggerIdprop and how theopenMethodstate is used. - Assistant
Let me look at how
useControlledPropworks in the store system. - Assistant
The
useControlledPropis a store method. Let me look at the store implementation. - Assistant
Let me look at the ReactStore implementation to understand how useControlledProp works.
- Assistant
Let me look at the ReactStore.ts to understand useControlledProp.
- Assistant
Now I need to understand the full flow. Let me look at the Dialog Popup component to see how focus management is set up.
- Assistant
Let me look at the DialogPopup.tsx file.
- Assistant
Now I need to understand the core issue. Let me trace through what happens when:
- A trigger is clicked normally
- The dialog is opened programmatically via the
openprop withtriggerId
Let me look at how
setOpenis called vs how the controlledopenprop changes. - Assistant
Let me look at the existing tests for detached triggers to understand the expected behavior.
- Assistant
Let me look at the full detached triggers test file to see if there's already a test for controlled mode focus return.
- Assistant
Let me look at the full test file to understand the expected behavior for controlled mode with triggers.
- Assistant
Let me look at the specific test for "sets the payload when opening programmatically with a controlled triggerId".
- Assistant
Let me look at how
openMethodis set and how the trigger element is tracked during focus. - Assistant
Let me look at how focus is managed on dialog close. I need to understand the complete flow. Let me look at the trigger component to see how it interacts with the store.
- Assistant
Let me look at the dialog trigger to understand how it opens the dialog.
- Assistant
Now let me understand the Popover implementation too, since it's also affected.
- Assistant
Let me now trace the full focus management path. Let me look at how the
activeTriggerElementis used for focus return. - Assistant
Let me look at the alert dialog popup to understand how it renders.
- Assistant
The AlertDialog doesn't have its own Popup, Backdrop, Close, Title, Description, Portal components - let me check the index to see what parts it exports.
- Assistant
Let me look at the Popover popup implementation as well.
- Assistant
Let me now look at the
useOpenInteractionTypehook to understand how the open method is tracked. - Assistant
Now I have a good understanding of the issue. Let me trace the exact problem. The issue is:
- When "Open programmatically" button is clicked, it sets
open=trueandtriggerId='alert-trigger-2' - The store resolves
activeTriggerElementto the trigger element with that ID (the "Delete" button) - This becomes
domReferenceElementin the floating context - When dialog closes,
FloatingFocusManagerreturns focus todomReference(the "Delete" button) - But focus should go to the "Open programmatically" button that was actually focused
Let me look at the FloatingRootStore to understand how
domReferenceElementis used. - When "Open programmatically" button is clicked, it sets
- Assistant
Now let me look at
resolveRefto understand how refs are used for focus return. - Assistant
Let me now look at the Popover root to understand how it works.
- Assistant
Now I have a thorough understanding of the issue. Let me create a reproduction script first to confirm the problem, then implement the fix.
- Assistant
Let me now look at how the existing tests run and how I can verify my fix. First, let me run the existing detached-triggers tests.
- Assistant
Let me look at the test setup to understand how to run specific tests.
- Assistant
Now I have a thorough understanding of the problem. Let me look at the key focus return logic in FloatingFocusManager.
- Assistant
Now I understand the full picture. The issue is in
getReturnElement()inFloatingFocusManager.tsx. When the dialog is opened programmatically with atriggerId,domReferencepoints to the trigger element identified bytriggerId(e.g., the "Delete" button), not the "Open programmatically" button. ThegetPreviouslyFocusedElement()correctly tracks the "Open programmatically" button, butdomReferencetakes precedence.The fix is to change the priority: prefer
getPreviouslyFocusedElement()overdomReferencewhen returning focus. This is correct because:- For trigger clicks: both point to the same element (the trigger), so no change
- For programmatic opens:
getPreviouslyFocusedElement()is the originating button, which is correct
Let me also check the Popover detached triggers demo to confirm the same pattern:
- Assistant
Let me look at the popover demo too.
- Assistant
Now I have a clear understanding of the issue. The problem is in how
domReferenceElementis set in the floating context. When the dialog is opened programmatically with atriggerId, theactiveTriggerElement(and thusdomReferenceElement) points to the trigger component with that ID, not the element that actually initiated the open. When the dialog closes,FloatingFocusManagerreturns focus todomReferenceElement.The fix is to track the actual originating element (the element that was focused when the dialog opened) and use it for focus return. Let me implement this:
- Add a
focusOriginRefto the popup store context - Capture
document.activeElementwhen the popup opens - Use it as the focus return target
- Add a
- Assistant
Now let me implement the fix. First, let me add
focusOriginReftoPopupStoreContext: - Assistant
Let me open the store.ts file to edit it.
- Assistant
Now let me add the
focusOriginRefto thePopupStoreContexttype: - Assistant
Now let me update the DialogStore to initialize
focusOriginRef: - Assistant
Now I need to add
focusOriginRefto the DialogStore constructor: - Assistant
Now let me update the PopoverStore to initialize
focusOriginRef: - Assistant
I need to add
focusOriginRefto the PopoverStore constructor. Let me edit that section. - Assistant
Now let me check if there are other stores that need updating (tooltip, preview-card):
- Assistant
Let me check the other stores that extend
PopupStoreContext: - Assistant
Let me check the tooltip and preview-card store context types to see if they need updating:
- Assistant
All stores that extend
PopupStoreContextneed thefocusOriginRef. Let me update them all: - Assistant
Let me update the remaining stores. First, the tooltip store:
- Assistant
I need to update the tooltip store to include
focusOriginRef. Let me edit it. - Assistant
Now let me update the preview-card store:
- Assistant
Let me update the preview-card store:
- Assistant
Now let me update the menu store:
- Assistant
I need to add
focusOriginRefto the menu store. Let me look at the constructor to find the right spot. - Assistant
Now I need to implement the core logic. Let me first modify
useRenderDialogRootto capturedocument.activeElementwhen the popup opens, then modify the popup components to use it for focus return. Let me start withuseRenderDialogRoot: - Assistant
Now I need to add logic to capture
document.activeElementwhen the dialog opens. I'll add auseIsoLayoutEffectthat detects the open transition and captures the active element: - Assistant
It seems the edit went to the wrong file. Let me check and fix this. Let me first revert the MenuStore.ts change and then edit useRenderDialogRoot.tsx properly:
- Assistant
The edit command modified the wrong file. Let me restore MenuStore.ts and properly edit useRenderDialogRoot.tsx:
- Assistant
Let me re-add focusOriginRef to the menu store:
- Assistant
Now let me properly edit the
useRenderDialogRoot.tsxfile. Let me open it first: - Assistant
Now I need to add logic to capture
document.activeElementwhen the dialog opens. I'll add auseIsoLayoutEffecthook: - Assistant
Now I need to add the focus origin tracking. Let me add a
useValueChangedcall to capturedocument.activeElementwhen the dialog opens: - Assistant
Now let me check how
useValueChangedworks to make sure I'm using it correctly: - Assistant
Let me look at useValueChanged to make sure I'm using it correctly:
- Assistant
Good,
useValueChangedusesuseIsoLayoutEffectand calls the callback with the previous value. This will correctly capturedocument.activeElementwhenopentransitions fromfalsetotrue.Now I need to modify
DialogPopupandPopoverPopupto usefocusOriginReffor focus return. Let me modifyDialogPopupfirst: - Assistant
Now I need to modify
DialogPopupto usefocusOriginReffor thereturnFocusprop. Let me update the FloatingFocusManager usage: - Assistant
Wait,
returnFocusRefisn't a valid prop on FloatingFocusManager. Let me revert that and use a different approach. I need to modify thegetReturnElement()function in FloatingFocusManager instead: - Assistant
Let me reconsider the approach. Instead of adding a new prop, I should modify the
getReturnElement()function in FloatingFocusManager to prefergetPreviouslyFocusedElement()overdomReference. Let me revert the DialogPopup change and modify FloatingFocusManager instead: - Assistant
Now I understand the full approach. Instead of modifying FloatingFocusManager, I should change how
domReferenceElementis set inuseSyncedFloatingRootContext. When the focus origin is available, it should be used instead ofactiveTriggerElement. Let me updateuseSyncedFloatingRootContext: - Assistant
Now I need to modify this to prefer
focusOriginRef.currentoveractiveTriggerElementfordomReferenceElement: - Assistant
Now I need to also implement the focus origin tracking in the Popover root. Let me find and update it:
- Assistant
Now I need to add the focus origin tracking to the Popover root as well. Let me add it after the
openstate is read: - Assistant
It seems the edit went to the wrong file again. Let me check and fix this:
- Assistant
The file got corrupted. Let me revert both files and start over carefully:
- Assistant
Let me be more careful. I'll open each file explicitly before editing:
- Assistant
Good, the file is back to its original state. Now let me edit it carefully:
- Assistant
Good, that edit worked correctly. Now let me add the focus origin tracking to the Popover root. Let me open it:
- Assistant
Now I need to add the focus origin tracking to the Popover root. Let me first add the import:
- Assistant
I see there's a duplicate import line. Let me fix that:
- Assistant
Good, imports are fixed. Now let me add the focus origin tracking after the
openstate is read: - Assistant
Now let me verify the changes are consistent. Let me check the current state of all modified files:
- Assistant
Let me clean up the unused import in useRenderDialogRoot.tsx:
- Assistant
Let me remove the unused
useIsoLayoutEffectimport: - Assistant
There's a duplicate import line. Let me fix it:
- Assistant
Good, the imports are clean now. Let me now run the existing tests to verify nothing is broken:
- Assistant
Tests pass. Let me also run the main Dialog tests:
- Assistant
Let me check if these test failures are pre-existing by reverting my changes temporarily:
- Assistant
The tests pass without my changes. Let me restore my changes and investigate the failures:
- Assistant
Let me see which specific tests are failing:
- Assistant
Let me see which specific tests fail:
- Assistant
Let me look at the failing test to understand what's happening:
- Assistant
The issue is that my
useSyncedFloatingRootContextchange affectsdomReferenceElementwhich is used for more than just focus return. Let me revert that change and use a different approach - passfocusOriginRefthrough thereturnFocusprop: - Assistant
Let me take a different approach. Instead of modifying
useSyncedFloatingRootContext, I'll modify the popup components to pass a customreturnFocusfunction that usesfocusOriginRef: - Assistant
Let me now modify the DialogPopup to pass a custom
returnFocusfunction that usesfocusOriginRef: - Assistant
There's a duplicate line. Let me fix that:
- Assistant
There's a duplicate line. Let me fix it:
- Assistant
Good. Now I need to update the FloatingFocusManager to use the defaultReturnFocus when finalFocus is not provided:
- Assistant
There's a duplicate closing tag. Let me fix it:
- Assistant
Let me fix the duplicate closing tag issue.
- Assistant
Good. Now let me do the same for PopoverPopup. Let me open it:
- Assistant
Let me add the defaultReturnFocus to PopoverPopup. First, let me see where to add the callback:
- Assistant
Let me add the defaultReturnFocus callback to PopoverPopup:
- Assistant
It seems the file got corrupted again. Let me check:
- Assistant
Let me use a script to make the changes to PopoverPopup.tsx cleanly:
- Assistant
Let me use sed instead:
- Assistant
Let me verify the change and also update the returnFocus prop:
- Assistant
Let me verify the PopoverPopup file:
- Assistant
Good. Now let me run the tests to see if the changes work:
- Assistant
All 86 tests pass. Let me also run the AlertDialog and Popover tests:
- Assistant
All AlertDialog tests pass. Let me also run the Popover tests.
- Assistant
All tests pass. Let me now verify the full diff looks correct:
- Assistant
Let me run the tooltip and menu tests to make sure nothing is broken:
- Assistant
All tests pass. Let me do a final review of all the changes:
- Assistant
The diff looks clean and correct. Let me now run a broader set of tests to make sure nothing is broken:
- Assistant
Let me also run the preview-card tests:
- Assistant
All tests pass. Let me run the menu tests too:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.