Skip to content

Commit 47cf520

Browse files
committed
feat: get rid of context and made useModal can be execute everywhere
1 parent fc15a7c commit 47cf520

File tree

8 files changed

+62
-41
lines changed

8 files changed

+62
-41
lines changed

docs/content/2.get-started/1.guide/4.types.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export type ModalSlot = string | Component | ModalSlotOptions
3333
export type UseModalOptions<P> = {
3434
defaultModelValue?: boolean
3535
keepAlive?: boolean
36-
context?: Vfm
3736
component?: Constructor<P>
3837
attrs?: (RawProps & P) | ({} extends P ? null : never)
3938
slots?: {

packages/vue-final-modal/cypress/components/focusTrap.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ describe('Test focusTrap', () => {
77
const vfm = createVfm()
88

99
const firstModal = useModal({
10-
context: vfm,
1110
component: VueFinalModal,
1211
attrs: { contentClass: 'first-modal-content' },
1312
slots: {
@@ -16,7 +15,6 @@ describe('Test focusTrap', () => {
1615
})
1716

1817
const secondModal = useModal({
19-
context: vfm,
2018
component: VueFinalModal,
2119
attrs: { contentClass: 'second-modal-content' },
2220
slots: {

packages/vue-final-modal/cypress/components/useModal.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ describe('Test useModal()', () => {
66
it('Should be closed by default', () => {
77
const vfm = createVfm()
88
const modal = useModal({
9-
context: vfm,
109
slots: { default: 'Hello World!' },
1110
})
1211

@@ -25,7 +24,6 @@ describe('Test useModal()', () => {
2524
it('Should be opened by given defaultModelValue: true', () => {
2625
const vfm = createVfm()
2726
useModal({
28-
context: vfm,
2927
defaultModelValue: true,
3028
slots: {
3129
default: 'Hello World!',
@@ -51,7 +49,6 @@ describe('Test useModal()', () => {
5149
const onClosed = cy.spy().as('onClosed')
5250

5351
const modal = useModal({
54-
context: vfm,
5552
attrs: {
5653
onBeforeOpen,
5754
onOpened,

packages/vue-final-modal/cypress/components/useZIndex.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@ describe('Test useZIndex()', () => {
55
it('Props: zIndexFn()', () => {
66
const vfm = createVfm()
77
const firstModal = useModal({
8-
context: vfm,
98
component: VueFinalModal,
109
attrs: { class: 'first-modal' },
1110
})
1211

1312
const secondModal = useModal({
14-
context: vfm,
1513
component: VueFinalModal,
1614
attrs: { class: 'second-modal' },
1715
})
1816

1917
const thirdModal = useModal({
20-
context: vfm,
2118
component: VueFinalModal,
2219
attrs: { class: 'third-modal' },
2320
})

packages/vue-final-modal/src/Modal.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export type ModalSlot = string | Component | ModalSlotOptions
2525
export type UseModalOptions<P> = {
2626
defaultModelValue?: boolean
2727
keepAlive?: boolean
28-
context?: Vfm
2928
component?: Constructor<P>
3029
attrs?: (RawProps & P) | ({} extends P ? null : never)
3130
slots?: {

packages/vue-final-modal/src/useApi.ts

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
import { computed, inject, markRaw, nextTick, reactive, useAttrs } from 'vue'
12
import { isString, tryOnUnmounted } from '@vueuse/core'
2-
import { computed, getCurrentInstance, inject, markRaw, reactive, useAttrs } from 'vue'
33
import type { Component } from 'vue'
44
import VueFinalModal from './components/VueFinalModal/VueFinalModal.vue'
55
import type CoreModal from './components/CoreModal/CoreModal.vue'
6-
import { internalVfmSymbol, vfmSymbol } from './injectionSymbols'
6+
import { internalVfmSymbol } from './injectionSymbols'
77

88
import 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
*/
1515
export 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
*/
5559
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-
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+
}

viteplay/src/components/VueFinalModal/Basic.example.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
import { ref } from 'vue'
33
import { ModalsContainer, VueFinalModal, useModal, useModalSlot, useVfm } from 'vue-final-modal'
44
import DefaultSlot from '../DefaultSlot.vue'
5+
import { modal } from './modalsHelpers'
56
import TestModal from './TestModal.vue'
67
7-
const { toggle, closeAll } = useVfm()
8+
console.log('modal → ', modal)
89
10+
const { toggle, closeAll } = useVfm()
11+
// modal.open()
912
const modal1 = useModal({
1013
keepAlive: true,
1114
component: VueFinalModal,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { VueFinalModal, useModal, useModalSlot } from 'vue-final-modal'
2+
import DefaultSlot from '../DefaultSlot.vue'
3+
4+
export const modal = useModal({
5+
component: VueFinalModal,
6+
// defaultModelValue: true,
7+
slots: {
8+
default: useModalSlot({
9+
component: DefaultSlot,
10+
attrs: {
11+
text: '123',
12+
onCreate() {
13+
// console.log('onCreated')
14+
},
15+
},
16+
}),
17+
},
18+
})
19+
// modal.open()

0 commit comments

Comments
 (0)