Skip to content

Commit

Permalink
fix(Modal): wait for transition to complete to reset state (#1618)
Browse files Browse the repository at this point in the history
  • Loading branch information
noook authored Apr 5, 2024
1 parent b62cd79 commit 2bdb5d2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/runtime/components/overlays/Modal.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<TransitionRoot :appear="appear" :show="isOpen" as="template">
<TransitionRoot :appear="appear" :show="isOpen" as="template" @after-leave="onAfterLeave">
<HDialog :class="ui.wrapper" v-bind="attrs" @close="close">
<TransitionChild v-if="overlay" as="template" :appear="appear" v-bind="ui.overlay.transition">
<div :class="[ui.overlay.base, ui.overlay.background]" />
Expand Down Expand Up @@ -82,7 +82,7 @@ export default defineComponent({
default: () => ({})
}
},
emits: ['update:modelValue', 'close', 'close-prevented'],
emits: ['update:modelValue', 'close', 'close-prevented', 'after-leave'],
setup (props, { emit }) {
const { ui, attrs } = useUI('modal', toRef(props, 'ui'), config, toRef(props, 'class'))
Expand Down Expand Up @@ -117,6 +117,10 @@ export default defineComponent({
emit('close')
}
const onAfterLeave = () => {
emit('after-leave')
}
provideUseId(() => useId())
return {
Expand All @@ -125,6 +129,7 @@ export default defineComponent({
attrs,
isOpen,
transitionClass,
onAfterLeave,
close
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/components/overlays/Modals.client.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<template>
<component :is="modalState.component" v-if="modalState" v-bind="modalState.props" v-model="isOpen" />
<component
:is="modalState.component"
v-if="modalState"
v-bind="modalState.props"
v-model="isOpen"
@after-leave="reset"
/>
</template>

<script lang="ts" setup>
Expand All @@ -8,5 +14,5 @@ import { useModal, modalInjectionKey } from '../../composables/useModal'
const modalState = inject(modalInjectionKey)
const { isOpen } = useModal()
const { isOpen, reset } = useModal()
</script>
5 changes: 4 additions & 1 deletion src/runtime/composables/useModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ function _useModal () {
isOpen.value = true
}

function close () {
async function close () {
if (!modalState) return

isOpen.value = false
}

function reset () {
modalState.value = {
component: 'div',
props: {}
Expand All @@ -53,6 +55,7 @@ function _useModal () {
return {
open,
close,
reset,
patch,
isOpen
}
Expand Down

0 comments on commit 2bdb5d2

Please sign in to comment.