@@ -6,13 +6,14 @@ import type CoreModal from './components/CoreModal/CoreModal.vue'
6
6
import { internalVfmSymbol , vfmSymbol } from './injectionSymbols'
7
7
8
8
import type { ComponentProps , Constructor , InternalVfm , ModalSlot , ModalSlotOptions , RawProps , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
9
+ import { activeVfm , getActiveVfm , setActiveVfm } from './plugin'
9
10
10
11
/**
11
12
* Returns the vfm instance. Equivalent to using `$vfm` inside
12
13
* templates.
13
14
*/
14
15
export function useVfm ( ) : Vfm {
15
- return inject ( vfmSymbol ) !
16
+ return getActiveVfm ( ) !
16
17
}
17
18
18
19
/**
@@ -52,23 +53,31 @@ function withMarkRaw<P>(options: Partial<UseModalOptions<P>>, DefaultComponent:
52
53
* Create a dynamic modal.
53
54
*/
54
55
export 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
+
55
72
const options = reactive ( {
56
73
id : Symbol ( 'useModal' ) ,
74
+ context : vfm ,
57
75
modelValue : ! ! _options ?. defaultModelValue ,
58
76
resolveOpened : ( ) => { } ,
59
77
resolveClosed : ( ) => { } ,
60
78
attrs : { } ,
61
79
...withMarkRaw < P > ( _options ) ,
62
80
} ) 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
-
72
81
tryOnUnmounted ( ( ) => {
73
82
if ( ! options . keepAlive )
74
83
destroy ( )
@@ -77,17 +86,13 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
77
86
if ( options . modelValue === true )
78
87
options . context ?. dynamicModals . push ( options )
79
88
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 > {
85
90
if ( options . modelValue )
86
91
return Promise . resolve ( '[Vue Final Modal] modal is already opened.' )
87
92
88
93
destroy ( )
89
94
options . modelValue = true
90
- options . context . dynamicModals . push ( options )
95
+ options . context ? .dynamicModals . push ( options )
91
96
92
97
return new Promise ( ( resolve ) => {
93
98
options . resolveOpened = ( ) => resolve ( 'opened' )
0 commit comments