@@ -21,10 +21,10 @@ export function useInternalVfm(): InternalVfm {
21
21
/**
22
22
* Define a dynamic modal.
23
23
*/
24
- export function defineModal <
24
+ function defineModal <
25
25
ModalProps extends ComponentProps ,
26
26
DefaultSlotProps extends ComponentProps = { } ,
27
- > ( _options ? : UseModalOptions < ModalProps , DefaultSlotProps > ) : UseModalReturnType < ModalProps , DefaultSlotProps > {
27
+ > ( _options : UseModalOptions < ModalProps , DefaultSlotProps > , vfm : Vfm ) : UseModalReturnType < ModalProps , DefaultSlotProps > {
28
28
const options = reactive ( {
29
29
id : Symbol ( 'useModal' ) ,
30
30
modelValue : false ,
@@ -57,32 +57,35 @@ export function defineModal<
57
57
Object . assign ( options ?. slots || { } , _options ?. slots || { } )
58
58
}
59
59
60
+ function destroy ( ) : void {
61
+ const index = vfm . dynamicModals . indexOf ( options )
62
+ if ( index !== - 1 )
63
+ vfm . dynamicModals . splice ( index , 1 )
64
+ }
65
+
60
66
return {
61
67
options,
62
68
open,
63
69
close,
64
70
patchOptions,
71
+ destroy,
65
72
}
66
73
}
67
74
68
75
/**
69
76
* Create a dynamic modal.
70
77
*/
71
78
export function useModal <
72
- ModalProps extends ComponentProps ,
73
- DefaultSlotProps extends ComponentProps = { } ,
74
- > ( _options ? : UseModalOptions < ModalProps , DefaultSlotProps > ) : UseModalReturnType < ModalProps , DefaultSlotProps > {
75
- const { dynamicModals } = useVfm ( )
79
+ ModalProps extends ComponentProps ,
80
+ DefaultSlotProps extends ComponentProps = { } ,
81
+ > ( _options : UseModalOptions < ModalProps , DefaultSlotProps > , vfm ?: Vfm ) : UseModalReturnType < ModalProps , DefaultSlotProps > {
82
+ const _vfm : Vfm = vfm || useVfm ( )
76
83
77
- const modal = defineModal ( _options )
84
+ const modal = defineModal ( _options , _vfm )
78
85
79
- dynamicModals . push ( modal . options )
86
+ _vfm . dynamicModals . push ( modal . options )
80
87
81
- onUnmounted ( ( ) => {
82
- const index = dynamicModals . indexOf ( modal . options )
83
- if ( index !== - 1 )
84
- dynamicModals . splice ( index , 1 )
85
- } )
88
+ onUnmounted ( ( ) => modal . destroy ( ) )
86
89
87
90
return modal
88
91
}
0 commit comments