Add methods param to component generator
This commit is contained in:
parent
38b2127920
commit
e75d90073e
1 changed files with 10 additions and 3 deletions
13
state.js
13
state.js
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue