Skip to content

Commit b2189ba

Browse files
authored
fix(runtime-dom): support mounting app on ShadowRoot (#2447)
fix #2399
1 parent 338d869 commit b2189ba

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

packages/runtime-dom/src/index.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const createApp = ((...args) => {
5858
}
5959

6060
const { mount } = app
61-
app.mount = (containerOrSelector: Element | string): any => {
61+
app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
6262
const container = normalizeContainer(containerOrSelector)
6363
if (!container) return
6464
const component = app._component
@@ -68,8 +68,10 @@ export const createApp = ((...args) => {
6868
// clear content before mounting
6969
container.innerHTML = ''
7070
const proxy = mount(container)
71-
container.removeAttribute('v-cloak')
72-
container.setAttribute('data-v-app', '')
71+
if (container instanceof Element) {
72+
container.removeAttribute('v-cloak')
73+
container.setAttribute('data-v-app', '')
74+
}
7375
return proxy
7476
}
7577

@@ -84,7 +86,7 @@ export const createSSRApp = ((...args) => {
8486
}
8587

8688
const { mount } = app
87-
app.mount = (containerOrSelector: Element | string): any => {
89+
app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
8890
const container = normalizeContainer(containerOrSelector)
8991
if (container) {
9092
return mount(container, true)
@@ -103,15 +105,26 @@ function injectNativeTagCheck(app: App) {
103105
})
104106
}
105107

106-
function normalizeContainer(container: Element | string): Element | null {
108+
function normalizeContainer(
109+
container: Element | ShadowRoot | string
110+
): Element | null {
107111
if (isString(container)) {
108112
const res = document.querySelector(container)
109113
if (__DEV__ && !res) {
110114
warn(`Failed to mount app: mount target selector returned null.`)
111115
}
112116
return res
113117
}
114-
return container
118+
if (
119+
__DEV__ &&
120+
container instanceof ShadowRoot &&
121+
container.mode === 'closed'
122+
) {
123+
warn(
124+
`mounting on a ShadowRoot with \`{mode: "closed"}\` may lead to unpredictable bugs`
125+
)
126+
}
127+
return container as any
115128
}
116129

117130
// SFC CSS utilities

0 commit comments

Comments
 (0)