Skip to content

Commit 017584c

Browse files
committed
refactor: push dynamic modal options when open a dynamic modal instead of create a dynamic modal instance
1 parent 7e38512 commit 017584c

File tree

2 files changed

+4
-40
lines changed

2 files changed

+4
-40
lines changed

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

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<script setup lang="ts">
2-
import { computed, onBeforeUnmount, shallowRef, watch } from 'vue'
2+
import { computed, onBeforeUnmount } from 'vue'
33
import { isString } from '@vueuse/core'
4-
import type { Ref } from 'vue'
5-
import type { UseModalOptions, UseModalOptionsPrivate } from '../Modal'
64
import { useInternalVfm, useVfm } from '~/useApi'
75
86
const vfm = useVfm()
@@ -11,39 +9,6 @@ const _vfm = useInternalVfm()
119
const uid = Symbol('ModalsContainer')
1210
const shouldMount = computed(() => uid === vfm.modalsContainers.value?.[0])
1311
14-
const openedDynamicModals: Ref<(UseModalOptions<any> & UseModalOptionsPrivate)[]> = shallowRef([])
15-
16-
function syncOpenDynamicModals() {
17-
openedDynamicModals.value = vfm.dynamicModals.filter(modal => modal.modelValue)
18-
}
19-
20-
function withSyncOpenDynamicModals(callbackFn?: () => void) {
21-
callbackFn?.()
22-
syncOpenDynamicModals()
23-
}
24-
25-
watch(() => vfm.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-
4712
vfm.modalsContainers.value.push(uid)
4813
onBeforeUnmount(() => {
4914
vfm.modalsContainers.value = vfm.modalsContainers.value.filter(i => i !== uid)
@@ -54,11 +19,11 @@ onBeforeUnmount(() => {
5419
<template v-if="shouldMount">
5520
<component
5621
:is="modal.component"
57-
v-for="(modal, index) in openedDynamicModals"
22+
v-for="(modal, index) in vfm.dynamicModals"
5823
:key="modal.id"
5924
v-bind="modal.attrs"
6025
v-model="modal.modelValue"
61-
@closed="() => withSyncOpenDynamicModals(() => _vfm.resolvedClosed?.(index))"
26+
@closed="() => _vfm.resolvedClosed?.(index)"
6227
@opened="() => _vfm.resolvedOpened?.(index)"
6328
>
6429
<template v-for="(slot, key) in modal.slots" #[key] :key="key">

packages/vue-final-modal/src/useApi.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
7474
return Promise.resolve('[Vue Final Modal] modal is already opened')
7575

7676
options.modelValue = true
77+
options.context?.dynamicModals.push(options)
7778
return new Promise((resolve) => {
7879
options.resolveOpened = () => resolve('opened')
7980
})
@@ -125,8 +126,6 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
125126
destroy,
126127
}
127128

128-
modal.options.context?.dynamicModals.push(modal.options)
129-
130129
tryOnUnmounted(() => modal.destroy())
131130

132131
return modal

0 commit comments

Comments
 (0)