Skip to content

Commit 17760b6

Browse files
fix: avoid mount before modal open (#306)
* fix: add appear and filter openedDynamicModals in ModalsContainer * feat: let modal filter after transition end event Co-authored-by: Alex Liu <[email protected]>
1 parent 655457e commit 17760b6

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

packages/vue-final-modal/src/components/CoreModal/CoreModal.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ defineExpose({
154154
@mouseup.self="() => onMouseupRoot()"
155155
@mousedown.self="e => onMousedown(e)"
156156
>
157-
<Transition v-if="!hideOverlay" v-bind="overlayTransition" v-on="overlayListeners">
157+
<Transition v-if="!hideOverlay" v-bind="overlayTransition" appear v-on="overlayListeners">
158158
<div
159159
v-if="overlayVisible"
160160
class="vfm__overlay vfm--overlay vfm--absolute vfm--inset vfm--prevent-none"
@@ -164,7 +164,7 @@ defineExpose({
164164
aria-hidden="true"
165165
/>
166166
</Transition>
167-
<Transition v-bind="contentTransition" v-on="contentListeners">
167+
<Transition v-bind="contentTransition" appear v-on="contentListeners">
168168
<div
169169
v-show="contentVisible"
170170
ref="vfmContentEl"

packages/vue-final-modal/src/components/ModalsContainer.vue

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script setup lang="ts">
2-
import { computed, onBeforeUnmount } from 'vue'
2+
import { computed, onBeforeUnmount, ref, watch } from 'vue'
33
import { isString } from '@vueuse/core'
4+
import type { Ref } from 'vue'
5+
import type { UseModalOptionsPrivate } from '../Modal'
46
import { useInternalVfm, useVfm } from '~/useApi'
57
68
const { modalsContainers, dynamicModals } = useVfm()
@@ -9,6 +11,39 @@ const { resolvedClosed, resolvedOpened } = useInternalVfm()
911
const uid = Symbol('ModalsContainer')
1012
const shouldMount = computed(() => uid === modalsContainers.value[0])
1113
14+
const openedDynamicModals: Ref<UseModalOptionsPrivate[]> = ref([])
15+
16+
function syncOpenDynamicModals() {
17+
openedDynamicModals.value = dynamicModals.filter(modal => modal.modelValue)
18+
}
19+
20+
function withSyncOpenDynamicModals(callbackFn?: () => void) {
21+
callbackFn?.()
22+
syncOpenDynamicModals()
23+
}
24+
25+
watch(() => dynamicModals.map(modal => modal.modelValue), (value, oldValue) => {
26+
if (!oldValue || value.length !== oldValue.length) {
27+
syncOpenDynamicModals()
28+
return
29+
}
30+
31+
let index = value.length
32+
let shouldUpdate = false
33+
34+
while (!shouldUpdate && index--) {
35+
if (value[index] === true && oldValue[index] === false)
36+
shouldUpdate = true
37+
}
38+
39+
if (!shouldUpdate)
40+
return
41+
42+
syncOpenDynamicModals()
43+
}, {
44+
immediate: true,
45+
})
46+
1247
modalsContainers.value.push(uid)
1348
onBeforeUnmount(() => {
1449
modalsContainers.value = modalsContainers.value.filter(i => i !== uid)
@@ -19,11 +54,11 @@ onBeforeUnmount(() => {
1954
<template v-if="shouldMount">
2055
<component
2156
:is="modal.component"
22-
v-for="(modal, index) in dynamicModals"
57+
v-for="(modal, index) in openedDynamicModals"
2358
:key="modal.id"
2459
v-bind="modal.attrs"
2560
v-model="modal.modelValue"
26-
@closed="() => resolvedClosed(index)"
61+
@closed="withSyncOpenDynamicModals(() => resolvedClosed(index))"
2762
@opened="() => resolvedOpened(index)"
2863
>
2964
<template v-for="(slot, key) in modal.slots" #[key] :key="key">

0 commit comments

Comments
 (0)