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

fix(custom-element): ensure correct order of nested component styles #13030

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,39 @@ describe('defineCustomElement', () => {
assertStyles(el, [`div { color: blue; }`, `div { color: red; }`])
})

test('child components styles should before parent styles', async () => {
const Baz = () => h(Bar)
const Bar = defineComponent({
styles: [`div { color: green; }`],
render() {
return 'bar'
},
})
const WarpperBar = defineComponent({
styles: [`div { color: blue; }`],
render() {
return h(Baz)
},
})
const WBaz = () => h(WarpperBar)
const Foo = defineCustomElement({
styles: [`div { color: red; }`],
render() {
return [h(Baz), h(WBaz)]
},
})
customElements.define('my-el-with-wrapper-child-styles', Foo)
container.innerHTML = `<my-el-with-wrapper-child-styles></my-el-with-wrapper-child-styles>`
const el = container.childNodes[0] as VueElement

// inject order should be child -> parent
assertStyles(el, [
`div { color: green; }`,
`div { color: blue; }`,
`div { color: red; }`,
])
})

test('with nonce', () => {
const Foo = defineCustomElement(
{
Expand Down
16 changes: 13 additions & 3 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class VueElement
private _connected = false
private _resolved = false
private _numberProps: Record<string, true> | null = null
private _styleChildren = new WeakSet()
private _styleChildren = new WeakMap<ConcreteComponent, HTMLStyleElement[]>()
private _pendingResolve: Promise<void> | undefined
private _parent: VueElement | undefined
/**
Expand Down Expand Up @@ -581,18 +581,28 @@ export class VueElement
owner?: ConcreteComponent,
) {
if (!styles) return
const styleList: HTMLStyleElement[] = []
if (owner) {
if (owner === this._def || this._styleChildren.has(owner)) {
if (owner === this._def) {
return
}
this._styleChildren.add(owner)
const styleChild = this._styleChildren.get(owner)
if (styleChild) {
this.shadowRoot!.prepend(...styleChild)
return
}
this._styleChildren.set(owner, styleList)
}

const nonce = this._nonce
for (let i = styles.length - 1; i >= 0; i--) {
const s = document.createElement('style')
if (nonce) s.setAttribute('nonce', nonce)
s.textContent = styles[i]
this.shadowRoot!.prepend(s)
if (owner) {
styleList.unshift(s)
}
// record for HMR
if (__DEV__) {
if (owner) {
Expand Down