Add methods param to component generator

This commit is contained in:
Talia 2024-01-24 10:14:43 +01:00
parent 2f95afbcb7
commit 74e364e714
1 changed files with 10 additions and 3 deletions

View File

@ -284,8 +284,12 @@ const attributeObserver = new MutationObserver(mutations => {
} }
}) })
export const component = (generator, name) => { export const component = (name, generator, methods) => {
name = name ?? camelToKebab(generator.name) if (typeof name === "function") {
methods = generator
generator = name
name = camelToKebab(generator.name)
}
const Element = class extends HTMLElement{ const Element = class extends HTMLElement{
constructor() { constructor() {
super() super()
@ -298,9 +302,12 @@ export const component = (generator, name) => {
} }
}) })
attributeObserver.observe(this, {attributes: true}) attributeObserver.observe(this, {attributes: true})
this.replaceChildren(generator(this)) this.replaceChildren(generator.call(this, this.state))
} }
} }
if (methods) {
Object.defineProperties(Element.prototype, Object.getOwnPropertyDescriptors(methods))
}
customElements.define(name, Element) customElements.define(name, Element)
return Element; return Element;
} }