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.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /base-ui, current file: none) bash-$
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 "Open programmatically" buttons with multiple triggers. When closing the dialog, focus should return to the control that triggered it, not some other trigger.
Let me explore the repository structure first.
Assistant
This is a monorepo. Let me explore the packages directory to find the relevant components.
Assistant
Let me look at the react package to find the components.
Assistant
Let me find the Alert Dialog, Dialog, and Popover components.
Assistant
Let me look at the dialog component structure first, then alert-dialog and pop
Assistant
Let me look at the relevant source files to understand the focus management
Assistant
Let me look at the popup utilities, which seem to be central to the focus management
Assistant
Let me look at the popups directory more closely.
Assistant
Let me look at the popups index and the popupStoreUtils to understand the focus management functions.
Assistant
Let me look at the popupStoreUtils file as it likely contains the key functions.
Assistant
Now let me look at the store.ts file to understand the state management
Assistant
Now let me look at the popupTriggerMap to understand how trigger elements are stored.
Assistant
Let me look at the dialog trigger component to understand how triggers are registered and how focus return works.
Assistant
Let me look at the DialogRoot test file for detached triggers and the main DialogRoot component
Assistant
Let me look at the DialogRoot component itself
Assistant
Let me look at the useRenderDialogRoot hook
Assistant
Now let me look at the DialogStore to understand how setOpen works
Assistant
Let me look at the floating-ui-react useDismiss hook, which likely handles focus
Assistant
Let me look at the useDismiss hook to understand how focus is returned
Assistant
Let me look at FloatingFocusManager, which likely handles focus
Assistant
Let me look at the FloatingRootStore to understand how domReference is managed
Assistant
Let me look at the useSyncedFloatingRootContext hook
Assistant
Let me look at how
domReferenceis used in FloatingFocusManagerAssistant
Let me search for the controlled mode examples in the docs
Assistant
Let me look at the docs for the dialog component
Assistant
Let me look at how the popover and alert-dialog components handle things similarly
Assistant
Let me look at the popover store to see if there's any
Assistant
Let me check the PopoverStore to see its setOpen.
Assistant
Let me now look at the FloatingFocusManager tests for
Assistant
Let me look at the test files for dialog/popover to understand how focus return is tested
Assistant
Let me look at specific parts of FloatingFocusManager.tsx to understand the full
getReturnElementfunction and the return focus logicAssistant
Let me look at the test around line 1695
Assistant
Let me continue reading this test.
Assistant
Now I have a clear understanding. Let me create a reproduction script first to verify the issue.
Assistant
Let me now look at the full context of the
getReturnElementfunction and surrounding logic to make sure I understand the change correctly.Assistant
Now I understand the issue clearly. The
getReturnElement()function inFloatingFocusManager.tsxalways prioritizesdomReference(the active trigger element) overgetPreviouslyFocusedElement()(the element that was focused before the dialog opened). When opening a dialog programmatically via a non-trigger button, the previously focused element is that button, but focus returns to the active trigger instead.Let me implement the fix:
Assistant
The open command requires only a file argument. Let me use the correct format.
Assistant
Let me navigate to the relevant area
Assistant
Now let me make the fix. I need to change the
getReturnElementfunction to prioritize the previously focused element overdomReferenceAssistant
Let me use the heredoc syntax for the multi-line
Assistant
Let me verify the change by looking at the
Assistant
Now let me run the existing tests related to FloatingFocusManager to make sure nothing is broken.
Assistant
One test failed. Let me look at the failing test to understand why
Assistant
Let me look at the test setup more carefully
Assistant
Let me look at the useClick implementation
Assistant
Let me look at the useClick toggle logic more carefully
Assistant
Let me check the useTestInteractions utility
Assistant
Let me look at the useTestInteractions implementation to understand how it works.
Assistant
Let me check if this test was already failing before my change by reverting temporarily
Assistant
So the test passes without my change. Let me restore my change.
Assistant
Let me check the
enqueueFocusutility to understand the timing of focus movesAssistant
Let me check the isMouseLikePointerType utility
Assistant
Let me check what
isMouseLikePointerTypereturns forundefinedAssistant
Let me add some debugging to understand what's happening in the test
Assistant
Let me verify the change is correctly applied
Assistant
Let me run the failing test with both old and new code multiple times to see if it's a
Assistant
Old code passes. Let me restore my change and re-run.
Assistant
Let me look at how DialogPopup uses FloatingFocusManager
Assistant
Let me look at how DialogStore handles
openPropchangesAssistant
Let me look at
useControlledPropAssistant
Let me add some debug logging to understand the difference
Assistant
Let me check how useFloating creates the root
Assistant
Let me check the useFloatingRootContext
Assistant
Let me revert my change and try a more targeted approach
Assistant
Let me add logging to the FloatingFocusManager and run the failing test to
Assistant
Let me add temporary logging to understand the behavior
Assistant
Let me add logging to understand the behavior
Assistant
Let me add temporary logging to understand the
Assistant
Let me try a simpler approach - just use replace on the specific lines
Assistant
Let me use the replace command directly
Assistant
Let me run the test with the logging
Assistant
Let me revert the debug logging and apply a clean fix
Assistant
Let me verify whether useClick actually toggles by adding a quick
Assistant
Let me apply the fix with the
!==approach. This prioritizesgetPreviouslyFocusedElement()overdomReferenceonly when they differAssistant
Now let me apply the fix
Assistant
Let me clean up the fallback computation first, then run tests
Assistant
Let me optimize the fallback computation to avoid multiple
Assistant
Let me check the diff and run the tests
Assistant
Now let me run the FloatingFocusManager
Assistant
Only 1 test fails. Let me run the Dialog and Popover tests too
Assistant
All Dialog tests pass. Let me run the Popover tests too.
Assistant
All Popover tests pass. Let me also run the alert-dialog tests
Assistant
Let me check if there's already tracking of previously focused element before initialFocus
Assistant
The issue is that
addPreviouslyFocusedElementis called afterinitialFocushas already moved focus. I need to record the previously focused element before focusing. Let me add recording in the initialFocus effect before it focusesAssistant
Let me find the initialFocus effect
Assistant
Let me look at line 650-660 to find where to add recording
Assistant
I found the issue. The initialFocus effect captures
previouslyFocusedElementbut doesn't add it to the list. Let me add it there.Assistant
Now let me remove the duplicate
addPreviouslyFocusedElementfrom the return-focus effect since it's now handled in the initialFocusAssistant
Let me keep both calls (they're harmless) and run
Assistant
Let me add temporary logging to understand the values
Assistant
Let me use sed to add debug logging
Assistant
Let me run the test with debug logging
Assistant
Let me check if the test renderer uses Strict
Assistant
Let me look at the createRenderer implementation
Assistant
Let me check the shared create
Assistant
I need a smarter condition. Let me also
Assistant
Now I understand the issue better. Let me implement a cleaner fix that distinguishes between the programmatic case and the keepMounted case.
The key difference: in the programmatic case,
previouslyFocusedis outside the floating tree. In the keepMounted case,previouslyFocusedis inside the floating tree (it'schild, which was focused byinitialFocus).Let me implement this
Assistant
Let me implement the fix step by step
Assistant
Now let me also add
addPreviouslyFocusedElementin the initialFocusAssistant
Let me add
addPreviouslyFocusedElementcall in the initialFocusAssistant
Let me use replace instead
Assistant
Let me use sed to insert the line
Assistant
Let me verify the
Assistant
Let me check the full diff and
Assistant
Now let me run the FloatingFocusManager
Assistant
All 49 tests pass, including the previously failing
keepMountedtest. Let me run the broader test suitesAssistant
All 590 tests pass. Let me run a few more test suites that might be
Assistant
All tests pass. Let me verify the final diff
Assistant
Let me clean up the extra blank line
Assistant
Let me verify the final diff one last
Assistant
Let me run the full test suite one final time to make sure everything is solid
Assistant
All 639 tests pass. The fix is clean and minimal. Let me now check the final diff and
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-lunaCompare side by side →