1
+ import { PortalTargetProps , Wormhole } from '@/types'
1
2
import {
2
3
ComponentOptions ,
3
4
createApp ,
4
5
defineComponent ,
5
6
getCurrentInstance ,
6
7
h ,
7
8
onBeforeUnmount ,
9
+ onMounted ,
8
10
} from 'vue'
9
11
import { useWormhole , wormholeSymbol } from '../composables/wormhole'
10
12
import { __DEV__ , assertStaticProps , inBrowser } from '../utils'
@@ -28,7 +30,6 @@ export default defineComponent({
28
30
} ,
29
31
order : { type : Number , default : 0 } ,
30
32
slotProps : { type : Object , default : ( ) => ( { } ) } ,
31
- tag : { type : String , default : 'DIV' } ,
32
33
// name for the target
33
34
to : {
34
35
type : String ,
@@ -40,7 +41,18 @@ export default defineComponent({
40
41
targetSlotProps : { type : Object , default : ( ) => ( { } ) } ,
41
42
} ,
42
43
setup ( props , { slots } ) {
44
+ __DEV__ &&
45
+ assertStaticProps ( 'Portal' , props , [
46
+ 'mountTo' ,
47
+ 'order' ,
48
+ 'name' ,
49
+ 'append' ,
50
+ 'multiple' ,
51
+ ] )
52
+
43
53
const wormhole = useWormhole ( )
54
+ usePortal ( props , slots )
55
+
44
56
if ( inBrowser ) {
45
57
const el = getTargetEl ( props . mountTo )
46
58
@@ -51,18 +63,9 @@ export default defineComponent({
51
63
__parent : getCurrentInstance ( ) ?. parent ,
52
64
}
53
65
54
- const app = createApp ( {
55
- // TODO: fix Component type error
56
- render : ( ) => h ( PortalTarget as ComponentOptions , targetProps ) ,
57
- } )
58
- app . provide ( wormholeSymbol , wormhole )
59
- onBeforeUnmount ( ( ) => {
60
- app . unmount ( el )
61
- } )
66
+ mountPortalTarget ( targetProps , wormhole , el )
62
67
}
63
68
64
- usePortal ( props , slots )
65
-
66
69
return ( ) => {
67
70
if ( props . disabled && slots . default ) {
68
71
return slots . default ( )
@@ -73,6 +76,24 @@ export default defineComponent({
73
76
} ,
74
77
} )
75
78
79
+ function mountPortalTarget (
80
+ targetProps : PortalTargetProps ,
81
+ wormhole : Wormhole ,
82
+ el : HTMLElement
83
+ ) {
84
+ const app = createApp ( {
85
+ // TODO: fix Component type error
86
+ render : ( ) => h ( PortalTarget as ComponentOptions , targetProps ) ,
87
+ } )
88
+
89
+ app . provide ( wormholeSymbol , wormhole )
90
+
91
+ onMounted ( ( ) => app . mount ( el ) )
92
+ onBeforeUnmount ( ( ) => {
93
+ app . unmount ( el )
94
+ } )
95
+ }
96
+
76
97
function getTargetEl ( mountTo : string ) : HTMLElement {
77
98
const el : HTMLElement | null = document . querySelector ( mountTo )
78
99
0 commit comments