Skip to content

Commit 68600b7

Browse files
feat: ability to use <VueFinalModal /> without register createVfm() plugin (#320)
* feat: make <CoreModal /> independent * chore: lint * test: add test case for using VueFinalModal without plugin registration --------- Co-authored-by: Hunter <hunterliu1003@gmail.com>
1 parent a7fb444 commit 68600b7

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

packages/vue-final-modal/src/components/CoreModal/CoreModal.vue

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { computed, nextTick, onBeforeUnmount, onMounted, ref, toRef, watch } from 'vue'
2+
import { computed, inject, nextTick, onBeforeUnmount, onMounted, ref, toRef, watch } from 'vue'
33
import { coreModalProps } from './CoreModalProps'
44
import { useTransition } from './useTransition'
55
import { useToClose } from './useToClose'
@@ -9,10 +9,11 @@ import { useLockScroll } from './useBodyScrollLock'
99
import { useEvent } from './useEvent'
1010
import { useZIndex } from './useZIndex'
1111
import { noop, once } from '~/utils'
12-
import type { Modal } from '~/Modal'
13-
import { useInternalVfm, useVfm } from '~/useApi'
12+
import type { InternalVfm, Modal, Vfm } from '~/Modal'
1413
import { useSwipeToClose } from '~/useSwipeToClose'
1514
15+
import { internalVfmSymbol, vfmSymbol } from '~/injectionSymbols'
16+
1617
export interface CoreModalEmits {
1718
(e: 'update:modelValue', modelValue: boolean): void
1819
@@ -28,13 +29,22 @@ export interface CoreModalEmits {
2829
const props = defineProps(coreModalProps)
2930
const emit = defineEmits<CoreModalEmits>()
3031
31-
const { modals, openedModals } = useVfm()
32+
const { modals, openedModals } = inject(vfmSymbol, {
33+
modals: [],
34+
openedModals: [],
35+
} as any as Vfm)
36+
3237
const {
3338
moveToLastOpenedModals,
3439
openLastOverlay,
3540
deleteFromOpenedModals,
3641
deleteFromModals,
37-
} = useInternalVfm()
42+
} = inject(internalVfmSymbol, {
43+
moveToLastOpenedModals: noop,
44+
openLastOverlay: noop,
45+
deleteFromOpenedModals: noop,
46+
deleteFromModals: noop,
47+
} as any as InternalVfm)
3848
3949
const vfmRootEl = ref<HTMLDivElement>()
4050
const { zIndex, refreshZIndex } = useZIndex(props, { openedModals })

packages/vue-final-modal/test/VueFinalModal.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,22 @@ function expectExist(coreWrapper: VueWrapper<InstanceType<typeof CoreModal | typ
3030
}
3131

3232
describe('tests VueFinalModal', () => {
33-
it('basic', async () => {
33+
it('without plugin registration', async () => {
34+
const wrapper = mount(VueFinalModal, {
35+
props: {
36+
focusTrap: false,
37+
},
38+
})
39+
const CoreModalWrapper = wrapper.findComponent(CoreModal)
40+
expectExist(CoreModalWrapper, '.vfm', false)
41+
expectExist(CoreModalWrapper, '.vfm__overlay', false)
42+
wrapper.setProps({ modelValue: true })
43+
await afterTransition()
44+
expectVisible(CoreModalWrapper, '.vfm', true)
45+
expectVisible(CoreModalWrapper, '.vfm__overlay', true)
46+
})
47+
48+
it('with plugin registration', async () => {
3449
const wrapper = mount(VueFinalModal, {
3550
props: {
3651
focusTrap: false,

0 commit comments

Comments
 (0)