Lab 03: Web Components Architecture
Overview
Step 1: Custom Element Lifecycle
class DsButton extends HTMLElement {
// Observed attributes trigger attributeChangedCallback
static get observedAttributes() {
return ['variant', 'size', 'disabled', 'loading'];
}
constructor() {
super();
// Always call super() first
this._shadow = this.attachShadow({ mode: 'open' });
this._state = { loading: false };
}
// Lifecycle: element added to DOM
connectedCallback() {
this._render();
this._setupEventListeners();
}
// Lifecycle: element removed from DOM
disconnectedCallback() {
this._cleanup();
}
// Lifecycle: element moved to new document
adoptedCallback() {
console.log('Element adopted into new document');
}
// Lifecycle: observed attribute changed
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue === newValue) return;
if (name === 'loading') this._state.loading = newValue !== null;
this._render();
}
}Step 2: Shadow DOM — Slots and Parts
Step 3: Element Registry with Collision Detection
Step 4: Component Composition Patterns
Step 5: CSS Custom Properties API Surface
Step 6: Reactive State Pattern
Step 7: Event Communication Pattern
Step 8: Capstone — Lifecycle Verification
Summary
Concept
API
Notes
Last updated
