1
1
<script setup lang="ts">
2
- import { computed , onBeforeUnmount } from ' vue'
2
+ import { computed , onBeforeUnmount , ref , watch } from ' vue'
3
3
import { isString } from ' @vueuse/core'
4
+ import type { Ref } from ' vue'
5
+ import type { UseModalOptionsPrivate } from ' ../Modal'
4
6
import { useInternalVfm , useVfm } from ' ~/useApi'
5
7
6
8
const { modalsContainers, dynamicModals } = useVfm ()
@@ -9,6 +11,39 @@ const { resolvedClosed, resolvedOpened } = useInternalVfm()
9
11
const uid = Symbol (' ModalsContainer' )
10
12
const shouldMount = computed (() => uid === modalsContainers .value [0 ])
11
13
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
+
12
47
modalsContainers .value .push (uid )
13
48
onBeforeUnmount (() => {
14
49
modalsContainers .value = modalsContainers .value .filter (i => i !== uid )
@@ -19,11 +54,11 @@ onBeforeUnmount(() => {
19
54
<template v-if =" shouldMount " >
20
55
<component
21
56
:is =" modal.component"
22
- v-for =" (modal, index) in dynamicModals "
57
+ v-for =" (modal, index) in openedDynamicModals "
23
58
:key =" modal.id"
24
59
v-bind =" modal.attrs"
25
60
v-model =" modal.modelValue"
26
- @closed =" ( ) => resolvedClosed(index)"
61
+ @closed =" withSyncOpenDynamicModals(( ) => resolvedClosed(index) )"
27
62
@opened =" () => resolvedOpened(index)"
28
63
>
29
64
<template v-for =" (slot , key ) in modal .slots " #[key ] :key =" key " >
0 commit comments