Skip to content

Commit 5b6716f

Browse files
author
Thorsten Luenborg
committed
examples: MountingPortal works
1 parent 83d00ee commit 5b6716f

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

example/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</head>
77
<body>
88
<div id="app"></div>
9+
<div id="external-target"></div>
910
<script type="application/json">
1011
{
1112
"test": "this should not be complied."

example/router.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Vue from 'vue'
21
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
32

43
import ToggleExample from './components/toggle/toggle-example.vue'
@@ -11,7 +10,7 @@ import Programmatic from './components/programmatic/index.vue'
1110
import RouterViewWithPortals from './components/router-view-with-portals/index.vue'
1211
import RouterViewWithPortalsA from './components/router-view-with-portals/a.vue'
1312
import RouterViewWithPortalsB from './components/router-view-with-portals/b.vue'
14-
// import MountToExternal from './components/mount-to/mount-to-external.vue'
13+
import MountToExternal from './components/mount-to/mount-to-external.vue'
1514
import EmptyPortal from './components/empty-portal/index.vue'
1615
import DefaultSlotContent from './components/default-content-on-target/index.vue'
1716
import Transitions from './components/transitions/transitions.vue'
@@ -71,10 +70,10 @@ const routes: RouteRecordRaw[] = [
7170
path: '/transitions',
7271
component: Transitions,
7372
},
74-
// {
75-
// path: '/Mount-to-external-element',
76-
// component: MountToExternal,
77-
// },
73+
{
74+
path: '/Mount-to-external-element',
75+
component: MountToExternal,
76+
},
7877
{
7978
path: '/multiple',
8079
component: Multiple,

src/components/mounting-portal.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { PortalTargetProps, Wormhole } from '@/types'
12
import {
23
ComponentOptions,
34
createApp,
45
defineComponent,
56
getCurrentInstance,
67
h,
78
onBeforeUnmount,
9+
onMounted,
810
} from 'vue'
911
import { useWormhole, wormholeSymbol } from '../composables/wormhole'
1012
import { __DEV__, assertStaticProps, inBrowser } from '../utils'
@@ -28,7 +30,6 @@ export default defineComponent({
2830
},
2931
order: { type: Number, default: 0 },
3032
slotProps: { type: Object, default: () => ({}) },
31-
tag: { type: String, default: 'DIV' },
3233
// name for the target
3334
to: {
3435
type: String,
@@ -40,7 +41,18 @@ export default defineComponent({
4041
targetSlotProps: { type: Object, default: () => ({}) },
4142
},
4243
setup(props, { slots }) {
44+
__DEV__ &&
45+
assertStaticProps('Portal', props, [
46+
'mountTo',
47+
'order',
48+
'name',
49+
'append',
50+
'multiple',
51+
])
52+
4353
const wormhole = useWormhole()
54+
usePortal(props, slots)
55+
4456
if (inBrowser) {
4557
const el = getTargetEl(props.mountTo)
4658

@@ -51,18 +63,9 @@ export default defineComponent({
5163
__parent: getCurrentInstance()?.parent,
5264
}
5365

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)
6267
}
6368

64-
usePortal(props, slots)
65-
6669
return () => {
6770
if (props.disabled && slots.default) {
6871
return slots.default()
@@ -73,6 +76,24 @@ export default defineComponent({
7376
},
7477
})
7578

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+
7697
function getTargetEl(mountTo: string): HTMLElement {
7798
const el: HTMLElement | null = document.querySelector(mountTo)
7899

0 commit comments

Comments
 (0)