feat: show error when has styles & not using shadow roots

This commit is contained in:
Ayo Ayco 2025-03-30 12:46:43 +02:00
parent 114ce87a41
commit 20c716d78d
2 changed files with 17 additions and 5 deletions

View file

@ -78,7 +78,7 @@
"size-limit": [
{
"path": "./dist/WebComponent.js",
"limit": "1.1 KB"
"limit": "1.2 KB"
},
{
"path": "./dist/html.js",

View file

@ -28,6 +28,7 @@ export class WebComponent extends HTMLElement {
*/
static props
// TODO: support array of styles
static styles
/**
@ -203,9 +204,20 @@ export class WebComponent extends HTMLElement {
}
#applyStyles() {
const styleObj = new CSSStyleSheet()
styleObj.replaceSync(this.constructor.styles)
console.log(this.constructor.styles, this.constructor.props)
this.#host.adoptedStyleSheets = [...this.#host.adoptedStyleSheets, styleObj]
if (this.constructor.styles !== undefined)
try {
const styleObj = new CSSStyleSheet()
styleObj.replaceSync(this.constructor.styles)
console.log(this.constructor.styles, this.constructor.props)
this.#host.adoptedStyleSheets = [
...this.#host.adoptedStyleSheets,
styleObj,
]
} catch (e) {
console.error(
'ERR: Constructable stylesheets are only supported in shadow roots',
e
)
}
}
}