CKTIS / JavaScript
Web Components: custom elements and lifecycle
Web Components are web-platform mechanisms for building custom interface elements. Learn how to register a class extending HTMLElement, respond to connection and disconnection, and observe attributes. These are browser APIs, not features supplied by ECMAScript itself.
Version: Web-platform APIs, not tied to an ECMAScript edition. The example's exact console output was verified in isolated Chromium 157.0.8103.51 only; this does not establish broader browser compatibility. Node 24 in the repository does not establish browser API support.
Syntax
class ExampleElement extends HTMLElement {
static get observedAttributes() { return ['label']; }
connectedCallback() { /* initialize or resume */ }
disconnectedCallback() { /* release external resources */ }
attributeChangedCallback(name, oldValue, newValue) { /* react to an observed attribute */ }
}
customElements.define('example-element', ExampleElement);Web Components encompass custom elements, Shadow DOM, and templates and slots. These mechanisms can be combined, but a custom element does not require Shadow DOM. An autonomous custom element is an instance of a class that extends HTMLElement. customElements.define() registers that class's constructor under the element name. A valid name must contain a hyphen, as in cktis-greeting.
In the constructor, call super() before using this; a shadow root can be created there with attachShadow(). connectedCallback() handles connection to the document, while disconnectedCallback() handles disconnection. Connection can happen repeatedly: avoid duplicating listeners or timers, and release external resources on disconnection. Shadow DOM provides structural and style encapsulation, not a security boundary.
Static observedAttributes specifies the observed attribute names. attributeChangedCallback(name, oldValue, newValue) reacts to their addition, change, or removal; it can run before the element is connected. A missing value is represented by null. Changing an ordinary JavaScript property does not automatically change an attribute. The example uses textContent to display text without interpreting it as HTML.
The listed sources focus on custom elements and HTMLElement: the WHATWG HTML Living Standard specifies custom elements, and the MDN references document HTMLElement and explain how to use custom elements. The example’s exact console output was verified only in an isolated instance of Chromium 157.0.8103.51. That does not establish compatibility with other browsers or versions; check API support in your target browsers. No single ECMAScript edition guarantees Web Components; Node 24 in the repository does not establish API availability either. The example requires a browser environment.
Examples
(() => {
class CktisGreeting extends HTMLElement {
static get observedAttributes() {
return ['label'];
}
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
connectedCallback() {
this.render();
}
disconnectedCallback() {
console.log('disconnected');
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue !== newValue) this.render();
}
render() {
this.shadowRoot.textContent = `Hello, ${this.getAttribute('label') ?? 'world'}!`;
}
}
if (customElements.get('cktis-greeting')) {
throw new Error('Reload the page before running this example again.');
}
customElements.define('cktis-greeting', CktisGreeting);
const element = document.createElement('cktis-greeting');
document.body.append(element);
console.log(element.shadowRoot.textContent);
element.setAttribute('label', 'Ada');
console.log(element.shadowRoot.textContent);
element.remove();
})();Result
Hello, world! Hello, Ada! disconnected
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.