1+ import { computed , inject , markRaw , nextTick , reactive , useAttrs } from 'vue'
12import { isString , tryOnUnmounted } from '@vueuse/core'
2- import { computed , getCurrentInstance , inject , markRaw , reactive , useAttrs } from 'vue'
33import type { Component } from 'vue'
44import VueFinalModal from './components/VueFinalModal/VueFinalModal.vue'
55import type CoreModal from './components/CoreModal/CoreModal.vue'
6- import { internalVfmSymbol , vfmSymbol } from './injectionSymbols'
6+ import { internalVfmSymbol } from './injectionSymbols'
77
88import type { ComponentProps , Constructor , InternalVfm , ModalSlot , ModalSlotOptions , RawProps , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
9- import { activeVfm , getActiveVfm , setActiveVfm } from './plugin'
9+ import { activeVfm , getActiveVfm } from './plugin'
1010
1111/**
1212 * Returns the vfm instance. Equivalent to using `$vfm` inside
1313 * templates.
1414 */
1515export function useVfm ( ) : Vfm {
16- return getActiveVfm ( ) !
16+ const vfm = getActiveVfm ( )
17+ if ( __DEV__ && ! vfm )
18+ consoleError ( )
19+
20+ return vfm !
1721}
1822
1923/**
@@ -53,25 +57,8 @@ function withMarkRaw<P>(options: Partial<UseModalOptions<P>>, DefaultComponent:
5357 * Create a dynamic modal.
5458 */
5559export 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-
7260 const options = reactive ( {
7361 id : Symbol ( 'useModal' ) ,
74- context : vfm ,
7562 modelValue : ! ! _options ?. defaultModelValue ,
7663 resolveOpened : ( ) => { } ,
7764 resolveClosed : ( ) => { } ,
@@ -83,16 +70,26 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
8370 destroy ( )
8471 } )
8572
86- if ( options . modelValue === true )
87- options . context ?. dynamicModals . push ( options )
73+ if ( options . modelValue === true ) {
74+ nextTick ( ( ) => {
75+ const vfm = useVfm ( )
76+ vfm ?. dynamicModals . push ( options )
77+ } )
78+ }
8879
89- function open ( ) : Promise < string > {
80+ async function open ( ) : Promise < string > {
81+ await nextTick ( )
82+ const vfm = useVfm ( )
83+ if ( ! vfm ) {
84+ consoleError ( )
85+ return Promise . resolve ( 'error' )
86+ }
9087 if ( options . modelValue )
9188 return Promise . resolve ( '[Vue Final Modal] modal is already opened.' )
9289
9390 destroy ( )
9491 options . modelValue = true
95- options . context ? .dynamicModals . push ( options )
92+ vfm . dynamicModals . push ( options )
9693
9794 return new Promise ( ( resolve ) => {
9895 options . resolveOpened = ( ) => resolve ( 'opened' )
@@ -116,8 +113,6 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
116113 options . defaultModelValue = _options . defaultModelValue
117114 if ( _options ?. keepAlive !== undefined )
118115 options . keepAlive = _options ?. keepAlive
119- if ( _options . context )
120- options . context = _options . context
121116
122117 // patch options.component and options.attrs
123118 patchComponentOptions ( options , rest )
@@ -156,11 +151,14 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
156151 }
157152
158153 function destroy ( ) : void {
159- if ( ! options . context )
154+ const vfm = useVfm ( )
155+ if ( ! vfm ) {
156+ consoleError ( )
160157 return
161- const index = options . context . dynamicModals . indexOf ( options )
158+ }
159+ const index = vfm . dynamicModals . indexOf ( options )
162160 if ( index !== - 1 )
163- options . context . dynamicModals . splice ( index , 1 )
161+ vfm . dynamicModals . splice ( index , 1 )
164162 }
165163
166164 return {
@@ -223,3 +221,14 @@ export function useVfmAttrs(options: {
223221
224222 return vfmAttrs
225223}
224+
225+ function consoleError ( ) {
226+ if ( __DEV__ && ! activeVfm ) {
227+ throw new Error (
228+ '[Vue Final Modal]: getActiveVfm was called with no active Vfm. Did you forget to install vfm?\n'
229+ + '\tconst vfm = createVfm()\n'
230+ + '\tapp.use(vfm)\n'
231+ + 'This will fail in production.' ,
232+ )
233+ }
234+ }
0 commit comments