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): Apply nested component styles when using defineCustomElements #4309

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 48 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,54 @@ describe('defineCustomElement', () => {
const style = el.shadowRoot?.querySelector('style')!
expect(style.textContent).toBe(`div { color: red; }`)
})

test('should attach styles recursively to shadow dom', () => {
const SecondDepthChild1 = {
styles: [`div { color: yellow; }`],
render() {
return h('div', '2nd')
}
}
const SecondDepthChild2 = {
styles: [`div { color: green; }`],
render() {
return h('div', '2nd')
}
}
const FirstDepthChild = {
styles: [`div { color: blue; }`],
components: { SecondDepthChild1, SecondDepthChild2 },
render() {
return h('div', [
'1st',
h('SecondDepthChild1'),
h('SecondDepthChild2')
])
}
}
const RootComponent = {
styles: [`div { color: red; }`],
components: { FirstDepthChild },
render() {
return h('div', ['root', h('FirstDepthChild')])
}
}

const customElement = defineCustomElement(RootComponent)
customElements.define('my-el-with-recursive-styles', customElement)

container.innerHTML = `<my-el-with-recursive-styles></my-el-with-recursive-styles>`
const el = container.childNodes[0] as VueElement
const styles = el.shadowRoot?.querySelectorAll('style')!
const styleTexts = Array.from(styles).map(style => style.textContent)

expect(styleTexts).toEqual([
...RootComponent.styles,
...FirstDepthChild.styles,
...SecondDepthChild1.styles,
...SecondDepthChild2.styles
])
})
})

describe('async', () => {
Expand Down
29 changes: 27 additions & 2 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
defineComponent,
nextTick,
warn,
Component,
ConcreteComponent,
ComponentOptions
} from '@vue/runtime-core'
Expand Down Expand Up @@ -221,7 +222,7 @@ export class VueElement extends BaseClass {
this._setProp(key, this[key as keyof this])
}
}
const { props, styles } = def
const { props } = def
// defining getter/setters on prototype
const rawKeys = props ? (isArray(props) ? props : Object.keys(props)) : []
for (const key of rawKeys.map(camelize)) {
Expand All @@ -234,7 +235,7 @@ export class VueElement extends BaseClass {
}
})
}
this._applyStyles(styles)
this._applyStyles(this._getStylesRecursively(def))
}

const asyncDef = (this._def as ComponentOptions).__asyncLoader
Expand Down Expand Up @@ -341,4 +342,28 @@ export class VueElement extends BaseClass {
})
}
}

private _getStylesRecursively(
Copy link
Contributor

Choose a reason for hiding this comment

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

This only addresses styles for locally registered non-async components. Async or global components would still have no styles.

Copy link

Choose a reason for hiding this comment

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

I consider a web component to be an isolated and self-contained component in the shadow DOM, which is fully loaded and registered as a module.
I would be concerned if code was dynamically reloaded in the shadow DOM, so I don't see the need for async components.

component: Component & {
components?: Record<string, Component>
styles?: string[]
}
): string[] {
const customElementStyles: string[] = []

if (component.styles) {
customElementStyles.push(...component.styles)
}

const childComponents = component.components
if (childComponents) {
Object.keys(childComponents).forEach(name => {
const childComponent = childComponents[name]
const styles = this._getStylesRecursively(childComponent)
customElementStyles.push(...styles)
})
}

return customElementStyles
}
}