11import type { Component , VNode } from 'vue'
2- import { h as _h , isVNode } from 'vue'
2+ import { h as _h } from 'vue'
33import { tryOnUnmounted } from '@vueuse/core'
44import type { CreateVNodeOptions } from './Modal'
55import { useSsrVfm , useVfm } from './useVfm'
66import type { ComponentSlots } from './Component'
77import { isString , objectEntries } from './utils'
88
99/**
10- * Create a dynamic vNode.
10+ * Create a vNode by passing `CreateVNodeOptions<T>` options .
1111 */
1212export 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 ) )
1614}
1715
1816/**
19- * Create a dynamic vNode.
17+ * A type helper for `CreateVNodeOptions<T>`
2018 */
2119export function h < T extends Component > ( options : CreateVNodeOptions < T > ) {
2220 return options
2321}
2422
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+ } )
2937
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+ }
3447
35- export function useVNode ( vNode : VNode , options ?: {
36- tryOnUnmounted ?: ( vNode : VNode ) => void
37- } ) {
38- tryOnUnmounted ( ( ) => options ?. tryOnUnmounted ?.( vNode ) )
3948 return {
40- show : ( ) => pushVNode ( vNode ) ,
41- hide : ( ) => removeVNode ( vNode ) ,
49+ show,
50+ hide,
4251 }
4352}
4453
@@ -49,7 +58,7 @@ export function isVNodeOptions<T extends Component>(value: unknown): value is Cr
4958 return false
5059}
5160
52- export function getSlots < T extends Component > ( slots ?: {
61+ function getSlots < T extends Component > ( slots ?: {
5362 [ K in keyof ComponentSlots < T > ] ?: string | Component | CreateVNodeOptions < Component >
5463} ) {
5564 return objectEntries ( slots || { } ) . reduce < Record < string , ( ) => VNode > > ( ( acc , cur ) => {
@@ -59,9 +68,6 @@ export function getSlots<T extends Component>(slots?: {
5968 acc [ slotName ] = ( ) => _h ( 'div' , { innerHTML : slot } )
6069 else if ( isVNodeOptions ( slot ) )
6170 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
6571 else
6672 acc [ slotName ] = ( ) => _h ( slot )
6773 return acc
0 commit comments