Skip to content

Commit eb7026c

Browse files
EasonLin0716claude
andauthored
fix(Dialog): re-key auto-focus on presence when force-mounted (#2856)
`DialogContent` folded `forceMount` into `Presence`'s `present`, pinning it to `true`. The content then never unmounted, so `FocusScope` never remounted and `openAutoFocus` fired exactly once — at mount, before the dialog had ever been opened, stealing focus into a closed dialog and locking body scroll. The `present` watcher added for `unmountOnHide` could not compensate either: the value passed down was `unmountOnHide || present`, a constant `true` under the default `unmountOnHide: true`, so it never saw a transition. Separate the two concerns. `:force-mount` keeps the node in the DOM, while `:present` now carries the real open state. `forceMount` stays in the `v-show` expression so a force-mounted dialog remains visible and the consumer keeps control of its own animation. `DialogOverlay` needs the identical change: its `present` drives the body scroll lock, which otherwise stayed locked after dismissing a force-mounted dialog. Closes #2450 Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 0351f0d commit eb7026c

3 files changed

Lines changed: 150 additions & 11 deletions

File tree

‎packages/core/src/Dialog/Dialog.test.ts‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,126 @@ describe('given a Dialog with unmountOnHide=false, openAutoFocus', () => {
243243

244244
wrapper.unmount()
245245
})
246+
247+
it('should emit openAutoFocus again on reopen', async () => {
248+
document.body.innerHTML = ''
249+
const onOpenAutoFocus = vi.fn()
250+
const wrapper = mount(OpenAutoFocusDialog, { attachTo: document.body, props: { onOpenAutoFocus } })
251+
const trigger = wrapper.find('button')
252+
253+
await fireEvent.click(trigger.element)
254+
await vi.waitFor(() => expect(onOpenAutoFocus).toHaveBeenCalledTimes(1))
255+
256+
await fireEvent.click(await findByText(document.body, CLOSE_TEXT))
257+
await fireEvent.click(trigger.element)
258+
await vi.waitFor(() => expect(onOpenAutoFocus).toHaveBeenCalledTimes(2))
259+
260+
wrapper.unmount()
261+
})
262+
})
263+
264+
// `forceMount` also keeps the content in the DOM across open/close, so
265+
// `FocusScope` never physically remounts and mount-keyed behaviour has to be
266+
// re-keyed on the presence state. Folding `forceMount` into `Presence`'s
267+
// `present` pinned that state to `true`, so `openAutoFocus` fired only on the
268+
// very first open and the overlay never released the body scroll lock (#2450).
269+
describe('given a Dialog with forceMount', () => {
270+
const ForceMountDialog = defineComponent({
271+
components: { DialogRoot, DialogTrigger, DialogOverlay, DialogContent, DialogClose, DialogTitle },
272+
props: ['onOpenAutoFocus'],
273+
template: `<DialogRoot>
274+
<DialogTrigger>${OPEN_TEXT}</DialogTrigger>
275+
<DialogOverlay force-mount />
276+
<DialogContent force-mount @open-auto-focus="onOpenAutoFocus">
277+
<DialogTitle>${TITLE_TEXT}</DialogTitle>
278+
<DialogClose>${CLOSE_TEXT}</DialogClose>
279+
</DialogContent>
280+
</DialogRoot>`,
281+
})
282+
283+
let wrapper: VueWrapper
284+
285+
// The body scroll lock is a shared composable: leaving a dialog mounted after
286+
// a failed assertion keeps the lock held and cascades into unrelated tests.
287+
afterEach(() => {
288+
wrapper?.unmount()
289+
document.body.style.overflow = ''
290+
document.body.style.pointerEvents = ''
291+
})
292+
293+
it('should emit openAutoFocus on every open, not only the first', async () => {
294+
document.body.innerHTML = ''
295+
const onOpenAutoFocus = vi.fn()
296+
wrapper = mount(ForceMountDialog, { attachTo: document.body, props: { onOpenAutoFocus } })
297+
const trigger = wrapper.find('button')
298+
299+
// Force-mounted but closed: the dialog must not grab focus or lock the body
300+
// on mount. `FocusScope` dispatches its mount auto-focus a few ticks in, so
301+
// a bare `nextTick()` here would pass even when it does fire.
302+
await sleep(100)
303+
expect(onOpenAutoFocus).toHaveBeenCalledTimes(0)
304+
expect(document.activeElement).toBe(document.body)
305+
expect(document.body.style.overflow).not.toBe('hidden')
306+
307+
await fireEvent.click(trigger.element)
308+
await vi.waitFor(() => expect(onOpenAutoFocus).toHaveBeenCalledTimes(1))
309+
310+
await fireEvent.click(await findByText(document.body, CLOSE_TEXT))
311+
await fireEvent.click(trigger.element)
312+
await vi.waitFor(() => expect(onOpenAutoFocus).toHaveBeenCalledTimes(2))
313+
})
314+
315+
it('should release the body scroll lock on close', async () => {
316+
document.body.innerHTML = ''
317+
wrapper = mount(ForceMountDialog, { attachTo: document.body, props: { onOpenAutoFocus: vi.fn() } })
318+
const trigger = wrapper.find('button')
319+
320+
await fireEvent.click(trigger.element)
321+
await vi.waitFor(() => expect(document.body.style.overflow).toBe('hidden'))
322+
323+
await fireEvent.click(await findByText(document.body, CLOSE_TEXT))
324+
await vi.waitFor(() => expect(document.body.style.overflow).not.toBe('hidden'))
325+
})
326+
})
327+
328+
// Regression guard for the default (unmounting) path: the content unmounts on
329+
// close, so the auto-focus is driven by the physical remount rather than by
330+
// `present`.
331+
describe('given a default Dialog, openAutoFocus', () => {
332+
const DefaultAutoFocusDialog = defineComponent({
333+
components: { DialogRoot, DialogTrigger, DialogContent, DialogClose, DialogTitle },
334+
props: ['onOpenAutoFocus'],
335+
template: `<DialogRoot>
336+
<DialogTrigger>${OPEN_TEXT}</DialogTrigger>
337+
<DialogContent @open-auto-focus="onOpenAutoFocus">
338+
<DialogTitle>${TITLE_TEXT}</DialogTitle>
339+
<DialogClose>${CLOSE_TEXT}</DialogClose>
340+
</DialogContent>
341+
</DialogRoot>`,
342+
})
343+
344+
it('should emit once per open across repeated opens', async () => {
345+
document.body.innerHTML = ''
346+
const onOpenAutoFocus = vi.fn()
347+
const wrapper = mount(DefaultAutoFocusDialog, { attachTo: document.body, props: { onOpenAutoFocus } })
348+
const trigger = wrapper.find('button')
349+
350+
await nextTick()
351+
expect(onOpenAutoFocus).toHaveBeenCalledTimes(0)
352+
353+
await fireEvent.click(trigger.element)
354+
await vi.waitFor(() => expect(onOpenAutoFocus).toHaveBeenCalledTimes(1))
355+
356+
await fireEvent.click(await findByText(document.body, CLOSE_TEXT))
357+
// The content unmounts asynchronously; reopening before it is gone would
358+
// reuse the still-mounted scope and tell us nothing about the remount path.
359+
await vi.waitFor(() => expect(document.querySelector('[role="dialog"]')).toBeNull())
360+
361+
await fireEvent.click(trigger.element)
362+
await vi.waitFor(() => expect(onOpenAutoFocus).toHaveBeenCalledTimes(2))
363+
364+
wrapper.unmount()
365+
})
246366
})
247367

248368
// Two dialogs with `unmountOnHide: false` coexist on the page (e.g. a menu

‎packages/core/src/Dialog/DialogContent.vue‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface DialogContentProps extends Omit<DialogContentImplProps, 'trapFo
1616
</script>
1717

1818
<script setup lang="ts">
19+
import { computed } from 'vue'
1920
import { Presence } from '@/Presence'
2021
import { useEmitAsProps, useForwardExpose } from '@/shared'
2122
import DialogContentModal from './DialogContentModal.vue'
@@ -35,28 +36,38 @@ const rootContext = injectDialogRootContext()
3536
3637
const emitsAsProps = useEmitAsProps(emits)
3738
const { forwardRef } = useForwardExpose()
39+
40+
// The node stays in the DOM across open/close when force-mounted, or when the
41+
// root opts out of unmounting. Only then does the child need the real presence
42+
// state: mount-keyed behaviour (`FocusScope`'s auto-focus, layer stacks) has to
43+
// be re-keyed on `present` because no physical unmount happens. When the node
44+
// does unmount on close, keep passing a constant `true` so the child is never
45+
// briefly told it is absent while `usePresence` settles on `MOUNT`.
46+
const staysMounted = computed(
47+
() => props.forceMount || !rootContext.unmountOnHide.value,
48+
)
3849
</script>
3950

4051
<template>
4152
<Presence
4253
v-slot="{ present }"
43-
:present="forceMount || rootContext.open.value"
44-
:force-mount="forceMount || !rootContext.unmountOnHide.value"
54+
:present="rootContext.open.value"
55+
:force-mount="staysMounted"
4556
>
4657
<DialogContentModal
4758
v-if="rootContext.modal.value"
48-
v-show="rootContext.unmountOnHide.value || present"
59+
v-show="forceMount || rootContext.unmountOnHide.value || present"
4960
:ref="forwardRef"
50-
:present="rootContext.unmountOnHide.value || present"
61+
:present="staysMounted ? present : true"
5162
v-bind="{ ...props, ...emitsAsProps, ...$attrs }"
5263
>
5364
<slot />
5465
</DialogContentModal>
5566
<DialogContentNonModal
5667
v-else
57-
v-show="rootContext.unmountOnHide.value || present"
68+
v-show="forceMount || rootContext.unmountOnHide.value || present"
5869
:ref="forwardRef"
59-
:present="rootContext.unmountOnHide.value || present"
70+
:present="staysMounted ? present : true"
6071
v-bind="{ ...props, ...emitsAsProps, ...$attrs }"
6172
>
6273
<slot />

‎packages/core/src/Dialog/DialogOverlay.vue‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,37 @@ export interface DialogOverlayProps extends DialogOverlayImplProps {
1313
</script>
1414

1515
<script setup lang="ts">
16+
import { computed } from 'vue'
1617
import { Presence } from '@/Presence'
1718
import { injectDialogRootContext } from './DialogRoot.vue'
1819
19-
defineProps<DialogOverlayProps>()
20+
const props = defineProps<DialogOverlayProps>()
2021
const rootContext = injectDialogRootContext()
2122
2223
const { forwardRef } = useForwardExpose()
24+
25+
// See `DialogContent.vue`: when the overlay is kept mounted across open/close it
26+
// must report the real presence state, otherwise `DialogOverlayImpl` keeps the
27+
// body scroll locked after the dialog is dismissed.
28+
const staysMounted = computed(
29+
() => props.forceMount || !rootContext.unmountOnHide.value,
30+
)
2331
</script>
2432

2533
<template>
2634
<Presence
2735
v-if="rootContext?.modal.value"
2836
v-slot="{ present }"
29-
:present="forceMount || rootContext.open.value"
30-
:force-mount="forceMount || !rootContext.unmountOnHide.value"
37+
:present="rootContext.open.value"
38+
:force-mount="staysMounted"
3139
>
3240
<DialogOverlayImpl
33-
v-show="rootContext.unmountOnHide.value || present"
41+
v-show="forceMount || rootContext.unmountOnHide.value || present"
3442
v-bind="$attrs"
3543
:ref="forwardRef"
3644
:as="as"
3745
:as-child="asChild"
38-
:present="rootContext.unmountOnHide.value || present"
46+
:present="staysMounted ? present : true"
3947
>
4048
<slot />
4149
</DialogOverlayImpl>

0 commit comments

Comments
 (0)