Skip to content

[dialog] Return focus inside the parent modal when a nested trigger is removed - #6047

Closed
FelixFangX wants to merge 3 commits into
mui:masterfrom
FelixFangX:fix/nested-dialog-focus-return
Closed

FelixFangX wants to merge 3 commits into
mui:masterfrom
FelixFangX:fix/nested-dialog-focus-return

Conversation

@FelixFangX

Copy link
Copy Markdown

Fixes #6037

Cause

When a popup closes and both its trigger and the element focused before it opened are disconnected, getReturnElement in FloatingFocusManager falls back to the global focus history via getPreviouslyFocusedElement(). For a nested dialog whose trigger was removed, the newest connected entry is the parent dialog's trigger, which sits behind the still-open parent modal. The parent's restoreFocus handling only acts when focus lands on body, so nothing corrects it.

Fix

  • The disconnected fallback calls getPreviouslyFocusedElement(true), so it skips elements marked data-base-ui-inert (outside an open modal) or hidden.
  • If nothing eligible is left, focus goes to the enclosing modal's popup. An open modal FloatingFocusManager provides its floating element through a small context for this. Dialogs don't register floating tree nodes, so the tree can't be used to find the parent here.
  • The return target is now resolved inside the queued focus callback instead of synchronously in the cleanup. At cleanup time the closing popup's own markOthers markers are still applied, so the inert filter would also skip valid background elements when a top-level modal closes (this broke the existing "returns focus to last connected element" test).

If the trigger is still mounted, nothing changes: focus returns to it as before.

Tests

Added two cases to dialogRootSharedTests, so they run for both Dialog and AlertDialog nested in a Dialog:

  • closing the nested dialog and removing its trigger returns focus to the parent popup, not the parent trigger (fails on master, focus lands on "Open settings")
  • the control case: with the trigger kept mounted, focus returns to it

Ran pnpm test:jsdom and pnpm test:chromium for FloatingFocusManager, Dialog, AlertDialog, Popover, Menu, Drawer, Select and Combobox (plus Tooltip, NavigationMenu and PreviewCard in jsdom). All pass. pnpm typescript, ESLint and Prettier pass on the changed files.

#5537 reworks focus return more broadly. This is a smaller fix for the current code.

@pkg-pr-new

pkg-pr-new Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

commit: 520cd6e

@code-infra-dashboard

code-infra-dashboard Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+190B(+0.04%) 🔺+41B(+0.02%)

Details of bundle changes

Performance

No regressions in 36 benchmarks · ran 4m 30s · details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 520cd6e
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ae1c3fdf283f6ccdbd38d4d
😎 Deploy Preview https://deploy-preview-6047--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@FelixFangX
FelixFangX force-pushed the fix/nested-dialog-focus-return branch 2 times, most recently from 0104e16 to 5f7d85e Compare October 27, 2026 02:49
@zannager zannager added the component: dialog Changes related to the dialog component. label Oct 27, 2026
@FelixFangX
FelixFangX force-pushed the fix/nested-dialog-focus-return branch from 5f7d85e to e939121 Compare October 28, 2026 02:43
…s removed

When a nested dialog closed and both its trigger and the element focused before it opened were gone, the focus manager fell back to the global focus history, which could pick an element behind the still-open parent modal. The fallback now skips elements marked inert by an open modal, and falls back to the enclosing modal's popup when nothing eligible is left. The return target is resolved when the queued focus runs, after the closing popup's own inert markers are removed.

Fixes #6037
@FelixFangX
FelixFangX force-pushed the fix/nested-dialog-focus-return branch from e939121 to 85bbee8 Compare October 28, 2026 08:08
@FelixFangX

Copy link
Copy Markdown
Author

Closing for now: open for 5+ days with no maintainer follow-up. Happy to reopen if you want this change.

@FelixFangX FelixFangX closed this Oct 28, 2026
@atomiks atomiks reopened this Oct 28, 2026
@atomiks

atomiks commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Sorry for the silence on this one, and thanks for the fix! I pushed two commits on top:

  • When no return target is left, focus now goes to the enclosing modal popup itself instead of its first tabbable element. This matches Dialog's restoreFocus="popup".
  • The inert-filtered fallback now only applies inside an open modal parent. With a non-modal parent, focus was being lost to body. The parent element is also read through a ref, so a change to the parent's modality no longer re-runs an open child's return-focus effect.

Both come with regression tests. Reopening so we can land it.

@FelixFangX

Copy link
Copy Markdown
Author

Closing to reduce open external PR noise. Happy to reopen if maintainers want this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: dialog Changes related to the dialog component.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[dialog] Closing a nested AlertDialog returns focus outside the parent modal when its trigger is removed

3 participants