Skip to content

fix(Dialog): leave focus in another layer when a kept-mounted dialog finishes closing - #3254

Merged
zernonia merged 1 commit into
v2from
fix/kept-mounted-modal-focus-reclaim
Nov 12, 2026
Merged

zernonia merged 1 commit into
v2from
fix/kept-mounted-modal-focus-reclaim

Conversation

@zernonia

@zernonia zernonia commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Part of #3248 (item "a kept-mounted modal pulls focus back when another layer opens").

Repro

Checked in real Chromium with trusted input, on v2 at d36c6a3.

The case as written in the issue does not reproduce: a Dialog or AlertDialog kept mounted with unmountOnHide: false or forceMount, never opened or opened and closed, leaves focus alone when a Popover, DropdownMenu or second Dialog opens, and focus returns to that layer's trigger when it closes.

What does reproduce is the same focus grab one step earlier, while the kept-mounted dialog is finishing its close:

  1. The dialog has an exit animation. It is closed, another layer opens before the animation ends, and when it ends focus jumps to the dialog's trigger.
  2. Another layer is open beside the dialog (its trigger takes pointer events, or the dialog ignores outside interaction) and the dialog is closed programmatically. Focus jumps to the dialog's trigger.

If the other layer is a non-modal Popover it then closes, because focus moved outside it. A modal DropdownMenu or modal Dialog pulls focus back through its own trap, so those cells passed before as well.

Cause

A kept-mounted content never unmounts its focus scope, so closeAutoFocus does not run on close. DialogContentModal.vue and DialogContentNonModal.vue each return focus to the trigger from a watcher on present instead. #2741 guarded the closeAutoFocus handlers with hasFocusLeftScope, but not these two watchers, which focused the trigger unconditionally.

Fix

Both watchers skip the focus return when hasFocusLeftScope reports that focus is on an element outside the dialog and the layers nested in it. This is the helper #2741 added; nothing new is introduced. AlertDialog renders DialogContent, so it is covered by the same change.

Unchanged: a kept-mounted dialog that closes on its own (Escape, close button) returns focus to its trigger, closeAutoFocus is still emitted when the kept-mounted content is finally unmounted, and a dialog that unmounts on close does not go through these watchers at all.

Verification

  • New Dialog/DialogKeptMounted.browser.test.ts, 108 tests (6 skipped: an AlertDialog does not close on an outside click). Modal Dialog, non-modal Dialog and AlertDialog, each with unmountOnHide: false and with forceMount, against a Popover, a DropdownMenu and a second Dialog: never opened, opened then closed, other layer opened during the exit animation, closed by the click that opens the other layer, closed programmatically while the other layer is open. Each asserts focus is in the new layer, stays there, and returns to that layer's trigger on Escape. Without the fix 10 fail (the Popover cells of the two scenarios above: 8 modal Dialog / AlertDialog, 2 non-modal Dialog); with it all pass.
  • Browser files for Dialog, Drawer, FocusScope, DismissableLayer, Presence, Popover, DropdownMenu: 182 passed, 6 skipped.
  • jsdom files for the same families: 306 passed. type-check, eslint on the changed files, build.
  • size-limit: Dialog 11.24 kB of 25, Popover 20.98 kB of 21, Tooltip 18.93 kB of 19, full library 168.08 kB of 170.

Not covered

  • Drawer. Not changed. A modal DrawerContent with forceMount is not a focus-return problem but a larger one: while closed it still acts as an open layer. On mount it moves focus to its first tabbable element, and body keeps pointer-events: none, so nothing else on the page can be clicked. It does not pass its presence to the focus scope or the dismissable layer the way DialogContent does. Fixing that is a change to Drawer's own behaviour and is left for a separate decision.
  • closeAutoFocus is not emitted when a kept-mounted dialog closes, only when it unmounts. This is the existing behaviour and is not changed here.
  • A Dialog that opens while focus is trapped in another Dialog records the trapped element as its trigger, so closing it returns focus there. Seen while writing the tests; unrelated to kept-mounted content.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved focus restoration when dialogs close, preventing focus from returning to the dialog trigger if the user has moved focus elsewhere or interacted outside the dialog.
    • Preserved appropriate focus behavior when closed dialogs remain mounted or are animating out.

…finishes closing

A Dialog or AlertDialog content that stays mounted while closed
(`unmountOnHide: false`, or `forceMount`) returns focus to its trigger from a
watcher on its presence, because its focus scope never unmounts. That watcher
moved focus unconditionally, so it took focus from a layer that had opened in
the meantime: one opened while the dialog was still animating out, or one that
was open beside the dialog when it was closed programmatically. A non-modal
Popover then closed again, since focus had moved outside it.

The watcher now skips the focus return when `hasFocusLeftScope` says focus is
on an element outside the dialog and the layers nested in it, the same check
the unmount path already uses.

Part of #3248

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Modal and non-modal Dialogs now restore trigger focus after closing only when focus has not left the dialog content scope. New browser tests cover kept-mounted Dialog and AlertDialog content, interactions with other layers, and focus return during closing and unmounting.

Changes: Dialog focus restoration

Layer / File(s) Summary
Conditional focus restoration and coverage
packages/core/src/Dialog/DialogContentModal.vue, packages/core/src/Dialog/DialogContentNonModal.vue, packages/core/src/Dialog/DialogKeptMounted.browser.test.ts
The modal and non-modal presence watchers check whether focus left the content scope before restoring trigger focus. Browser tests cover kept-mounted content, interactions with popovers, menus, and dialogs, close behavior, and closeAutoFocus on unmount.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix

Possibly related issues

  • unovue/reka-ui#3248 — Describes kept-mounted modal Dialog content taking focus from another layer, which this change addresses.

Possibly related PRs

  • unovue/reka-ui#2741 — Introduces the hasFocusLeftScope helper used by the presence watchers.
  • unovue/reka-ui#3169 — Changes the return-focus target in the same presence watchers.
  • unovue/reka-ui#3082 — Changes how triggerElement is selected, affecting the target used by the focus restoration path.
Merge Risk: Minimal · up to 24a58

Dialogs kept mounted while closed no longer take focus from another layer that opens as they close. Focus still returns to the trigger on self-close. No actionable merge risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the fix (kept-mounted Dialog no longer takes focus from another layer when it finishes closing), which matches the main change: the presence watchers in DialogContentModal.vue and Dial…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the fix (kept-mounted Dialog no longer takes focus from another layer when it finishes closing), which matches the main change: the presence watchers in DialogContentModal.vue and DialogContentNonModal.vue now skip focus restoration when focus has left the dialog scope. The title is specific and scoped to Dialog, though it does not mention AlertDialog, which the PR also covers through DialogContent.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between cf0e57b and 24a5869.

Files selected for processing: 3

  • packages/core/src/Dialog/DialogContentModal.vue
  • packages/core/src/Dialog/DialogContentNonModal.vue
  • packages/core/src/Dialog/DialogKeptMounted.browser.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 2 remain after this review.

@pkg-pr-new

pkg-pr-new Bot commented Nov 12, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/reka-ui@3254

commit: 24a5869

@zernonia
zernonia merged commit 618614b into v2 Nov 12, 2026
9 checks passed
@zernonia
zernonia deleted the fix/kept-mounted-modal-focus-reclaim branch November 12, 2026 13:15
praburangki pushed a commit to vinicunca/akar that referenced this pull request Nov 16, 2026
…finishes closing (unovue/reka-ui#3254)

A Dialog or AlertDialog content that stays mounted while closed
(`unmountOnHide: false`, or `forceMount`) returns focus to its trigger from a
watcher on its presence, because its focus scope never unmounts. That watcher
moved focus unconditionally, so it took focus from a layer that had opened in
the meantime: one opened while the dialog was still animating out, or one that
was open beside the dialog when it was closed programmatically. A non-modal
Popover then closed again, since focus had moved outside it.

The watcher now skips the focus return when `hasFocusLeftScope` says focus is
on an element outside the dialog and the layers nested in it, the same check
the unmount path already uses.

Part of unovue/reka-ui#3248

Co-authored-by: Claude <[email protected]>
Upstream-Commit: 618614b7b2a4d0d1d643b54e9d729e0b01e5e551
zernonia added a commit to kricsleo/reka-ui that referenced this pull request Nov 19, 2026
…-hide

Conflicts resolved:
- Popper/PopperContent.vue: keep v2's `scope` prop next to the internal `present` prop.
- Popover/PopoverContentImpl.vue: keep v2's useContentId/useContentMounted and `scope`.
- Popover/PopoverContentModal.vue: keep v2's prevented closeAutoFocus handling (unovue#3283) and
  hasFocusLeftScope check (unovue#3254) next to the `present` gated scroll lock and hideOthers.
- Dialog/Dialog.test.ts, Dialog/DialogContentImpl.vue: take v2. The suppression of
  closeAutoFocus on the final unmount of kept-mounted content is dropped: a kept-mounted
  layer must still fire it when it is unmounted (DialogKeptMounted.browser.test.ts).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant