Skip to content

Commit

Permalink
feat: ability to use <VueFinalModal /> without register `createVfm(…
Browse files Browse the repository at this point in the history
…)` 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>
  • Loading branch information
Mini-ghost and hunterliu1003 committed Feb 8, 2023
1 parent a7fb444 commit 68600b7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
20 changes: 15 additions & 5 deletions packages/vue-final-modal/src/components/CoreModal/CoreModal.vue
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref, toRef, watch } from 'vue'
import { computed, inject, nextTick, onBeforeUnmount, onMounted, ref, toRef, watch } from 'vue'
import { coreModalProps } from './CoreModalProps'
import { useTransition } from './useTransition'
import { useToClose } from './useToClose'
Expand All @@ -9,10 +9,11 @@ import { useLockScroll } from './useBodyScrollLock'
import { useEvent } from './useEvent'
import { useZIndex } from './useZIndex'
import { noop, once } from '~/utils'
import type { Modal } from '~/Modal'
import { useInternalVfm, useVfm } from '~/useApi'
import type { InternalVfm, Modal, Vfm } from '~/Modal'
import { useSwipeToClose } from '~/useSwipeToClose'
import { internalVfmSymbol, vfmSymbol } from '~/injectionSymbols'
export interface CoreModalEmits {
(e: 'update:modelValue', modelValue: boolean): void
Expand All @@ -28,13 +29,22 @@ export interface CoreModalEmits {
const props = defineProps(coreModalProps)
const emit = defineEmits<CoreModalEmits>()
const { modals, openedModals } = useVfm()
const { modals, openedModals } = inject(vfmSymbol, {
modals: [],
openedModals: [],
} as any as Vfm)
const {
moveToLastOpenedModals,
openLastOverlay,
deleteFromOpenedModals,
deleteFromModals,
} = useInternalVfm()
} = inject(internalVfmSymbol, {
moveToLastOpenedModals: noop,
openLastOverlay: noop,
deleteFromOpenedModals: noop,
deleteFromModals: noop,
} as any as InternalVfm)
const vfmRootEl = ref<HTMLDivElement>()
const { zIndex, refreshZIndex } = useZIndex(props, { openedModals })
Expand Down
17 changes: 16 additions & 1 deletion packages/vue-final-modal/test/VueFinalModal.test.ts
Expand Up @@ -30,7 +30,22 @@ function expectExist(coreWrapper: VueWrapper<InstanceType<typeof CoreModal | typ
}

describe('tests VueFinalModal', () => {
it('basic', async () => {
it('without plugin registration', async () => {
const wrapper = mount(VueFinalModal, {
props: {
focusTrap: false,
},
})
const CoreModalWrapper = wrapper.findComponent(CoreModal)
expectExist(CoreModalWrapper, '.vfm', false)
expectExist(CoreModalWrapper, '.vfm__overlay', false)
wrapper.setProps({ modelValue: true })
await afterTransition()
expectVisible(CoreModalWrapper, '.vfm', true)
expectVisible(CoreModalWrapper, '.vfm__overlay', true)
})

it('with plugin registration', async () => {
const wrapper = mount(VueFinalModal, {
props: {
focusTrap: false,
Expand Down

0 comments on commit 68600b7

Please sign in to comment.