forked from LinusBorg/portal-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmounting-portal.tsx
158 lines (137 loc) · 3.72 KB
/
mounting-portal.tsx
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Vue from 'vue'
import { VNode, VueConstructor, PropOptions } from 'vue'
import Portal from './portal'
import PortalTarget from './portal-target'
import { wormhole } from './wormhole'
import { pick } from '@/utils'
import { PropWithComponent } from '../types'
let _id = 0
export type withPortalTarget = VueConstructor<
Vue & {
portalTarget: any
}
>
const portalProps = [
'disabled',
'name',
'order',
'slim',
'slotProps',
'tag',
'to',
]
const targetProps = ['multiple', 'transition']
export default (Vue as withPortalTarget).extend({
name: 'MountingPortal',
inheritAttrs: false,
props: {
append: { type: [Boolean, String] },
bail: {
type: Boolean,
},
mountTo: { type: String, required: true },
// Portal
disabled: { type: Boolean },
// name for the portal
name: {
type: String,
default: () => 'mounted_' + String(_id++),
},
order: { type: Number, default: 0 },
slim: { type: Boolean },
slotProps: { type: Object, default: () => ({}) },
tag: { type: String, default: 'DIV' },
// name for the target
to: {
type: String,
default: () => String(Math.round(Math.random() * 10000000)),
},
// Target
multiple: { type: Boolean, default: false },
targetSlim: { type: Boolean },
targetSlotProps: { type: Object, default: () => ({}) },
targetTag: { type: String, default: 'div' },
transition: { type: [String, Object, Function] } as PropOptions<
PropWithComponent
>,
},
created() {
if (typeof document === 'undefined') return
let el: HTMLElement | null = document.querySelector(this.mountTo)
if (!el) {
console.error(
`[portal-vue]: Mount Point '${this.mountTo}' not found in document`
)
return
}
const props = this.$props
// Target already exists
if (wormhole.targets[props.name]) {
if (props.bail) {
console.warn(`[portal-vue]: Target ${props.name} is already mounted.
Aborting because 'bail: true' is set`)
} else {
this.portalTarget = wormhole.targets[props.name]
}
return
}
const { append } = props
if (append) {
const type = typeof append === 'string' ? append : 'DIV'
const mountEl = document.createElement(type)
el.appendChild(mountEl)
el = mountEl
}
// get props for target from $props
// we have to rename a few of them
const _props = pick(this.$props, targetProps)
_props.slim = this.targetSlim
_props.tag = this.targetTag
_props.slotProps = this.targetSlotProps
_props.name = this.to
this.portalTarget = new PortalTarget({
el,
parent: this.$parent || this,
propsData: _props,
})
},
beforeDestroy() {
const target = this.portalTarget
if (this.append) {
const el = target.$el
el.parentNode.removeChild(el)
}
target.$destroy()
},
render(h): VNode {
if (!this.portalTarget) {
console.warn("[portal-vue] Target wasn't mounted")
return h()
}
// if there's no "manual" scoped slot, so we create a <Portal> ourselves
if (!this.$scopedSlots.manual) {
const props = pick(this.$props, portalProps)
return h(
Portal,
{
props: props,
attrs: this.$attrs,
on: this.$listeners,
scopedSlots: this.$scopedSlots,
},
this.$slots.default
)
}
// else, we render the scoped slot
let content: VNode = (this.$scopedSlots.manual({
to: this.to,
}) as unknown) as VNode
// if user used <template> for the scoped slot
// content will be an array
if (Array.isArray(content)) {
content = content[0]
}
if (!content) return h()
return content
},
})