Skip to content

Commit 41379c4

Browse files
committed
wip: save
1 parent a60bb6d commit 41379c4

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

packages/runtime-dom/__tests__/customElement.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,49 @@ describe('defineCustomElement', () => {
977977
assertStyles(el, [`div { color: green; }`, `div { color: blue; }`])
978978
})
979979

980+
test('inject nested child component styles', async () => {
981+
const Baz = defineComponent({
982+
styles: [`div { color: yellow; }`],
983+
render() {
984+
return h(Bar)
985+
},
986+
})
987+
const Bar = defineComponent({
988+
styles: [`div { color: green; }`],
989+
render() {
990+
return 'bar'
991+
},
992+
})
993+
const WrapperBar = defineComponent({
994+
styles: [`div { color: blue; }`],
995+
render() {
996+
return h(Baz)
997+
},
998+
})
999+
const WBaz = defineComponent({
1000+
styles: [`div { color: black; }`],
1001+
render() {
1002+
return h(WrapperBar)
1003+
},
1004+
})
1005+
const Foo = defineCustomElement({
1006+
styles: [`div { color: red; }`],
1007+
render() {
1008+
return [h(Baz), h(WBaz)]
1009+
},
1010+
})
1011+
customElements.define('my-el-with-inject-nested-child-styles', Foo)
1012+
container.innerHTML = `<my-el-with-inject-nested-child-styles></my-el-with-inject-nested-child-styles>`
1013+
const el = container.childNodes[0] as VueElement
1014+
assertStyles(el, [
1015+
`div { color: green; }`,
1016+
`div { color: yellow; }`,
1017+
`div { color: blue; }`,
1018+
`div { color: black; }`,
1019+
`div { color: red; }`,
1020+
])
1021+
})
1022+
9801023
test('with nonce', () => {
9811024
const Foo = defineCustomElement(
9821025
{

packages/runtime-dom/src/apiCustomElement.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ export class VueElement
232232
private _styleChildren = new WeakSet()
233233
private _pendingResolve: Promise<void> | undefined
234234
private _parent: VueElement | undefined
235-
private _styleAnchor?: HTMLStyleElement | Text
235+
private _styleAnchors: WeakMap<ConcreteComponent, HTMLStyleElement | Text> =
236+
new WeakMap()
236237
/**
237238
* dev only
238239
*/
@@ -599,12 +600,13 @@ export class VueElement
599600
// to inject child styles before it.
600601
if (parentComp && !parentComp.styles) {
601602
const anchor = document.createTextNode('')
602-
if (this._styleAnchor) {
603-
this.shadowRoot!.insertBefore(anchor, this._styleAnchor)
603+
const styleAnchor = this._styleAnchors.get(this._def)
604+
if (styleAnchor) {
605+
this.shadowRoot!.insertBefore(anchor, styleAnchor)
604606
} else {
605607
this.shadowRoot!.prepend(anchor)
606608
}
607-
this._styleAnchor = anchor
609+
this._styleAnchors.set(this._def, anchor)
608610
}
609611

610612
const nonce = this._nonce
@@ -616,12 +618,19 @@ export class VueElement
616618

617619
// inject styles before parent styles
618620
if (parentComp) {
619-
this.shadowRoot!.insertBefore(s, last || this._styleAnchor!)
621+
this.shadowRoot!.insertBefore(
622+
s,
623+
last ||
624+
this._styleAnchors.get(parentComp) ||
625+
this._styleAnchors.get(this._def) ||
626+
null,
627+
)
620628
} else {
621629
this.shadowRoot!.prepend(s)
622-
this._styleAnchor = s
630+
this._styleAnchors.set(this._def, s)
623631
}
624632
last = s
633+
if (owner && i === 0) this._styleAnchors.set(owner, s)
625634

626635
// record for HMR
627636
if (__DEV__) {

0 commit comments

Comments
 (0)