1- import { computed , inject , onUnmounted , reactive , useAttrs } from 'vue'
1+ import { tryOnUnmounted } from '@vueuse/core'
2+ import { computed , inject , reactive , useAttrs } from 'vue'
23import type CoreModal from './components/CoreModal/CoreModal.vue'
34import { internalVfmSymbol , vfmSymbol } from './injectionSymbols'
45import type { ComponentProps , InternalVfm , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
@@ -24,13 +25,16 @@ export function useInternalVfm(): InternalVfm {
2425function defineModal <
2526 ModalProps extends ComponentProps ,
2627 DefaultSlotProps extends ComponentProps = { } ,
27- > ( _options : UseModalOptions < ModalProps , DefaultSlotProps > , vfm : Vfm ) : UseModalReturnType < ModalProps , DefaultSlotProps > {
28+ > ( _options : UseModalOptions < ModalProps , DefaultSlotProps > ) : UseModalReturnType < ModalProps , DefaultSlotProps > {
2829 const options = reactive ( {
2930 id : Symbol ( 'useModal' ) ,
3031 modelValue : false ,
3132 ..._options ,
3233 } ) as UseModalOptionsPrivate < ModalProps , DefaultSlotProps >
3334
35+ if ( ! options . context )
36+ options . context = useVfm ( )
37+
3438 function open ( ) : Promise < string > {
3539 if ( options . modelValue )
3640 return Promise . resolve ( '[Vue Final Modal] modal is already opened' )
@@ -58,9 +62,11 @@ function defineModal<
5862 }
5963
6064 function destroy ( ) : void {
61- const index = vfm . dynamicModals . indexOf ( options )
65+ if ( ! options . context )
66+ return
67+ const index = options . context . dynamicModals . indexOf ( options )
6268 if ( index !== - 1 )
63- vfm . dynamicModals . splice ( index , 1 )
69+ options . context . dynamicModals . splice ( index , 1 )
6470 }
6571
6672 return {
@@ -78,14 +84,12 @@ function defineModal<
7884export function useModal <
7985 ModalProps extends ComponentProps ,
8086 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 )
8589
86- _vfm . dynamicModals . push ( modal . options )
90+ modal . options . context ? .dynamicModals . push ( modal . options )
8791
88- onUnmounted ( ( ) => modal . destroy ( ) )
92+ tryOnUnmounted ( ( ) => modal . destroy ( ) )
8993
9094 return modal
9195}
0 commit comments