-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
mountPortalTarget.ts
38 lines (36 loc) · 1022 Bytes
/
mountPortalTarget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { PortalTargetProps } from '../types'
import {
ComponentInternalInstance,
ComponentOptions,
createApp,
getCurrentInstance,
h,
onBeforeUnmount,
onMounted,
} from 'vue'
import PortalTarget from '../components/portal-target'
export function mountPortalTarget(
targetProps: PortalTargetProps,
el: HTMLElement | string
) {
const app = createApp({
// TODO: fix Component type error
render: () => h(PortalTarget as ComponentOptions, targetProps),
})
if (!targetProps.multiple) {
// this is hacky as it relies on internals, but works.
// TODO: can we get rid of this by somehow properly replacing the target's .parent?
const provides =
(getCurrentInstance() as ComponentInternalInstance & {
provides: Record<string, any>
}).provides ?? {}
app._context.provides = Object.create(provides)
//Object.assign(app._context.provides, Object.create(provides))
}
onMounted(() => {
app.mount(el)
})
onBeforeUnmount(() => {
app.unmount()
})
}