Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
13 changes: 9 additions & 4 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<my-el-with-styles></my-el-with-styles>`
const el = container.childNodes[0] as VueElement
const style = el.shadowRoot?.querySelector('style')!
expect(style.textContent).toBe(`div { color: red; }`)
assertStyles(el, [`div { color: red; }`, `div { color: green; }`])

// hmr
__VUE_HMR_RUNTIME__.reload('foo', {
Expand All @@ -866,7 +867,11 @@ describe('defineCustomElement', () => {
} as any)

await nextTick()
assertStyles(el, [`div { color: blue; }`, `div { color: yellow; }`])
assertStyles(el, [
`div { color: blue; }`,
`div { color: yellow; }`,
`div { color: green; }`,
])
})

test("child components should inject styles to root element's shadow root", async () => {
Expand Down
18 changes: 14 additions & 4 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type VueElementConstructor<P = {}> = {

export interface CustomElementOptions {
styles?: string[]
extraStyles?: string[]
shadowRoot?: boolean
nonce?: string
configureApp?: (app: App) => void
Expand Down Expand Up @@ -220,6 +221,10 @@ export class VueElement
* @internal
*/
_nonce: string | undefined = this._def.nonce
/**
* @internal
*/
_extraStyles?: string[] = this._def.extraStyles

/**
* @internal
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -586,18 +591,23 @@ export class VueElement
styles: string[] | undefined,
owner?: ConcreteComponent,
) {
if (!styles) return
const fullStyles: string[] = []
styles && fullStyles.push(...styles)
const { _extraStyles } = this
_extraStyles && fullStyles.push(..._extraStyles)
if (!fullStyles.length) return

if (owner) {
if (owner === this._def || this._styleChildren.has(owner)) {
return
}
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__) {
Expand Down