Repository navigation
fix(Dialog): leave focus in another layer when a kept-mounted dialog finishes closing - #3254
Conversation
…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]>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
commit: |
…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
…-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]>
Part of #3248 (item "a kept-mounted modal pulls focus back when another layer opens").
Repro
Checked in real Chromium with trusted input, on
v2at d36c6a3.The case as written in the issue does not reproduce: a Dialog or AlertDialog kept mounted with
unmountOnHide: falseorforceMount, 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:
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
closeAutoFocusdoes not run on close.DialogContentModal.vueandDialogContentNonModal.vueeach return focus to the trigger from a watcher onpresentinstead. #2741 guarded thecloseAutoFocushandlers withhasFocusLeftScope, but not these two watchers, which focused the trigger unconditionally.Fix
Both watchers skip the focus return when
hasFocusLeftScopereports 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 rendersDialogContent, 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,
closeAutoFocusis 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
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 withunmountOnHide: falseand withforceMount, 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.Not covered
DrawerContentwithforceMountis 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, andbodykeepspointer-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 wayDialogContentdoes. Fixing that is a change to Drawer's own behaviour and is left for a separate decision.closeAutoFocusis not emitted when a kept-mounted dialog closes, only when it unmounts. This is the existing behaviour and is not changed here.🤖 Generated with Claude Code
Summary by CodeRabbit