Add methods param to component generator
This commit is contained in:
parent
2f95afbcb7
commit
74e364e714
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) => {
|
||||
name = name ?? camelToKebab(generator.name)
|
||||
export const component = (name, generator, methods) => {
|
||||
if (typeof name === "function") {
|
||||
methods = generator
|
||||
generator = name
|
||||
name = camelToKebab(generator.name)
|
||||
}
|
||||
const Element = class extends HTMLElement{
|
||||
constructor() {
|
||||
super()
|
||||
|
@ -298,9 +302,12 @@ export const component = (generator, name) => {
|
|||
}
|
||||
})
|
||||
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)
|
||||
return Element;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue