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 start by understanding the repository structure and the issue. The issue is about focus management in Alert Dialog, Dialog, and Popover components when using multiple triggers in controlled mode. When closing the dialog/popover opened via "Open programmatically" button, focus should return to that button, not to a different trigger.
Let me explore the repository structure first.
Assistant
Let me explore the packages directory to find the relevant components.
Assistant
Let me look at the react package.
Assistant
Let me look at the source directory to find the dialog, alert-dialog, and popover components.
Assistant
Let me explore the dialog, alert-dialog, and popover components to understand their structure.
Assistant
Let me look at the dialog component.
Assistant
Let me explore the dialog root and trigger files
Assistant
Let me look at the main dialog root files.
Assistant
Now let me look at the core logic in useDialogRoot.ts:
Assistant
Let me look at the popups utility file - this likely contains the focus management logic:
Assistant
Let me look at the files in this directory.
Assistant
Let me look at popupStoreUtils.ts which seems to have the key logic:
Assistant
Let me look at the trigger focus guards and the store:
Assistant
Let me look at the store and popupTriggerMap:
Assistant
Let me look at the floating-ui-react dismiss hook:
Assistant
Let me look at the useDismiss hook which likely manages focus return:
Assistant
Let me look at the enqueueFocus utility and the FloatingFocusManager:
Assistant
Let me look at the FloatingFocusManager component:
Assistant
Let me search for how
domReferenceis used in relation to the dialog/popover componentsAssistant
Let me search for where the reference element is set in the FloatingRootStore
Assistant
Let me look at the DialogStore:
Assistant
Let me look at the DialogStore:
Assistant
Let me look at the DialogHandle:
Assistant
Let me look at the dialog trigger component:
Assistant
Let me look at DialogTrigger.tsx:
Assistant
Let me look at the detached triggers test file to understand how this is supposed to work:
Assistant
Let me look at the useRenderDialogRoot file:
Assistant
Let me look at the dialog popup component:
Assistant
Let me look at how useSyncedFloatingRootContext sets the reference element:
Assistant
Let me look at how
useControlledPropworks in the store:Assistant
Let me look at the store's
useControlledPropimplementation:Assistant
Let me look at ReactStore implementation:
Assistant
Let me also look at the Popover component to understand the similar structure
Assistant
Let me search for focus-related tests to understand the expected behavior better
Assistant
Let me search for focus return tests in the dialog test
Assistant
Let me look at the "returns focus to the menu trigger when a detached dialog trigger unmounts" test
Assistant
Let me run the existing dialog tests to understand the current
Assistant
Let me find and examine
useOpenMethodTriggerProps:Assistant
Let me look at the Popover store:
Assistant
Let me look at the PopoverStore:
Assistant
Let me look at the FloatingFocusManager more carefully, specifically how it accesses
openInteractionType:Assistant
Let me check the Popover popup to see if it also uses FloatingFocusManager:
Assistant
Let me look at the full
getReturnElementfunction and its surrounding context:Assistant
Let me check the imports in FloatingFocusManager.tsx:
Assistant
Now I have a clear understanding of the issue and the fix. Let me implement it.
The issue is in
FloatingFocusManager'sgetReturnElement()function. WhenreturnFocusistrue(default), it always returnsdomReference(the active trigger element) if connected, even when the dialog/popover was opened programmatically via a different element. The fix is to prefer the previously focused element overdomReferencewhen the dialog was opened programmatically (i.e.,openInteractionTypeis falsy).Assistant
Now let me edit the
getReturnElementAssistant
Let me open the file first.
Assistant
Now I can make the edit to the
getReturnElementfunction:Assistant
Let me verify which components use FloatingFocusManager:
Assistant
Let me check how each component passes
openInteractionType:Assistant
Let me check SelectPopup and MenuPopup for openInteractionType:
Assistant
Let me check how MenuPopup passes props to FloatingFocusManager
Assistant
Let me check if MenuStore has
openMethod:Assistant
Let me search in the menu directory recursively:
Assistant
Let me check how SelectPopup passes props to FloatingFocusManager
Assistant
Now let me run the FloatingFocusManager tests and dialog tests to make sure nothing is broken
Assistant
Let me look at the failing test:
Assistant
Let me find the full test context
Assistant
Let me check what FloatingContext looks like:
Assistant
Let me look at the FloatingContext type:
Assistant
Let me check how
useFloatingsets up the FloatingRootStore:Assistant
Let me look at the full useFloating hook:
Assistant
Let me verify my change is correct:
Assistant
Let me run the failing test with more detail:
Assistant
Let me create a debug script to understand the issue:
Assistant
Let me check if this test passes without my change by reverting temporarily:
Assistant
Let me check if this test passes without my change:
Assistant
Let me check what
useTestInteractionsdoes:Assistant
Let me find and check
useTestInteractionsAssistant
Let me check the useTestInteractions implementation:
Assistant
Let me check
useFloatingRootContext:Assistant
The issue might be that
previouslyFocusedElementsis module-level state shared across tests. Let me check if this is causing the failure:Assistant
I've identified the issue with my approach - the module-level
previouslyFocusedElementsis shared across all FloatingFocusManager instances. Let me revert and try a different approach. I'll track the previously focused element locally.Assistant
Let me check if useClick toggles or only opens:
Assistant
Let me check if there's proper cleanup between tests:
Assistant
Let me check for cleanup in the test:
Assistant
Let me check what
createRendererdoes:Assistant
Let me look at createRenderer.ts:
Assistant
Let me take a refined approach. I'll modify the FloatingFocusManager to only apply the programmatic-open logic when
openInteractionTypeis explicitlynull(which is what Dialog/Popover pass), not when it's the default''(which is what happens when not specified):Assistant
The file is already open. Let me look at the current state:
Assistant
I see that
openInteractionTypecan't distinguish between programmatic and not-specified. Let me take a different approach. I'll modifyuseSyncedFloatingRootContextto check the store'sopenMethodstate directly, which indicates whether the popup was opened via trigger interaction or programmatically.Assistant
Now I'll modify
useSyncedFloatingRootContextto skip syncingdomReferenceElementwhen the popup was opened programmatically (openMethod is null):Assistant
Now let me edit the
useSyncedFloatingRootContextto conditionally syncdomReferenceElement:Assistant
Let me verify which code paths use `useSyncedFloatingRoot
Assistant
My change only affects components that use
usePopupStore→useSyncedFloatingRootContext(Dialog, Popover, Menu, etc.), not direct FloatingFocusManager usage. Let me run the tests:Assistant
The previously failing test passes now. Let me run the full test suites to ensure nothing is broken:
Assistant
All FloatingFocusManager tests pass. Let me run the related tests:
Assistant
Now let me run the Dialog and Popover tests:
Assistant
All dialog tests pass. Let me run the Popover tests:
Assistant
Let me check if these failures are pre-existing by reverting my change and running the popover tests:
Assistant
My change introduces 14 popover test failures. Let me investigate the specific failures:
Assistant
Let me look at a specific failing test:
Assistant
Let me look at the failing test:
Assistant
Let me run a single failing test to see the full error:
Assistant
Let me look at the useClick implementation to understand the click flow:
Assistant
I see the issue - my change to
useSyncedFloatingRootContextbreaksuseClickbecause it relies ondomReferenceElementto detect clicks on different triggers. Let me revert and take a different approach. I'll modifyFloatingFocusManagerto capture the previously focused element locally.Assistant
Now I understand the full picture. Let me implement a clean fix. I'll modify
FloatingFocusManagerto:- Capture the previously focused element in a local ref (per-instance)
- In
getReturnElement, whenopenInteractionTypeisnull(programmatic open), prefer the locally captured previously focused element over `domReference
Assistant
Let me implement the fix. I'll add a local ref to track the previously focused element and modify
getReturnElement:Assistant
The edit has issues with the braces. Let me fix the structure properly:
Assistant
Let me look at the component structure to find where to add the ref:
Assistant
Let me find where
addPreviouslyFocusedElementis called in the effect:Assistant
Let me see the context around line 733:
Assistant
Now I'll implement the fix step by step. First, add the ref:
Assistant
I see there are duplicate declarations. Let me restore and be more careful:
Assistant
Let me view the exact ref definitions more carefully:
Assistant
Now let me add the ref properly:
Assistant
There's a duplicate line. Let me restore and use a more precise edit:
Assistant
Let me verify the exact lines:
Assistant
Let me use the replace command which is more precise:
Assistant
Let me use the heredoc syntax:
Assistant
Now let me store the previously focused element in our ref. Let me find the exact line:
Assistant
Now let me add the ref assignment:
Assistant
Let me navigate to that line:
Assistant
Now I'll make both edits. First, store the previously focused element:
Assistant
Now modify
getReturnElement:Assistant
Let me use heredoc syntax:
Assistant
Now let me run the tests to verify everything works:
Assistant
FloatingFocusManager tests pass. Now let me run the Popover tests:
Assistant
All Popover tests pass now! Let me also run the Dialog tests:
Assistant
All Dialog tests pass. Let me run the remaining related tests:
Assistant
All tests pass. Let me run the full test suite to make sure nothing is broken:
Assistant
All unit tests pass (6198 passed). The only 2 failures are e2e tests that fail due to missing browser - not related to my change.
Let me verify my changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.