diff --git a/packages/runtime-dom/__tests__/customElement.spec.ts b/packages/runtime-dom/__tests__/customElement.spec.ts
index 07ea091486e..79ad4d36f54 100644
--- a/packages/runtime-dom/__tests__/customElement.spec.ts
+++ b/packages/runtime-dom/__tests__/customElement.spec.ts
@@ -852,12 +852,13 @@ describe('defineCustomElement', () => {
return h('div', 'hello')
},
})
- const Foo = defineCustomElement(def)
+ const Foo = defineCustomElement(def, {
+ extraStyles: [`div { color: green; }`],
+ })
customElements.define('my-el-with-styles', Foo)
container.innerHTML = `
= { export interface CustomElementOptions { styles?: string[] + extraStyles?: string[] shadowRoot?: boolean nonce?: string configureApp?: (app: App) => void @@ -220,6 +221,10 @@ export class VueElement * @internal */ _nonce: string | undefined = this._def.nonce + /** + * @internal + */ + _extraStyles?: string[] = this._def.extraStyles /** * @internal @@ -391,7 +396,7 @@ export class VueElement // apply CSS if (this.shadowRoot) { this._applyStyles(styles) - } else if (__DEV__ && styles) { + } else if (__DEV__ && (styles || this._extraStyles)) { warn( 'Custom element style injection is not supported when using ' + 'shadowRoot: false', @@ -586,7 +591,17 @@ export class VueElement styles: string[] | undefined, owner?: ConcreteComponent, ) { - if (!styles) return + const fullStyles: string[] = [] + if (styles && styles.length) { + fullStyles.push(...styles) + } + // Only apply extraStyles for the root element injection. + if (!owner) { + const { _extraStyles } = this + _extraStyles && fullStyles.push(..._extraStyles) + } + if (!fullStyles.length) return + if (owner) { if (owner === this._def || this._styleChildren.has(owner)) { return @@ -594,10 +609,10 @@ export class VueElement this._styleChildren.add(owner) } const nonce = this._nonce - for (let i = styles.length - 1; i >= 0; i--) { + for (let i = fullStyles.length - 1; i >= 0; i--) { const s = document.createElement('style') if (nonce) s.setAttribute('nonce', nonce) - s.textContent = styles[i] + s.textContent = fullStyles[i] this.shadowRoot!.prepend(s) // record for HMR if (__DEV__) {