@@ -6,13 +6,14 @@ import type CoreModal from './components/CoreModal/CoreModal.vue'
66import { internalVfmSymbol , vfmSymbol } from './injectionSymbols'
77
88import type { ComponentProps , Constructor , InternalVfm , ModalSlot , ModalSlotOptions , RawProps , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
9+ import { activeVfm , getActiveVfm , setActiveVfm } from './plugin'
910
1011/**
1112 * Returns the vfm instance. Equivalent to using `$vfm` inside
1213 * templates.
1314 */
1415export function useVfm ( ) : Vfm {
15- return inject ( vfmSymbol ) !
16+ return getActiveVfm ( ) !
1617}
1718
1819/**
@@ -52,23 +53,31 @@ function withMarkRaw<P>(options: Partial<UseModalOptions<P>>, DefaultComponent:
5253 * Create a dynamic modal.
5354 */
5455export function useModal < P = InstanceType < typeof VueFinalModal > [ '$props' ] > ( _options : UseModalOptions < P > ) : UseModalReturnType < P > {
56+ const currentInstance = getCurrentInstance ( )
57+ let vfm = _options . context || ( currentInstance && inject ( vfmSymbol ) )
58+ if ( vfm )
59+ setActiveVfm ( vfm )
60+
61+ if ( __DEV__ && ! activeVfm ) {
62+ throw new Error (
63+ '[🍍]: getActiveVfm was called with no active Vfm. Did you forget to install vfm?\n'
64+ + '\tconst vfm = createVfm()\n'
65+ + '\tapp.use(vfm)\n'
66+ + 'This will fail in production.' ,
67+ )
68+ }
69+
70+ vfm = activeVfm
71+
5572 const options = reactive ( {
5673 id : Symbol ( 'useModal' ) ,
74+ context : vfm ,
5775 modelValue : ! ! _options ?. defaultModelValue ,
5876 resolveOpened : ( ) => { } ,
5977 resolveClosed : ( ) => { } ,
6078 attrs : { } ,
6179 ...withMarkRaw < P > ( _options ) ,
6280 } ) as UseModalOptions < P > & UseModalOptionsPrivate
63-
64- if ( ! options . context ) {
65- const currentInstance = getCurrentInstance ( )
66- if ( currentInstance )
67- options . context = useVfm ( )
68- else if ( __DEV__ )
69- console . warn ( '[Vue Final Modal warn] useModal() can only be used inside setup() or functional components.' )
70- }
71-
7281 tryOnUnmounted ( ( ) => {
7382 if ( ! options . keepAlive )
7483 destroy ( )
@@ -77,17 +86,13 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
7786 if ( options . modelValue === true )
7887 options . context ?. dynamicModals . push ( options )
7988
80- function open ( opt ?: { context : Vfm } ) : Promise < string > {
81- if ( opt ?. context )
82- options . context = opt . context
83- if ( ! options ?. context )
84- return Promise . resolve ( '[Vue Final Modal] options.context is not exist.' )
89+ function open ( ) : Promise < string > {
8590 if ( options . modelValue )
8691 return Promise . resolve ( '[Vue Final Modal] modal is already opened.' )
8792
8893 destroy ( )
8994 options . modelValue = true
90- options . context . dynamicModals . push ( options )
95+ options . context ? .dynamicModals . push ( options )
9196
9297 return new Promise ( ( resolve ) => {
9398 options . resolveOpened = ( ) => resolve ( 'opened' )
0 commit comments