Designing Custom Elements
In this pack: sizing and rule strictness are set by the core
plugin's oo-design skill; each rule here keeps a core rule in this
language.
Overview
The browser gives a custom element a lifecycle it runs itself: the
constructor, attributeChangedCallback, connectedCallback and a
shadow root that holds the element's markup. That lifecycle moves where
several of the pack's rules are kept. This skill holds only what the
platform changes on top of oo-javascript and oo-typescript; load the
one that matches the project's language too, because its rules for
#private state, frozen values and classes still hold. Each change
brings a shortcut that looks harmless: a constructor that reads the
attributes the browser has not given it yet, an attribute and a property
that drift apart, an element that calls the page's save function or a
store, state left on the element for any script to change, and one
element extended from another to reuse its code. The rules below close
those shortcuts with a two-step construction at the edge, one public
protocol over one private state, events instead of calls, a shadow root
that holds the markup, and composition between elements.
When to use
- A class extends
HTMLElement and its constructor calls
getAttribute, hasAttribute or setAttribute, or writes its own
children.
- A custom element exposes an attribute and a property for the same
value, and the two answer differently.
- A custom element calls a function of the page, a store or a use case,
or a page listener reads a field of the element.
- An element's state or markup is reachable from the page, or the page
needs to restyle one part of it.
- One custom element extends another to reuse its code.
- The page must hear about a pick inside a custom element.
When not to use
- The application is Angular:
oo-typescript-angular covers it.
- The code is JavaScript or TypeScript with no custom element:
oo-javascript or oo-typescript covers it.
- No custom element feature bears on the question, such as which object
owns a rule: the core skills, starting from
oo-design.
- The code is a different framework over JavaScript or TypeScript:
outside this skill.
Core rules
Rule. Treat a custom element as a class at the edge that the
browser builds in two steps: the constructor calls super() first,
attaches the shadow root and sets private fields, and neither reads
nor adds attributes or children; the clinic value the element holds
is built complete from its attributes in attributeChangedCallback,
for the names observedAttributes lists, and the element renders or
sends nothing that needs that value before it exists; work that the
constructor must not do, and work that needs the element in the page,
goes in connectedCallback, guarded because it can run more than
once.
The platform requires of the constructor: super() comes first, no
return other than return or return this, no document.write or
document.open, and no inspection or addition of the element's
attributes and children. A parsed element has its attributes when it
upgrades, and attributeChangedCallback receives each observed one,
so that hook is where text becomes the value the clinic's rules
describe; the hook also runs when an observed attribute is added,
changed, removed or replaced. A connectedCallback runs each time
the element joins the page, and a move runs disconnectedCallback
and then connectedCallback, so one-time work needs a private flag.
This keeps oo-constructing-valid-objects rules 1 and 2 and
oo-working-within-a-framework rules 1 and 2.
Default. Make an observed attribute and its property one piece of
public protocol: the property's setter checks the value and reflects
it to the attribute, a change of the attribute sets the same private
state, and no other state is reachable from outside.
The attribute is the route that markup and the page's styles read,
and the property is the route that script writes. A setter that keeps
the value in a field and never reflects it leaves the attribute
showing an older value; an attribute the class does not list in
observedAttributes never reaches the callback, and a callback that
drops the new value leaves the field older than the attribute. Both
routes run the same check through the value class and end on the one
#private field: a checked value reaches both, an invalid property
write throws before the attribute changes, and invalid markup leaves
the element with no value.
This keeps oo-hiding-implementation rules 1 and 3 and
oo-encapsulating-state-and-behaviour rule 1.
The exception is an attribute that only the page's markup writes,
such as a name fixed for the page: the element observes it and needs
no property, because a property would add a second route onto a value
that has one writer.
Rule. Tell the page what happened with a CustomEvent dispatched
from the element, the pick in its detail, and bubbles and
composed set when a listener outside the shadow root must hear it;
the element never calls the page's functions, a store or a use case.
An element that calls the page's save function holds the page's job
and cannot be used by another screen; a listener on the page holds
the job instead, and the element states what happened in the clinic's
words. bubbles carries an event dispatched on the host up to the
page, where a listener hears it; composed lets the event cross a
shadow boundary, and the event meets one only when the element sits
inside another element's shadow root. Both default to false, so a
page listener hears nothing without bubbles. detail defaults to
null, so the event carries the value itself.
This keeps oo-separating-presentation-from-domain rules 1 and 4 and
oo-inverting-dependencies rule 2.
Rule. Keep the element's state in #private fields and its markup
in its shadow root, and open a part to the page's styles with part
only where the page may restyle it.
A public field on an element is reachable from any script on the
page, so a write to it skips the method that keeps the rule;
oo-javascript rule 3 and oo-typescript rule 2 give the runtime's
reason for a #private field. The shadow root keeps the markup out
of the page's queries and the element's styles out of the page's
rules; attachShadow({ mode: 'closed' }) leaves shadowRoot null,
so an element that wants the root it built keeps it in a field. A
part attribute is the one hook a page's ::part() rule can reach,
so add it to the elements the page may restyle and no others.
This keeps oo-hiding-implementation rules 1 and 3.
Rule. Extend HTMLElement because the platform asks it, and never
extend one custom element from another to reuse its code; share
behaviour between elements by holding an object that has it.
Every custom element class answers the browser's lifecycle calls, and
HTMLElement is the base the platform names for an autonomous
element, so that extends is the platform's demand. An element that
extends another inherits its lifecycle calls and its whole public
surface, so a change to the first reaches both and a caller can treat
one as the other; an override that works because a parent's lifecycle
method makes an internal send breaks when the parent changes that
send. An object held in a #private field offers only the methods
the element calls, and two elements can hold the same class without
sharing a base.
This keeps oo-choosing-composition-over-inheritance rules 2 and 3.
Pattern
PainScale is the clinic value the element holds: the maximum of a pain
scale, 1 to 10, built once and never changed. PainScaleElement is the
custom element a patient uses to rate their pain before a visit; it
shows the scale and sends the patient's pick.
Before:
PainScaleElement is the element in the Before. It reads maximum with
getAttribute in its constructor, writes role there, and sends a
click to a function of the page.
declare function savePainScore(score: number): void
export class PainScaleElement extends HTMLElement {
#maximum: number
#root: ShadowRoot
constructor() {
super()
this.setAttribute('role', 'group')
this.#maximum = Number(this.getAttribute('maximum'))
this.#root = this.attachShadow({ mode: 'open' })
this.#root.innerHTML =
'<button type="button">Rate your pain</button>'
this.#root
.querySelector('button')
?.addEventListener('click', () => {
savePainScore(this.#maximum)
})
}
}
The Before breaks rule 1 twice: the constructor reads the maximum, which
a document.createElement('pain-scale') call has not set, and it writes
role, which the platform forbids. The browser refuses that
constructor: the call reports NotSupportedError through the page's
error event and answers an element that is not a PainScaleElement;
nothing is thrown to the caller. The click handler breaks rule 3,
because the element holds the page's job. The fence stores nothing and
leaves persistence out.
After:
PainScale is the value class: its constructor refuses a maximum
outside 1 to 10, and Object.freeze and the missing writer keep it.
PainScaleElement builds that value when the attribute arrives,
renders inside its shadow root, and dispatches the pick.
export class PainScale {
#maximum: number
constructor(maximum: number) {
if (!PainScale.accepts(maximum)) {
throw new RangeError('a pain scale runs from 1 to 10')
}
this.#maximum = maximum
Object.freeze(this)
}
static accepts(maximum: number): boolean {
return Number.isInteger(maximum) && maximum >= 1 && maximum <= 10
}
static fromText(value: string | null): PainScale | null {
if (value === null) return null
const maximum = Number(value)
return PainScale.accepts(maximum)
? new PainScale(maximum)
: null
}
get maximum(): number {
return this.#maximum
}
}
export class PainScaleElement extends HTMLElement {
static observedAttributes = ['maximum']
#scale: PainScale | null
#root: ShadowRoot
#shown: boolean
constructor() {
super()
this.#scale = null
this.#shown = false
this.#root = this.attachShadow({ mode: 'open' })
}
connectedCallback(): void {
if (this.#shown) return
this.#shown = true
this.setAttribute('role', 'group')
this.#render()
}
attributeChangedCallback(
_name: string,
_oldValue: string | null,
newValue: string | null
): void {
this.#scale = PainScale.fromText(newValue)
if (this.#shown) this.#render()
}
get maximum(): number | null {
return this.#scale === null ? null : this.#scale.maximum
}
set maximum(value: number | null) {
if (value === null) {
this.removeAttribute('maximum')
return
}
const scale = new PainScale(value)
this.setAttribute('maximum', String(scale.maximum))
}
#render(): void {
const scale = this.#scale
if (scale === null) {
this.#root.replaceChildren()
return
}
const choices = document.createElement('div')
choices.setAttribute('part', 'choices')
for (let score = 0; score <= scale.maximum; score += 1) {
const button = document.createElement('button')
button.type = 'button'
button.textContent = String(score)
button.addEventListener('click', () => this.#pick(score))
choices.append(button)
}
this.#root.replaceChildren(choices)
}
#pick(score: number): void {
const scale = this.#scale
if (scale === null || score > scale.maximum) return
this.dispatchEvent(new CustomEvent('pain-score', {
detail: { score },
bubbles: true,
composed: true
}))
}
}
customElements.define('pain-scale', PainScaleElement)
The After keeps rule 1: the constructor calls super() first, attaches
the shadow root and sets its fields, and reads or writes no attribute or
child; attributeChangedCallback builds PainScale when maximum
arrives, and nothing renders or sends before that value exists.
connectedCallback sets role and renders the first time, and #shown
stops that work when a move brings the element back. The After keeps
rule 3 too: #pick dispatches pain-score with the score in detail,
bubbles carries it to a listener on the page, and composed lets it
cross a shadow boundary if the element sits inside another element's
shadow root, so the pick is heard wherever the element is placed; the
element calls nothing of the page's. It keeps rule 4: #scale
and #root are #private, the buttons live in the shadow root, and
only the choices carry part. It keeps rule 5 because the element
holds a PainScale in #scale rather than extending a class that has
one. The name pain-scale starts with a lowercase alphabetic ASCII
character and holds a hyphen, as a valid custom element name must. The
element stores the scale in #scale, on the element in memory, under no
identity and in no transaction; the fence leaves persistence out.
Red flags
Places to look, not verdicts. A red flag becomes a finding only with
evidence: a measurement, a reproducible bug, or a failing test.
- A constructor that calls
getAttribute, hasAttribute or
setAttribute, or sets innerHTML.
- A property whose setter never writes its attribute, an attribute
missing from
observedAttributes, or an attributeChangedCallback
that drops the new value.
- An element that imports a use case, calls
fetch, or calls a global
save function.
- A public field, or a getter that hands out the shadow root.
- A
CustomEvent without bubbles where a page listener must hear it,
or without composed where the element sits in another shadow root.
- A class that extends another custom element.
- A
part on every node in the shadow root, or on none where the page
must restyle one.
- One-time work in
connectedCallback with no flag that stops a second
run.
Common mistakes
- Reading attributes in the constructor because they are there in the
parsed markup.
- Writing an attribute in the constructor to make the element look
right before it joins the page.
- Keeping the attribute and the property in two fields and syncing them
by hand in several places.
- Calling the page's save function from the element and calling the
element reusable.
- Forgetting
bubbles, so a page listener never hears the event, or
composed, so the event never leaves the shadow root of another
element that holds this one.
- Extending a working element to reuse one method, then inheriting its
whole public surface.
- Believing
private in TypeScript keeps an element's field out of the
page's reach.
- Letting a getter answer the shadow root or an internal list.
Related skills
oo-design sizes a task and defines the rule labels.
oo-javascript keeps the pack's rules in plain JavaScript.
oo-typescript keeps the same rules where the compiler checks them.
oo-typescript-angular covers Angular: service lifetimes, shared
state in a service, and injection tokens.
oo-constructing-valid-objects says when an object is complete.
oo-working-within-a-framework decides which platform demands the
edge may meet.
oo-hiding-implementation keeps a module's decisions behind its
interface.
oo-encapsulating-state-and-behaviour keeps a rule with the state it
guards.
oo-separating-presentation-from-domain keeps the clinic's rules out
of the view.
oo-inverting-dependencies says how collaborators arrive and where
the graph is built.
oo-choosing-composition-over-inheritance chooses holding an object
over extending a class.
Further reading
- Attributes and properties
1---2name: oo-web-components3description: Use when a custom element that extends HTMLElement reads attributes in its constructor, exposes attributes and properties, or dispatches custom events.4license: MIT5metadata:6 source: https://skillmd.com/skills/yutna/oo-web-components7 version: 0.21.08 pack: object-oriented-design-skills9---1011# Designing Custom Elements1213**In this pack:** sizing and rule strictness are set by the core14plugin's `oo-design` skill; each rule here keeps a core rule in this15language.1617## Overview1819The browser gives a custom element a lifecycle it runs itself: the20constructor, `attributeChangedCallback`, `connectedCallback` and a21shadow root that holds the element's markup. That lifecycle moves where22several of the pack's rules are kept. This skill holds only what the23platform changes on top of `oo-javascript` and `oo-typescript`; load the24one that matches the project's language too, because its rules for25`#private` state, frozen values and classes still hold. Each change26brings a shortcut that looks harmless: a constructor that reads the27attributes the browser has not given it yet, an attribute and a property28that drift apart, an element that calls the page's save function or a29store, state left on the element for any script to change, and one30element extended from another to reuse its code. The rules below close31those shortcuts with a two-step construction at the edge, one public32protocol over one private state, events instead of calls, a shadow root33that holds the markup, and composition between elements.3435## When to use3637- A class extends `HTMLElement` and its constructor calls38 `getAttribute`, `hasAttribute` or `setAttribute`, or writes its own39 children.40- A custom element exposes an attribute and a property for the same41 value, and the two answer differently.42- A custom element calls a function of the page, a store or a use case,43 or a page listener reads a field of the element.44- An element's state or markup is reachable from the page, or the page45 needs to restyle one part of it.46- One custom element extends another to reuse its code.47- The page must hear about a pick inside a custom element.4849## When not to use5051- The application is Angular: `oo-typescript-angular` covers it.52- The code is JavaScript or TypeScript with no custom element:53 `oo-javascript` or `oo-typescript` covers it.54- No custom element feature bears on the question, such as which object55 owns a rule: the core skills, starting from `oo-design`.56- The code is a different framework over JavaScript or TypeScript:57 outside this skill.5859## Core rules60611. Rule. **Treat a custom element as a class at the edge that the62 browser builds in two steps: the constructor calls `super()` first,63 attaches the shadow root and sets private fields, and neither reads64 nor adds attributes or children; the clinic value the element holds65 is built complete from its attributes in `attributeChangedCallback`,66 for the names `observedAttributes` lists, and the element renders or67 sends nothing that needs that value before it exists; work that the68 constructor must not do, and work that needs the element in the page,69 goes in `connectedCallback`, guarded because it can run more than70 once.**71 The platform requires of the constructor: `super()` comes first, no72 `return` other than `return` or `return this`, no `document.write` or73 `document.open`, and no inspection or addition of the element's74 attributes and children. A parsed element has its attributes when it75 upgrades, and `attributeChangedCallback` receives each observed one,76 so that hook is where text becomes the value the clinic's rules77 describe; the hook also runs when an observed attribute is added,78 changed, removed or replaced. A `connectedCallback` runs each time79 the element joins the page, and a move runs `disconnectedCallback`80 and then `connectedCallback`, so one-time work needs a private flag.81 This keeps `oo-constructing-valid-objects` rules 1 and 2 and82 `oo-working-within-a-framework` rules 1 and 2.83842. Default. **Make an observed attribute and its property one piece of85 public protocol: the property's setter checks the value and reflects86 it to the attribute, a change of the attribute sets the same private87 state, and no other state is reachable from outside.**88 The attribute is the route that markup and the page's styles read,89 and the property is the route that script writes. A setter that keeps90 the value in a field and never reflects it leaves the attribute91 showing an older value; an attribute the class does not list in92 `observedAttributes` never reaches the callback, and a callback that93 drops the new value leaves the field older than the attribute. Both94 routes run the same check through the value class and end on the one95 `#private` field: a checked value reaches both, an invalid property96 write throws before the attribute changes, and invalid markup leaves97 the element with no value.98 This keeps `oo-hiding-implementation` rules 1 and 3 and99 `oo-encapsulating-state-and-behaviour` rule 1.100101 The exception is an attribute that only the page's markup writes,102 such as a name fixed for the page: the element observes it and needs103 no property, because a property would add a second route onto a value104 that has one writer.1051063. Rule. **Tell the page what happened with a `CustomEvent` dispatched107 from the element, the pick in its `detail`, and `bubbles` and108 `composed` set when a listener outside the shadow root must hear it;109 the element never calls the page's functions, a store or a use case.**110 An element that calls the page's save function holds the page's job111 and cannot be used by another screen; a listener on the page holds112 the job instead, and the element states what happened in the clinic's113 words. `bubbles` carries an event dispatched on the host up to the114 page, where a listener hears it; `composed` lets the event cross a115 shadow boundary, and the event meets one only when the element sits116 inside another element's shadow root. Both default to `false`, so a117 page listener hears nothing without `bubbles`. `detail` defaults to118 `null`, so the event carries the value itself.119 This keeps `oo-separating-presentation-from-domain` rules 1 and 4 and120 `oo-inverting-dependencies` rule 2.1211224. Rule. **Keep the element's state in `#private` fields and its markup123 in its shadow root, and open a part to the page's styles with `part`124 only where the page may restyle it.**125 A public field on an element is reachable from any script on the126 page, so a write to it skips the method that keeps the rule;127 `oo-javascript` rule 3 and `oo-typescript` rule 2 give the runtime's128 reason for a `#private` field. The shadow root keeps the markup out129 of the page's queries and the element's styles out of the page's130 rules; `attachShadow({ mode: 'closed' })` leaves `shadowRoot` null,131 so an element that wants the root it built keeps it in a field. A132 `part` attribute is the one hook a page's `::part()` rule can reach,133 so add it to the elements the page may restyle and no others.134 This keeps `oo-hiding-implementation` rules 1 and 3.1351365. Rule. **Extend `HTMLElement` because the platform asks it, and never137 extend one custom element from another to reuse its code; share138 behaviour between elements by holding an object that has it.**139 Every custom element class answers the browser's lifecycle calls, and140 `HTMLElement` is the base the platform names for an autonomous141 element, so that `extends` is the platform's demand. An element that142 extends another inherits its lifecycle calls and its whole public143 surface, so a change to the first reaches both and a caller can treat144 one as the other; an override that works because a parent's lifecycle145 method makes an internal send breaks when the parent changes that146 send. An object held in a `#private` field offers only the methods147 the element calls, and two elements can hold the same class without148 sharing a base.149 This keeps `oo-choosing-composition-over-inheritance` rules 2 and 3.150151## Pattern152153`PainScale` is the clinic value the element holds: the maximum of a pain154scale, 1 to 10, built once and never changed. `PainScaleElement` is the155custom element a patient uses to rate their pain before a visit; it156shows the scale and sends the patient's pick.157158Before:159160`PainScaleElement` is the element in the Before. It reads `maximum` with161`getAttribute` in its constructor, writes `role` there, and sends a162click to a function of the page.163164```typescript165declare function savePainScore(score: number): void166167export class PainScaleElement extends HTMLElement {168 #maximum: number169 #root: ShadowRoot170171 constructor() {172 super()173 this.setAttribute('role', 'group')174 this.#maximum = Number(this.getAttribute('maximum'))175 this.#root = this.attachShadow({ mode: 'open' })176 this.#root.innerHTML =177 '<button type="button">Rate your pain</button>'178 this.#root179 .querySelector('button')180 ?.addEventListener('click', () => {181 savePainScore(this.#maximum)182 })183 }184}185```186187The Before breaks rule 1 twice: the constructor reads the maximum, which188a `document.createElement('pain-scale')` call has not set, and it writes189`role`, which the platform forbids. The browser refuses that190constructor: the call reports `NotSupportedError` through the page's191error event and answers an element that is not a `PainScaleElement`;192nothing is thrown to the caller. The click handler breaks rule 3,193because the element holds the page's job. The fence stores nothing and194leaves persistence out.195196After:197198`PainScale` is the value class: its constructor refuses a maximum199outside 1 to 10, and `Object.freeze` and the missing writer keep it.200`PainScaleElement` builds that value when the attribute arrives,201renders inside its shadow root, and dispatches the pick.202203```typescript204export class PainScale {205 #maximum: number206207 constructor(maximum: number) {208 if (!PainScale.accepts(maximum)) {209 throw new RangeError('a pain scale runs from 1 to 10')210 }211 this.#maximum = maximum212 Object.freeze(this)213 }214215 static accepts(maximum: number): boolean {216 return Number.isInteger(maximum) && maximum >= 1 && maximum <= 10217 }218219 static fromText(value: string | null): PainScale | null {220 if (value === null) return null221 const maximum = Number(value)222 return PainScale.accepts(maximum)223 ? new PainScale(maximum)224 : null225 }226227 get maximum(): number {228 return this.#maximum229 }230}231232export class PainScaleElement extends HTMLElement {233 static observedAttributes = ['maximum']234235 #scale: PainScale | null236 #root: ShadowRoot237 #shown: boolean238239 constructor() {240 super()241 this.#scale = null242 this.#shown = false243 this.#root = this.attachShadow({ mode: 'open' })244 }245246 connectedCallback(): void {247 if (this.#shown) return248 this.#shown = true249 this.setAttribute('role', 'group')250 this.#render()251 }252253 attributeChangedCallback(254 _name: string,255 _oldValue: string | null,256 newValue: string | null257 ): void {258 this.#scale = PainScale.fromText(newValue)259 if (this.#shown) this.#render()260 }261262 get maximum(): number | null {263 return this.#scale === null ? null : this.#scale.maximum264 }265266 set maximum(value: number | null) {267 if (value === null) {268 this.removeAttribute('maximum')269 return270 }271 const scale = new PainScale(value)272 this.setAttribute('maximum', String(scale.maximum))273 }274275 #render(): void {276 const scale = this.#scale277 if (scale === null) {278 this.#root.replaceChildren()279 return280 }281 const choices = document.createElement('div')282 choices.setAttribute('part', 'choices')283 for (let score = 0; score <= scale.maximum; score += 1) {284 const button = document.createElement('button')285 button.type = 'button'286 button.textContent = String(score)287 button.addEventListener('click', () => this.#pick(score))288 choices.append(button)289 }290 this.#root.replaceChildren(choices)291 }292293 #pick(score: number): void {294 const scale = this.#scale295 if (scale === null || score > scale.maximum) return296 this.dispatchEvent(new CustomEvent('pain-score', {297 detail: { score },298 bubbles: true,299 composed: true300 }))301 }302}303304customElements.define('pain-scale', PainScaleElement)305```306307The After keeps rule 1: the constructor calls `super()` first, attaches308the shadow root and sets its fields, and reads or writes no attribute or309child; `attributeChangedCallback` builds `PainScale` when `maximum`310arrives, and nothing renders or sends before that value exists.311`connectedCallback` sets `role` and renders the first time, and `#shown`312stops that work when a move brings the element back. The After keeps313rule 3 too: `#pick` dispatches `pain-score` with the score in `detail`,314`bubbles` carries it to a listener on the page, and `composed` lets it315cross a shadow boundary if the element sits inside another element's316shadow root, so the pick is heard wherever the element is placed; the317element calls nothing of the page's. It keeps rule 4: `#scale`318and `#root` are `#private`, the buttons live in the shadow root, and319only the choices carry `part`. It keeps rule 5 because the element320holds a `PainScale` in `#scale` rather than extending a class that has321one. The name `pain-scale` starts with a lowercase alphabetic ASCII322character and holds a hyphen, as a valid custom element name must. The323element stores the scale in `#scale`, on the element in memory, under no324identity and in no transaction; the fence leaves persistence out.325326## Red flags327328Places to look, not verdicts. A red flag becomes a finding only with329evidence: a measurement, a reproducible bug, or a failing test.330331- A constructor that calls `getAttribute`, `hasAttribute` or332 `setAttribute`, or sets `innerHTML`.333- A property whose setter never writes its attribute, an attribute334 missing from `observedAttributes`, or an `attributeChangedCallback`335 that drops the new value.336- An element that imports a use case, calls `fetch`, or calls a global337 save function.338- A public field, or a getter that hands out the shadow root.339- A `CustomEvent` without `bubbles` where a page listener must hear it,340 or without `composed` where the element sits in another shadow root.341- A class that extends another custom element.342- A `part` on every node in the shadow root, or on none where the page343 must restyle one.344- One-time work in `connectedCallback` with no flag that stops a second345 run.346347## Common mistakes348349- Reading attributes in the constructor because they are there in the350 parsed markup.351- Writing an attribute in the constructor to make the element look352 right before it joins the page.353- Keeping the attribute and the property in two fields and syncing them354 by hand in several places.355- Calling the page's save function from the element and calling the356 element reusable.357- Forgetting `bubbles`, so a page listener never hears the event, or358 `composed`, so the event never leaves the shadow root of another359 element that holds this one.360- Extending a working element to reuse one method, then inheriting its361 whole public surface.362- Believing `private` in TypeScript keeps an element's field out of the363 page's reach.364- Letting a getter answer the shadow root or an internal list.365366## Related skills367368- `oo-design` sizes a task and defines the rule labels.369- `oo-javascript` keeps the pack's rules in plain JavaScript.370- `oo-typescript` keeps the same rules where the compiler checks them.371- `oo-typescript-angular` covers Angular: service lifetimes, shared372 state in a service, and injection tokens.373- `oo-constructing-valid-objects` says when an object is complete.374- `oo-working-within-a-framework` decides which platform demands the375 edge may meet.376- `oo-hiding-implementation` keeps a module's decisions behind its377 interface.378- `oo-encapsulating-state-and-behaviour` keeps a rule with the state it379 guards.380- `oo-separating-presentation-from-domain` keeps the clinic's rules out381 of the view.382- `oo-inverting-dependencies` says how collaborators arrive and where383 the graph is built.384- `oo-choosing-composition-over-inheritance` chooses holding an object385 over extending a class.386387## Further reading388389- [Attributes and properties](references/attributes-and-properties.md)