A base class for building custom elements.
This class provides a mechanism for asynchronous updates and lifecycle callbacks.
▶Create a custom element
Create a custom element
// Create a custom class that extends the CustomElement class. class ColorPreviewElement extends CustomElement { static override get observedAttributes(): Array<string> { return ['color', 'size']; } // When a observed attributes changes, the element is re-rendered. override render(): void { const color = this.getAttribute('color') ?? '#000000'; const size = this.getAttribute('size') ?? '100px'; this.style.display = 'inline-block'; this.style.width = size; this.style.height = size; this.style.backgroundColor = color; this.textContent = color; } override updatedCallback(): void { console.log('color-preview-updated'); } } // Define the custom element. ColorPreviewElement.define('color-preview'); // Use the custom element in HTML. // <color-preview color="#ff0000" size="100px"></color-preview> // <color-preview color="#00ff00" size="100px"></color-preview> // <color-preview color="#0000ff" size="100px"></color-preview>
updateCounter: number
Returns the number of times the element has been updated.
adoptedCallback(): void
Callback invoked when the element is moved to a new document.
By default, to do nothing.
attributeChangedCallback(): void
Callback invoked when an observed attribute changes.
By default, the element is updated.
connectedCallback(): void
Callback invoked when the element is connected to a parent node.
By default, the element is updated.
disconnectedCallback(): void
Callback invoked when the element is disconnected from a parent node.
By default, to do nothing.
errorCallback(exception: unknown): void
Callback invoked when an error occurs during the update process.
By default, output an error log.
render(): void
Renders the element's content.
This method should be implemented by subclasses to render the content.
Updates the element asynchronously, scheduling an update for later execution.
updateSync(): void
Updates the element synchronously, calling additional lifecycle callbacks.
updatedCallback(): void
Callback invoked when the element has been updated.
By default, to do nothing.
observedAttributes: Array<string>
Returns an observed attributes.