1
- import { computed , inject , onUnmounted , reactive , useAttrs } from 'vue'
1
+ import { tryOnUnmounted } from '@vueuse/core'
2
+ import { computed , inject , reactive , useAttrs } from 'vue'
2
3
import type CoreModal from './components/CoreModal/CoreModal.vue'
3
4
import { internalVfmSymbol , vfmSymbol } from './injectionSymbols'
4
5
import type { ComponentProps , InternalVfm , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
@@ -24,13 +25,16 @@ export function useInternalVfm(): InternalVfm {
24
25
function defineModal <
25
26
ModalProps extends ComponentProps ,
26
27
DefaultSlotProps extends ComponentProps = { } ,
27
- > ( _options : UseModalOptions < ModalProps , DefaultSlotProps > , vfm : Vfm ) : UseModalReturnType < ModalProps , DefaultSlotProps > {
28
+ > ( _options : UseModalOptions < ModalProps , DefaultSlotProps > ) : UseModalReturnType < ModalProps , DefaultSlotProps > {
28
29
const options = reactive ( {
29
30
id : Symbol ( 'useModal' ) ,
30
31
modelValue : false ,
31
32
..._options ,
32
33
} ) as UseModalOptionsPrivate < ModalProps , DefaultSlotProps >
33
34
35
+ if ( ! options . context )
36
+ options . context = useVfm ( )
37
+
34
38
function open ( ) : Promise < string > {
35
39
if ( options . modelValue )
36
40
return Promise . resolve ( '[Vue Final Modal] modal is already opened' )
@@ -58,9 +62,11 @@ function defineModal<
58
62
}
59
63
60
64
function destroy ( ) : void {
61
- const index = vfm . dynamicModals . indexOf ( options )
65
+ if ( ! options . context )
66
+ return
67
+ const index = options . context . dynamicModals . indexOf ( options )
62
68
if ( index !== - 1 )
63
- vfm . dynamicModals . splice ( index , 1 )
69
+ options . context . dynamicModals . splice ( index , 1 )
64
70
}
65
71
66
72
return {
@@ -78,14 +84,12 @@ function defineModal<
78
84
export function useModal <
79
85
ModalProps extends ComponentProps ,
80
86
DefaultSlotProps extends ComponentProps = { } ,
81
- > ( _options : UseModalOptions < ModalProps , DefaultSlotProps > , vfm ?: Vfm ) : UseModalReturnType < ModalProps , DefaultSlotProps > {
82
- const _vfm : Vfm = vfm || useVfm ( )
83
-
84
- const modal = defineModal ( _options , _vfm )
87
+ > ( _options : UseModalOptions < ModalProps , DefaultSlotProps > ) : UseModalReturnType < ModalProps , DefaultSlotProps > {
88
+ const modal = defineModal ( _options )
85
89
86
- _vfm . dynamicModals . push ( modal . options )
90
+ modal . options . context ? .dynamicModals . push ( modal . options )
87
91
88
- onUnmounted ( ( ) => modal . destroy ( ) )
92
+ tryOnUnmounted ( ( ) => modal . destroy ( ) )
89
93
90
94
return modal
91
95
}
0 commit comments