Skip to content

Commit

Permalink
feat: let modal filter after transition end event
Browse files Browse the repository at this point in the history
  • Loading branch information
Mini-ghost committed Jan 13, 2023
1 parent 136bb98 commit ab78614
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions packages/vue-final-modal/src/components/ModalsContainer.vue
@@ -1,6 +1,8 @@
<script setup lang="ts">
import { computed, onBeforeUnmount } from 'vue'
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import { isString } from '@vueuse/core'
import type { Ref } from 'vue'
import type { UseModalOptionsPrivate } from '../Modal'
import { useInternalVfm, useVfm } from '~/useApi'
const { modalsContainers, dynamicModals } = useVfm()
Expand All @@ -9,12 +11,43 @@ const { resolvedClosed, resolvedOpened } = useInternalVfm()
const uid = Symbol('ModalsContainer')
const shouldMount = computed(() => uid === modalsContainers.value[0])
const openedDynamicModals: Ref<UseModalOptionsPrivate[]> = ref([])
function syncOpenDynamicModals() {
openedDynamicModals.value = dynamicModals.filter(modal => modal.modelValue)
}
function withSyncOpenDynamicModals(callbackFn?: () => void) {
callbackFn?.()
syncOpenDynamicModals()
}
watch(() => dynamicModals.map(modal => modal.modelValue), (value, oldValue) => {
if (!oldValue || value.length !== oldValue.length) {
syncOpenDynamicModals()
return
}
let index = value.length
let shouldUpdate = false
while (!shouldUpdate && index--) {
if (value[index] === true && oldValue[index] === false)
shouldUpdate = true
}
if (!shouldUpdate)
return
syncOpenDynamicModals()
}, {
immediate: true,
})
modalsContainers.value.push(uid)
onBeforeUnmount(() => {
modalsContainers.value = modalsContainers.value.filter(i => i !== uid)
})
const openedDynamicModals = computed(() => dynamicModals.filter(modal => modal.modelValue))
</script>

<template>
Expand All @@ -25,7 +58,7 @@ const openedDynamicModals = computed(() => dynamicModals.filter(modal => modal.m
:key="modal.id"
v-bind="modal.attrs"
v-model="modal.modelValue"
@closed="() => resolvedClosed(index)"
@closed="withSyncOpenDynamicModals(() => resolvedClosed(index))"
@opened="() => resolvedOpened(index)"
>
<template v-for="(slot, key) in modal.slots" #[key] :key="key">
Expand Down

0 comments on commit ab78614

Please sign in to comment.