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.
Interface
No new interfaces are introduced.
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
I'll start by exploring the repository structure to understand the codebase and find the relevant files for the focus restoration issue.
Assistant
I'll explore the repository structure first.
Assistant
I need to make single tool calls. Let me explore the dialog directory first.
Assistant
Now let me look at the utils/popups directory which seems to contain the shared popup logic.
Assistant
Now let me look at the store and the popupTriggerMap to understand how triggers are tracked.
Assistant
Now let me find where the focus is being restored after closing the dialog. Let me search for focus management code.
Assistant
The key file is FloatingFocusManager.tsx. Let me look at how it's used and how
returnFocusis passed.Assistant
Now let me look at how
domReferenceElementis synced fromactiveTriggerElement.Assistant
So here's the key. The
useSyncedFloatingRootContextsyncsactiveTriggerElementtodomReferenceElementin the FloatingRootStore. The FloatingFocusManager usesdomReferenceElementto return focus.So when there are multiple triggers, and the dialog is opened programmatically (e.g. with a button that calls
store.setOpen(true)), theactiveTriggerElementmay not be set to the correct trigger.Let me understand the scenario better. The issue is: when you open the dialog programmatically (via "Open programmatically" button that is NOT a trigger, it just calls setOpen), then the
activeTriggerElementis set to a different trigger (the first registered one, or the implicit one).Let me look at the docs examples to understand the scenario.
Assistant
So the scenario is:
- There are 3 triggers (trigger-1, trigger-2, trigger-3)
- There's a separate "Open programmatically" button that sets
triggerIdto 'trigger-2' andopento true. - When the popover opens, focus should be returned to trigger-2 (the "active trigger").
But the issue says focus goes back to the wrong trigger. Let me look at the alert dialog demo to understand better.
Assistant
Let me look at the DialogStore to understand how setOpen works.
Assistant
Let me look at how the dialog popup uses the FloatingFocusManager and the returnFocus prop.
Assistant
Let me look at the experiment file that was mentioned and the dialog tests to understand the scenario better.
Assistant
Let me look at the existing tests for detached triggers to understand the test patterns.
Assistant
Let me search for existing focus-return tests to understand the patterns used.
Assistant
Now let me write a reproduction test to confirm the issue. Let me first check how to run a single test file.
Assistant
Let me look at the test-utils and createRenderer.
Assistant
Let me check the DialogPopup.test.tsx focus tests setup to understand if they run in jsdom.
Assistant
These tests run in jsdom. Let me look at how the focus tests are set up - what describe block they are in. Let me look at the focus-related tests.
Assistant
These focus tests run in jsdom. Let me create a temporary reproduction test.
Assistant
The Escape key didn't close the dialog. Let me look at how existing tests handle closing and look at the finalFocus test more.
Assistant
Let me rewrite the test to use the Close button and add a Backdrop, matching existing test patterns.
Assistant
The dialog is controlled but I forgot
onOpenChange. Let me fix the test.Assistant
I've reproduced the issue. Focus returns to the "Two" button (trigger-2) instead of the "Open programmatically" button. Now let me understand the FloatingFocusManager's focus tracking order.
Assistant
Let me look at the initial focus effect that moves focus when the dialog opens.
Assistant
Let me look at how
openMethodis set and how the trigger sets it.Assistant
Let me look at the DialogTrigger to understand how it handles focus and openMethod.
Assistant
Let me check the git history of the FloatingFocusManager to understand what Base UI customized.
Assistant
Let me look at the test that was added in that commit to understand the "reference disconnects" case.
Assistant
Let me look at the useButton hook and useClick to understand focus behavior on trigger click.
Assistant
Let me look at the DialogHandle to understand the handle.open() API and how it sets openMethod.
Assistant
Let me check the
useValueAsRefhook to understand when the ref is updated.Assistant
Let me look at the popover trigger and how it handles focus, and check for any focus-on-click logic.
Assistant
Now I understand the architecture. Let me look at the exact section of the FloatingFocusManager I need to modify.
Assistant
Now I'll implement the fix. First, let me capture the open interaction type at mount time.
Assistant
No python. Let me use the
editcommand instead.Assistant
Now let me make the edits. First, capture the open interaction type at mount:
Assistant
Now let me modify the
getReturnElementfunction to use the captured interaction type:Assistant
I accidentally removed the closing brace of
getReturnElement. Let me fix that:Assistant
The structure looks correct now. Let me run the reproduction test:
Assistant
The reproduction test now passes. Focus returns to the "Open programmatically" button. Now let me run the existing dialog tests to make sure I didn't break anything.
Assistant
All dialog tests pass. Now let me run the popover, alert-dialog, and other popup tests that use the FloatingFocusManager.
Assistant
All passing. Now let me run the other components that use FloatingFocusManager: menu, select, combobox, tooltip, preview-card, drawer.
Assistant
All passing. Let me run the floating-ui-react tests directly, plus the utils/popups tests.
Assistant
There's a failing test. Let me look at it.
Assistant
Let me look at the full test to understand the setup. This is a non-modal focus manager (modal={false}). It uses
disabled={!isOpen}and opens viafireEvent.click(reference). There's noopenInteractionTypepassed. SoopenInteractionTypedefaults to''(empty string).Let me look at the broader test setup.
Assistant
The failing test uses
FloatingFocusManagerdirectly withoutopenInteractionType(defaults to''). My check treated''as programmatic. Let me examine how Base UI components passopenInteractionTypeto understand the right signal.Assistant
The Menu and Select don't pass
openInteractionType(defaults to''). So I need to treat''(default) as "trigger interaction" (old behavior), and onlynullas "programmatic". Let me check the combobox's openMethod handling.Assistant
I see - Menu and Select don't pass
openInteractionType(defaults to''), and direct FloatingFocusManager usage in tests also defaults to''. The Base UI components passopenMethodwhich isnullwhen programmatic. So I should treatnullas programmatic, and everything else (including'') as a trigger interaction. Let me update the check:Assistant
Now let me re-run the FloatingFocusManager tests and the reproduction test:
Assistant
Both pass now. Let me re-run the full set of affected component tests to make sure everything is consistent:
Assistant
All tests pass. Now let me verify the fix handles the popover and alert-dialog scenarios too, and check edge cases. Let me look at the PopoverPopup's FloatingFocusManager usage first.
Assistant
The popover passes
returnFocus={finalFocus}(default true) andopenInteractionType={openMethod}. My fix should apply. Let me write more comprehensive tests covering popover, alert-dialog, thehandle.open()case, and the edge case where the originating control unmounts.Assistant
All 4 tests pass. Now let me test the popover scenario, which may be non-modal by default. Let me check the popover's modal default.
Assistant
The popover is non-modal by default. Let me write a popover test mirroring the docs demo to verify the fix works there too.
Assistant
The popover test passes too. Now let me test the edge case where the originating control unmounts while the dialog is open (focus should fall back gracefully).
Assistant
All 5 tests pass, including the edge case. Let me verify the fix is actually responsible by temporarily reverting it and confirming the tests fail. Let me use git stash.
Assistant
The fix is confirmed responsible - without it, 3 programmatic-open tests fail. With it, all pass. The disconnect fallback case passes both ways (returns to trigger).
Now let me clean up the temporary test files (I must not add new tests to the repository).
Assistant
Now let me review the final diff of my change.
Assistant
The diff is clean. Let me verify TypeScript compiles correctly for the react package.
Assistant
TypeScript compiles cleanly. Let me run a final comprehensive test of all affected components.
Assistant
All tests pass (3006 passed, 0 failed). Let me run eslint on the modified file to ensure no lint issues.
Assistant
There's a lint error - the effect is missing the
openInteractionTypeRefdependency. This is consistent with the existing pattern (the initial focus effect already includesopenInteractionTypeRef, andreturnFocusRefis in this effect's deps). Let me add it:Assistant
Now let me re-run eslint and the FloatingFocusManager tests to confirm:
Assistant
Lint passes now. Let me run the FloatingFocusManager tests and the dialog tests once more to confirm nothing regressed with the dep change:
Assistant
All pass. Let me run prettier to verify formatting, then do a final review of the complete diff.
Assistant
Let me verify the Drawer also sets openMethod via its trigger (so the null signal is consistent across all popup components).
Assistant
The DrawerTrigger is an alias for DialogTrigger, so it sets openMethod the same way. All Base UI popup components (Dialog, Popover, Combobox, Drawer) pass
openMethodwhich isnullwhen programmatic. Let me do a final check for any other FloatingFocusManager usages I might have missed.Assistant
All usages covered. Let me do one final broad test run including toolbar (which nests menus) to be thorough.
Assistant
All tests pass. Let me do a final verification of the git status to ensure only the intended file is modified.
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
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →