Lesson 2 of 9 · 25m
Attributes, Properties, and Events: Designing the Public Contract
The difference between declarative HTML and imperative JavaScript state, when reflection is correct, and how events cross a component boundary.
The quickest way to spot an immature component is an API that only supports attributes. Attributes are strings and HTML-facing. Properties are typed and JavaScript-facing. Serious components support both, with clear ownership rules.
Attributes are input, properties are state
<quantity-stepper min="0" max="10" value="2"></quantity-stepper>stepper.value = 4;
stepper.disabled = true;
stepper.items = ["small", "medium"];HTML authors can only write strings, so primitive configuration belongs in attributes. Arrays, objects, callbacks, and class instances belong in properties. Do not JSON-encode rich state into attributes just because a host framework serializes props that way; that is a serialization leak, not an API.
Reflection is for visible, primitive state
Reflect when CSS, debugging, or markup consumers benefit from seeing state in HTML. For booleans, the attribute can be the only source of truth:
static observedAttributes = ["disabled"];
get disabled() {
return this.hasAttribute("disabled");
}
set disabled(value) {
this.toggleAttribute("disabled", Boolean(value));
}Do not maintain a separate _disabled field that can disagree with the attribute. If the property owns richer state, write through carefully and guard against attribute/property feedback loops. Reflection is a contract decision, not an automatic habit.
Upgrade timing creates a property hole
const el = document.createElement("user-badge");
el.user = { name: "Ada" }; // before definition or upgrade
customElements.define("user-badge", UserBadge);The assignment happened before the class installed its setter, so it became an own property that shadows the prototype setter. During first connection, move documented own properties back through the setter:
_upgradeProperty(prop) {
if (Object.prototype.hasOwnProperty.call(this, prop)) {
const value = this[prop];
delete this[prop];
this[prop] = value;
}
}This pattern looks ceremonial until a CMS, test harness, or framework creates elements before registration. Then it is the difference between correct data and a silently ignored value.
Events are the output channel
this.dispatchEvent(new CustomEvent("stepper-change", {
detail: { value: this.value },
bubbles: true,
composed: true,
}));bubbles lets ancestors listen naturally; composed lets the event cross shadow boundaries. Emit facts (item-selected) rather than instructions (open-dialog). Use lowercase hyphenated names, and document the shape of detail as carefully as a REST response.
Field notes
- Attribute names are case-insensitive in HTML; use lowercase hyphenated names and map them to camelCase properties.
- Setting a reflected boolean property false must remove the attribute — assigning the string "false" in HTML still means true.
- Property setters must tolerate being called before connection or before shadow children exist.
- Events are not imperative method calls. Parents decide what to do with facts the element reports.
Was this lesson helpful?