Loading...
class MyCounter extends HTMLElement {
onResize = () => this.update();
connectedCallback() {
window.addEventListener('resize', this.onResize);
}
}
customElements.define('mycounter', MyCounter);class MyCounter extends HTMLElement {
onResize = () => this.update();
connectedCallback() {
window.addEventListener('resize', this.onResize);
}
disconnectedCallback() {
window.removeEventListener('resize', this.onResize);
}
}
customElements.define('my-counter', MyCounter);Custom element names need a hyphen, and listeners added in connectedCallback are removed when the element leaves the page.
customElements.define() is not called twice for one name.connectedCallback are removed in disconnectedCallback.observedAttributes and handled in attributeChangedCallback.Yes. Custom elements are regular HTML elements, so most frameworks render them, though passing objects and listening to custom events may need extra code.
No. Custom elements work without it, and shadow DOM adds style and DOM encapsulation when you need it.
Use CSS custom properties, which inherit into the shadow root, or ::part() for elements the component exposes with a part attribute.