Skip to content

Commit 98a1e6f

Browse files
committed
refactor: improve type, lint, cleanup
1 parent 9562ebe commit 98a1e6f

File tree

7 files changed

+55
-44
lines changed

7 files changed

+55
-44
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Constructor<P = any> = {
88
__isFragment?: never
99
__isTeleport?: never
1010
__isSuspense?: never
11-
new (...args: any[]): { $props: P }
11+
new(...args: any[]): { $props: P }
1212
}
1313

1414
export interface ModalSlotOptions { component: Raw<ComponentType>; attrs?: Record<string, any> }
@@ -61,16 +61,16 @@ export type Vfm = {
6161
toggle: (modalId: ModalId, show?: boolean) => undefined | Promise<string>
6262
open: (modalId: ModalId) => undefined | Promise<string>
6363
close: (modalId: ModalId) => undefined | Promise<string>
64-
closeAll: () => Promise<[PromiseSettledResult<Promise<string>[]>]>
64+
closeAll: () => Promise<PromiseSettledResult<string>[]>
6565
}
6666

6767
export type InternalVfm = {
68-
deleteFromModals: (modal: ComputedRef<Modal>) => void
68+
openLastOverlay: () => Promise<void>
6969
moveToLastOpenedModals: (modal: ComputedRef<Modal>) => void
7070
deleteFromOpenedModals: (modal: ComputedRef<Modal>) => void
7171
moveToLastOpenedModalOverlays: (modal: ComputedRef<Modal>) => void
7272
deleteFromOpenedModalOverlays: (modal: ComputedRef<Modal>) => void
73-
openLastOverlay: () => Promise<void>
73+
deleteFromModals: (modal: ComputedRef<Modal>) => void
7474
resolvedClosed: (index: number) => void
7575
resolvedOpened: (index: number) => void
7676
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ import { computed, onBeforeUnmount } from 'vue'
33
import { isString } from '~/utils'
44
import { useInternalVfm, useVfm } from '~/useApi'
55
6-
const vfm = useVfm()
7-
const _vfm = useInternalVfm()
6+
const { modalsContainers, dynamicModals } = useVfm()
7+
const { resolvedClosed, resolvedOpened } = useInternalVfm()
88
99
const uid = Symbol('ModalsContainer')
10-
const shouldMount = computed(() => uid === vfm.modalsContainers.value?.[0])
10+
const shouldMount = computed(() => uid === modalsContainers.value?.[0])
1111
12-
vfm.modalsContainers.value.push(uid)
12+
modalsContainers.value.push(uid)
1313
onBeforeUnmount(() => {
14-
vfm.modalsContainers.value = vfm.modalsContainers.value.filter(i => i !== uid)
14+
modalsContainers.value = modalsContainers.value.filter(i => i !== uid)
1515
})
1616
</script>
1717

1818
<template>
1919
<template v-if="shouldMount">
2020
<component
21-
:is="modal.component as any"
22-
v-for="(modal, index) in vfm.dynamicModals"
21+
:is="modal.component"
22+
v-for="(modal, index) in dynamicModals"
2323
:key="modal.id"
2424
v-bind="{
2525
displayDirective: modal?.keepAlive ? 'show' : undefined,
2626
...(typeof modal.attrs === 'object' ? modal.attrs : {}),
2727
}"
2828
v-model="modal.modelValue"
29-
@closed="() => _vfm.resolvedClosed?.(index)"
30-
@opened="() => _vfm.resolvedOpened?.(index)"
29+
@closed="() => resolvedClosed(index)"
30+
@opened="() => resolvedOpened(index)"
3131
>
3232
<template v-for="(slot, key) in modal.slots" #[key] :key="key">
3333
<div v-if="isString(slot)" v-html="slot" />

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { computed, inject, nextTick, onBeforeUnmount, onMounted, ref, toRef, useAttrs, watch } from 'vue'
2+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, toRef, useAttrs, watch } from 'vue'
33
import { vueFinalModalProps } from './VueFinalModalProps'
44
import { useTransition } from './useTransition'
55
import { useToClose } from './useToClose'
@@ -9,10 +9,9 @@ import { useLockScroll } from './useBodyScrollLock'
99
import { useZIndex } from './useZIndex'
1010
import { vVisible } from './vVisible'
1111
import { noop, once } from '~/utils'
12-
import type { InternalVfm, Modal, Vfm } from '~/Modal'
12+
import { type Modal } from '~/Modal'
1313
import { useSwipeToClose } from '~/useSwipeToClose'
14-
15-
import { internalVfmSymbol, vfmSymbol } from '~/injectionSymbols'
14+
import { useInternalVfm, useVfm } from '~/useApi'
1615
1716
export interface VueFinalModalEmits {
1817
(e: 'update:modelValue', modelValue: boolean): void
@@ -41,10 +40,7 @@ defineSlots<{
4140
'swipe-banner'(): void
4241
}>()
4342
44-
const { modals, openedModals } = inject(vfmSymbol, {
45-
modals: [],
46-
openedModals: [],
47-
} as any as Vfm)
43+
const { modals, openedModals } = useVfm()
4844
4945
const {
5046
openLastOverlay,
@@ -53,14 +49,7 @@ const {
5349
moveToLastOpenedModalOverlays,
5450
deleteFromOpenedModalOverlays,
5551
deleteFromModals,
56-
} = inject(internalVfmSymbol, {
57-
openLastOverlay: noop,
58-
moveToLastOpenedModals: noop,
59-
deleteFromOpenedModals: noop,
60-
moveToLastOpenedModalOverlays: noop,
61-
deleteFromOpenedModalOverlays: noop,
62-
deleteFromModals: noop,
63-
} as any as InternalVfm)
52+
} = useInternalVfm()
6453
6554
const vfmRootEl = ref<HTMLDivElement>()
6655
const vfmContentEl = ref<HTMLDivElement>()

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@ import type { App, ComputedRef } from 'vue'
22
import { getCurrentInstance, inject, markRaw, nextTick, ref, shallowReactive } from 'vue'
33
import { internalVfmSymbol, vfmSymbol } from './injectionSymbols'
44
import type { InternalVfm, Modal, ModalId, UseModalOptions, UseModalOptionsPrivate, Vfm } from './Modal'
5+
import { noop } from './utils'
56

67
// eslint-disable-next-line import/no-mutable-exports
78
export let activeVfm: Vfm | undefined
89

910
export const setActiveVfm = (vfm: Vfm | undefined) =>
1011
(activeVfm = vfm)
1112

13+
export const defaultVfm: Vfm = {
14+
install: noop,
15+
modals: [],
16+
openedModals: [],
17+
openedModalOverlays: [],
18+
dynamicModals: [],
19+
modalsContainers: ref([]),
20+
get: () => undefined,
21+
toggle: () => undefined,
22+
open: () => undefined,
23+
close: () => undefined,
24+
closeAll: () => Promise.allSettled([]),
25+
}
26+
1227
export const getActiveVfm = () =>
13-
(getCurrentInstance() && inject(vfmSymbol)) || activeVfm
28+
(getCurrentInstance() && inject(vfmSymbol, defaultVfm)) || activeVfm
1429

1530
export function createVfm() {
1631
const modals: ComputedRef<Modal>[] = shallowReactive([])
@@ -46,7 +61,7 @@ export function createVfm() {
4661
return vfm.toggle(modalId, false)
4762
},
4863
closeAll() {
49-
return Promise.allSettled([openedModals.map(modal => modal.value.toggle(false))])
64+
return Promise.allSettled(openedModals.map(modal => modal.value.toggle(false)))
5065
},
5166
})
5267

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { computed, inject, markRaw, nextTick, reactive, useAttrs } from 'vue'
22
import { tryOnUnmounted } from '@vueuse/core'
33
import VueFinalModal from './components/VueFinalModal/VueFinalModal.vue'
4-
import type VueFinalModal from './components/VueFinalModal/VueFinalModal.vue'
5-
import { internalVfmSymbol } from './injectionSymbols'
6-
74
import type { ComponentProps, ComponentType, InternalVfm, ModalSlot, ModalSlotOptions, UseModalOptions, UseModalOptionsPrivate, UseModalReturnType, Vfm } from './Modal'
85
import { activeVfm, getActiveVfm } from './plugin'
9-
import { isString } from '~/utils'
6+
import { internalVfmSymbol } from './injectionSymbols'
7+
import { isString, noop, noopPromise } from '~/utils'
108

119
/**
1210
* Returns the vfm instance. Equivalent to using `$vfm` inside
@@ -17,20 +15,28 @@ export function useVfm(): Vfm {
1715
if (__DEV__ && !vfm) {
1816
throw new Error(
1917
'[Vue Final Modal]: getActiveVfm was called with no active Vfm. Did you forget to install vfm?\n'
20-
+ '\tconst vfm = createVfm()\n'
21-
+ '\tapp.use(vfm)\n'
22-
+ 'This will fail in production.',
18+
+ '\tconst vfm = createVfm()\n'
19+
+ '\tapp.use(vfm)\n'
20+
+ 'This will fail in production.',
2321
)
2422
}
2523

2624
return vfm!
2725
}
2826

29-
/**
30-
* Returns the internalVfm instance.
31-
*/
32-
export function useInternalVfm(): InternalVfm {
33-
return inject(internalVfmSymbol)!
27+
export const defaultInternalVfm: InternalVfm = {
28+
openLastOverlay: noopPromise,
29+
moveToLastOpenedModals: noop,
30+
deleteFromOpenedModals: noop,
31+
moveToLastOpenedModalOverlays: noop,
32+
deleteFromOpenedModalOverlays: noop,
33+
deleteFromModals: noop,
34+
resolvedClosed: noop,
35+
resolvedOpened: noop,
36+
}
37+
38+
export function useInternalVfm() {
39+
return inject(internalVfmSymbol, defaultInternalVfm)
3440
}
3541

3642
function withMarkRaw<T extends ComponentType>(options: Partial<UseModalOptions<T>>, DefaultComponent: ComponentType = VueFinalModal) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const once
88
}
99

1010
export const noop = () => {}
11+
export const noopPromise = async () => {}
1112

1213
export function clamp(val: number, min: number, max: number) {
1314
return val > max ? max : val < min ? min : val

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const modal2 = useModal({
4747
background: 'interactive',
4848
},
4949
slots: {
50-
default: useModalSlot('test'),
50+
default: 'test'
5151
},
5252
})
5353

0 commit comments

Comments
 (0)