feat(runtime-dom): support specifying shadow dom styles in defineCustomElement
This commit is contained in:
parent
f0ca233d8b
commit
a7fa4ac28a
@ -274,4 +274,20 @@ describe('defineCustomElement', () => {
|
|||||||
expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
|
expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('styles', () => {
|
||||||
|
test('should attach styles to shadow dom', () => {
|
||||||
|
const Foo = defineCustomElement({
|
||||||
|
styles: [`div { color: red; }`],
|
||||||
|
render() {
|
||||||
|
return h('div', 'hello')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
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; }`)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import {
|
import {
|
||||||
Component,
|
|
||||||
ComponentOptionsMixin,
|
ComponentOptionsMixin,
|
||||||
ComponentOptionsWithArrayProps,
|
ComponentOptionsWithArrayProps,
|
||||||
ComponentOptionsWithObjectProps,
|
ComponentOptionsWithObjectProps,
|
||||||
@ -18,7 +17,8 @@ import {
|
|||||||
createVNode,
|
createVNode,
|
||||||
defineComponent,
|
defineComponent,
|
||||||
nextTick,
|
nextTick,
|
||||||
warn
|
warn,
|
||||||
|
ComponentOptions
|
||||||
} from '@vue/runtime-core'
|
} from '@vue/runtime-core'
|
||||||
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
|
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
|
||||||
import { hydrate, render } from '.'
|
import { hydrate, render } from '.'
|
||||||
@ -60,7 +60,7 @@ export function defineCustomElement<
|
|||||||
Extends,
|
Extends,
|
||||||
E,
|
E,
|
||||||
EE
|
EE
|
||||||
>
|
> & { styles?: string[] }
|
||||||
): VueElementConstructor<Props>
|
): VueElementConstructor<Props>
|
||||||
|
|
||||||
// overload 3: object format with array props declaration
|
// overload 3: object format with array props declaration
|
||||||
@ -85,7 +85,7 @@ export function defineCustomElement<
|
|||||||
Extends,
|
Extends,
|
||||||
E,
|
E,
|
||||||
EE
|
EE
|
||||||
>
|
> & { styles?: string[] }
|
||||||
): VueElementConstructor<{ [K in PropNames]: any }>
|
): VueElementConstructor<{ [K in PropNames]: any }>
|
||||||
|
|
||||||
// overload 4: object format with object props declaration
|
// overload 4: object format with object props declaration
|
||||||
@ -110,7 +110,7 @@ export function defineCustomElement<
|
|||||||
Extends,
|
Extends,
|
||||||
E,
|
E,
|
||||||
EE
|
EE
|
||||||
>
|
> & { styles?: string[] }
|
||||||
): VueElementConstructor<ExtractPropTypes<PropsOptions>>
|
): VueElementConstructor<ExtractPropTypes<PropsOptions>>
|
||||||
|
|
||||||
// overload 5: defining a custom element from the returned value of
|
// overload 5: defining a custom element from the returned value of
|
||||||
@ -176,7 +176,7 @@ export class VueElement extends BaseClass {
|
|||||||
_connected = false
|
_connected = false
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private _def: Component,
|
private _def: ComponentOptions & { styles?: string[] },
|
||||||
private _attrKeys: string[],
|
private _attrKeys: string[],
|
||||||
private _propKeys: string[],
|
private _propKeys: string[],
|
||||||
hydrate?: RootHydrateFunction
|
hydrate?: RootHydrateFunction
|
||||||
@ -192,6 +192,13 @@ export class VueElement extends BaseClass {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
this.attachShadow({ mode: 'open' })
|
this.attachShadow({ mode: 'open' })
|
||||||
|
if (_def.styles) {
|
||||||
|
_def.styles.forEach(css => {
|
||||||
|
const s = document.createElement('style')
|
||||||
|
s.textContent = css
|
||||||
|
this.shadowRoot!.appendChild(s)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user