Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime-dom): support mount app on ShadowRoot #2447

Merged
merged 2 commits into from
Dec 4, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions packages/runtime-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const createApp = ((...args) => {
}

const { mount } = app
app.mount = (containerOrSelector: Element | string): any => {
app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
const container = normalizeContainer(containerOrSelector)
if (!container) return
const component = app._component
Expand All @@ -68,8 +68,10 @@ export const createApp = ((...args) => {
// clear content before mounting
container.innerHTML = ''
const proxy = mount(container)
container.removeAttribute('v-cloak')
container.setAttribute('data-v-app', '')
if (!container.__isShadowRoot) {
unbyte marked this conversation as resolved.
Show resolved Hide resolved
container.removeAttribute('v-cloak')
container.setAttribute('data-v-app', '')
}
return proxy
}

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

const { mount } = app
app.mount = (containerOrSelector: Element | string): any => {
app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
const container = normalizeContainer(containerOrSelector)
if (container) {
return mount(container, true)
Expand All @@ -103,14 +105,25 @@ function injectNativeTagCheck(app: App) {
})
}

function normalizeContainer(container: Element | string): Element | null {
function normalizeContainer(
container: Element | ShadowRoot | string
): Element & { __isShadowRoot?: boolean } | null {
if (isString(container)) {
const res = document.querySelector(container)
if (__DEV__ && !res) {
warn(`Failed to mount app: mount target selector returned null.`)
}
return res
}
if (container instanceof ShadowRoot) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mode: 'closed' doesn't actually prevent Vue from mounting to it (see https://blog.revillweb.com/open-vs-closed-shadow-dom-9f3d7427d1af) so this whole check is unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no in-depth study yet, but at least input can't work
https://codepen.io/unbyte/pen/XWKjaVO?editors=1010

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know why now. It's a regression of Vimium, an extension that I enabled in my browsers. philc/vimium#853

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I wonder if warnings should be retained so that developers can be aware that some of the problems may be caused by the closed shadow root, for what the user side is using is beyond developers' control

if (__DEV__ && container.mode === 'closed') {
warn(
`mounting on a ShadowRoot with \`{mode: "closed"}\` may lead to unpredictable bugs`
)
}
;(container as any).__isShadowRoot = true
return container as any
}
return container
}

Expand Down