1
1
import type { Component , VNode } from 'vue'
2
- import { h as _h , isVNode } from 'vue'
2
+ import { h as _h } from 'vue'
3
3
import { tryOnUnmounted } from '@vueuse/core'
4
4
import type { CreateVNodeOptions } from './Modal'
5
5
import { useSsrVfm , useVfm } from './useVfm'
6
6
import type { ComponentSlots } from './Component'
7
7
import { isString , objectEntries } from './utils'
8
8
9
9
/**
10
- * Create a dynamic vNode.
10
+ * Create a vNode by passing `CreateVNodeOptions<T>` options .
11
11
*/
12
12
export function createVNode < T extends Component > ( options : CreateVNodeOptions < T > ) {
13
- const id = Symbol ( __DEV__ ? 'createVNode' : '' )
14
- const vNode = _h ( options . component , { key : id , ...options . attrs } , getSlots ( options . slots ) )
15
- return vNode
13
+ return _h ( options . component , options . attrs , getSlots ( options . slots ) )
16
14
}
17
15
18
16
/**
19
- * Create a dynamic vNode.
17
+ * A type helper for `CreateVNodeOptions<T>`
20
18
*/
21
19
export function h < T extends Component > ( options : CreateVNodeOptions < T > ) {
22
20
return options
23
21
}
24
22
25
- async function pushVNode ( vNode : VNode ) {
26
- const vfm = await useSsrVfm ( )
27
- vfm . vNodesContainer . push ( vNode )
28
- }
23
+ export function useVNode < T extends Component > ( vNodeOptions : CreateVNodeOptions < T > , options ?: {
24
+ onUnmounted ?: ( vNode : VNode ) => void
25
+ } ) {
26
+ if ( vNodeOptions . attrs ) {
27
+ const id = Symbol ( __DEV__ ? 'createVNode' : '' )
28
+ Object . assign ( vNodeOptions . attrs , { key : id } )
29
+ }
30
+ const vNode = createVNode ( vNodeOptions )
31
+ tryOnUnmounted ( ( ) => {
32
+ if ( options ?. onUnmounted )
33
+ options ?. onUnmounted ( vNode )
34
+ else
35
+ hide ( )
36
+ } )
29
37
30
- async function removeVNode ( vNode : VNode ) {
31
- const vfm = useVfm ( )
32
- vfm . vNodesContainer . remove ( vNode )
33
- }
38
+ async function show ( ) {
39
+ const vfm = await useSsrVfm ( )
40
+ vfm . vNodesContainer . push ( vNode )
41
+ }
42
+
43
+ async function hide ( ) {
44
+ const vfm = useVfm ( )
45
+ vfm . vNodesContainer . remove ( vNode )
46
+ }
34
47
35
- export function useVNode ( vNode : VNode , options ?: {
36
- tryOnUnmounted ?: ( vNode : VNode ) => void
37
- } ) {
38
- tryOnUnmounted ( ( ) => options ?. tryOnUnmounted ?.( vNode ) )
39
48
return {
40
- show : ( ) => pushVNode ( vNode ) ,
41
- hide : ( ) => removeVNode ( vNode ) ,
49
+ show,
50
+ hide,
42
51
}
43
52
}
44
53
@@ -49,7 +58,7 @@ export function isVNodeOptions<T extends Component>(value: unknown): value is Cr
49
58
return false
50
59
}
51
60
52
- export function getSlots < T extends Component > ( slots ?: {
61
+ function getSlots < T extends Component > ( slots ?: {
53
62
[ K in keyof ComponentSlots < T > ] ?: string | Component | CreateVNodeOptions < Component >
54
63
} ) {
55
64
return objectEntries ( slots || { } ) . reduce < Record < string , ( ) => VNode > > ( ( acc , cur ) => {
@@ -59,9 +68,6 @@ export function getSlots<T extends Component>(slots?: {
59
68
acc [ slotName ] = ( ) => _h ( 'div' , { innerHTML : slot } )
60
69
else if ( isVNodeOptions ( slot ) )
61
70
acc [ slotName ] = ( ) => _h ( slot . component , slot . attrs , slot . slots ? getSlots ( slot . slots ) : undefined )
62
- else if ( isVNode ( slot ) )
63
- // acc[slotName] = () => slot
64
- return acc
65
71
else
66
72
acc [ slotName ] = ( ) => _h ( slot )
67
73
return acc
0 commit comments