Skip to content

Commit 7949452

Browse files
committed
refactor: split out useModal, useVfm, useC2v to individual files
1 parent 6d87697 commit 7949452

File tree

9 files changed

+290
-272
lines changed

9 files changed

+290
-272
lines changed

packages/vue-final-modal/src/components/DynamicModal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Component, PropType } from 'vue'
22
import { defineComponent, h } from 'vue'
33
import type { C2VOptions, UseModalOptions, UseModalOptionsPrivate } from '..'
4-
import { getSlots, isC2VOptions } from '~/useApi'
54
import { isString, objectEntries } from '~/utils'
5+
import { getSlots, isC2VOptions } from '~/useC2v'
66

77
export const DynamicModal = defineComponent({
88
name: 'DynamicModal',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { computed, defineComponent, onBeforeUnmount } from 'vue'
2-
import { useVfm } from '~/useApi'
2+
import { useVfm } from '~/useVfm'
33

44
export const ModalsContainer = defineComponent({
55
name: 'ModalsContainer',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { vVisible } from './vVisible'
1111
import { arrayMoveItemToLast, arrayRemoveItem, noop, once } from '~/utils'
1212
import { type ModalExposed } from '~/Modal'
1313
import { useSwipeToClose } from '~/useSwipeToClose'
14-
import { useVfm } from '~/useApi'
14+
import { useVfm } from '~/useVfm'
1515
import { getModalExposed } from '~/plugin'
1616
1717
export interface VueFinalModalEmits {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export { vueFinalModalProps } from './components/VueFinalModal/VueFinalModalProp
2121
export type { VueFinalModalEmits } from './components/VueFinalModal/VueFinalModal.vue'
2222

2323
/** Composables */
24-
export { useVfm, useModal, useVfmAttrs, useC2v, c2v } from './useApi'
24+
export { useC2v, c2v } from './useC2v'
25+
export { useVfm } from './useVfm'
26+
export { useModal } from './useModal'
27+
export { useVfmAttrs } from './useVfmAttrs'
2528

2629
declare module '@vue/runtime-core' {
2730
export interface ComponentCustomProperties {

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

Lines changed: 0 additions & 268 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { Component, VNode } from 'vue'
2+
import { h } from 'vue'
3+
import { tryOnUnmounted } from '@vueuse/core'
4+
import type { C2VOptions } from './Modal'
5+
import { pushVNode, removeVNode } from './useVfm'
6+
import type { ComponentSlots } from './Component'
7+
import { isString, objectEntries } from './utils'
8+
9+
/**
10+
* Create a dynamic vNode.
11+
*/
12+
export function useC2v<T extends Component>(_options: C2VOptions<T>) {
13+
const id = Symbol(__DEV__ ? 'useC2v' : '')
14+
15+
const vNode = h(_options.component, { key: id, ..._options.attrs }, _options.slots)
16+
17+
tryOnUnmounted(() => {
18+
removeVNode(vNode)
19+
})
20+
21+
return {
22+
open: () => pushVNode(vNode),
23+
close: () => removeVNode(vNode),
24+
}
25+
}
26+
27+
/** Convert Component Options to VNode */
28+
export function c2v<T extends Component>(options: C2VOptions<T>) {
29+
return options
30+
}
31+
32+
export function isC2VOptions<T extends Component>(value: unknown): value is C2VOptions<T> {
33+
if (typeof value === 'object' && value !== null)
34+
return 'component' in value
35+
else
36+
return false
37+
}
38+
39+
export function getSlots<T extends Component>(slots: {
40+
[K in keyof ComponentSlots<T>]?: string | Component | C2VOptions<Component>
41+
}) {
42+
return objectEntries(slots || {}).reduce((acc, cur) => {
43+
const slotName = cur[0] as string
44+
const slot = cur[1] as string | Component | C2VOptions<any>
45+
if (isString(slot))
46+
acc[slotName] = () => h('div', { innerHTML: slot })
47+
else if (isC2VOptions(slot))
48+
acc[slotName] = () => h(slot.component, slot.attrs, slot.slots ? getSlots(slot.slots) : undefined)
49+
else
50+
acc[slotName] = () => h(slot)
51+
return acc
52+
}, {} as Record<string, () => VNode>)
53+
}

0 commit comments

Comments
 (0)