It might be nice if we could use ES2015 classes for defining Custom Elements with document.registerElement
, as an alternative to having to define the prototype of the new element explicitly. For example, this is the current way:
const div = document.registerElement('something-awesome', {
prototype: Object.assign(Object.create(HTMLElement.prototype), {
createdCallback() {
console.log('created')
},
attachedCallback() {
console.log('attached')
},
detachedCallback() {
console.log('detahced')
},
attributeChangedCallback() {
console.log('attr changed')
},
})
})
And this would be the new way:
class SomethingAwesome extends HTMLElement {
createdCallback() {
console.log('created')
}
attachedCallback() {
console.log('attached')
}
detachedCallback() {
console.log('detahced')
}
attributeChangedCallback() {
console.log('attr changed')
}
}
const div = document.registerElement('something-awesome', SomethingAwesome)
Perhaps the name of the element can be inferred from the class name if the class is the first arg as a bonus:
const div = document.registerElement(SomethingAwesome)