Skip to content

Commit f2cb5bf

Browse files
committed
feat: destroy dynamic modal onClosed by default and add keepModalInstance option for keeping modal instance after modal.close
1 parent 017584c commit f2cb5bf

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type ModalSlot = string | Component | ModalSlotOptions
2424

2525
export type UseModalOptions<P> = {
2626
defaultModelValue?: boolean
27+
keepModalInstance?: boolean
2728
context?: Vfm
2829
component?: Constructor<P>
2930
attrs?: (RawProps & P) | ({} extends P ? null : never)

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
6969
console.warn('[Vue Final Modal warn] useModal() can only be used inside setup() or functional components.')
7070
}
7171

72+
tryOnUnmounted(() => {
73+
if (!options.keepModalInstance)
74+
destroy()
75+
})
76+
7277
function open(): Promise<string> {
7378
if (options.modelValue)
7479
return Promise.resolve('[Vue Final Modal] modal is already opened')
@@ -86,7 +91,12 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
8691

8792
options.modelValue = false
8893
return new Promise((resolve) => {
89-
options.resolveClosed = () => resolve('closed')
94+
options.resolveClosed = () => {
95+
console.log(`close options.keepModalInstance → `, options.keepModalInstance)
96+
if (!options.keepModalInstance)
97+
destroy()
98+
resolve('closed')
99+
}
90100
})
91101
}
92102

@@ -118,17 +128,13 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
118128
options.context.dynamicModals.splice(index, 1)
119129
}
120130

121-
const modal = {
131+
return {
122132
options,
123133
open,
124134
close,
125135
patchOptions,
126136
destroy,
127137
}
128-
129-
tryOnUnmounted(() => modal.destroy())
130-
131-
return modal
132138
}
133139

134140
export function useModalSlot<P>(options: {

0 commit comments

Comments
 (0)