web-components-conventions
Step 6 of the pipeline (WORKFLOW.md), for custom elements written with Lit. Status: a base to confront with real
work, no in-house project behind it yet (same status as go-conventions). Pinned to Lit 3 (documentation read
2026-10-02; registry latest that day: lit 3.3.3). The raw custom-elements specification beyond what Lit exposes, and
the design-system packaging around components, are not covered.
When
As soon as a file defining a custom element, a Lit template or a reactive property is written or modified, during
code (6) and review (8).
Steps
1. Properties and state
- A public reactive property is an input; internal reactive state is declared as state. Inputs may be set from
markup or by the owner; state is private to the element and never an attribute.
- The
type option of a property drives attribute conversion, not type checking. Typing is the compiler's job.
- Class fields can overwrite the reactive accessor. With plain JavaScript fields, or TypeScript compiling to
define semantics, a declared field shadows the property: initialise in the constructor, use
declare or the
accessor keyword, or turn off define semantics in the compiler options. Decorators need a compiler that
supports them: the decorators page recommends the experimental form for production today, with experimentalDecorators
and useDefineForClassFields: false (required from target ES2022), and says the standard form needs accessor
fields and, on Babel, the 2023-05 decorator version.
- The default change test is strict inequality. Mutating an object or array in place triggers nothing: assign a
new value, or call
requestUpdate(). A custom hasChanged is for a real equality need.
2. Update cycle
render() is pure: properties in, template out; no state change, no side effect, no read of anything else. The
lifecycle page says a property change in render() does not schedule another update and that Lit runs it on the
server; the purity rule itself is the block's own.
- Derived values are computed in
willUpdate(); changing a property up to and including render() does not
schedule a new update. Changing one in updated() does, so a write there needs a stated reason.
- DOM measurement and imperative work go in
updated() or firstUpdated(); reach shadow nodes through the
render root or a query decorator, not through document-wide selectors.
- Updates are asynchronous and batched: several property writes give one render. Await
updateComplete before
reading the DOM or dispatching an event that depends on the new render.
3. Events and listeners
- Dispatch events in response to user interaction or an asynchronous change, not because the owner set a
property. Name events in lowercase (own convention: the events page uses lowercase, hyphenated names without making it a rule);
an event leaves a shadow root only with
composed: true, and reaches every ancestor only when bubbles: true is
set too, so set them deliberately.
- Listeners on
window, document or other outside targets are added in connectedCallback and removed in
disconnectedCallback. Listeners declared in the element's own template (@click) are added when it renders; that they need no removal is the
block's own reading, as the events page does not say it.
4. Shadow DOM and styles
- Children reach a named slot only with a matching
slot attribute; fallback content shows only when nothing is
assigned.
- Style the host with
:host so the defaults stay overridable from outside; expose theming through custom
properties rather than by piercing the shadow root.
unsafeCSS, unsafeHTML and unsafeSVG take trusted input only. Query-string and user-supplied values are
never passed to them: the directives page names cross-site scripting, CSS injection and data exfiltration (it uses
innerHTML), and the styles page shows injected CSS loading an image from a third party. The same page says only
template-literal strings are parsed as HTML; "ordinary expressions are escaped" is an inference from that, not a
statement.
5. Templates and lists
- A child expression renders primitives, templates, nodes and arrays;
'', null, undefined and nothing
render nothing. An attribute that must be absent when a value is missing uses nothing, otherwise the attribute is
rendered with an empty string (the expressions page says null and undefined in an attribute give an empty
string, not a removal), so src goes out empty and the browser requests the page itself; ifDefined(value) is
shorthand for value ?? nothing. A boolean in a child position renders as the text true or false.
- Plain
map is enough for lists; use repeat with a key when updating a DOM node costs more than moving it, or
when nodes hold state no expression controls (a checkbox's checked state): on a reorder map reassigns values in
place, so the labels move and the checked state stays behind (lists page). The key must be unique per item.
6. Server rendering
- Only part of the lifecycle runs on the server: the constructor,
hasChanged, willUpdate and render do;
connectedCallback, disconnectedCallback, attributeChangedCallback, shouldUpdate, update, firstUpdated and
updated do not (SSR authoring page; the lifecycle page agrees for willUpdate, updated and firstUpdated). Browser
APIs belong in client-only callbacks such as updated(), never in the constructor, willUpdate() or render().
The same page says these restrictions may change while Lit SSR is in Lit Labs.
- A module with browser-API side effects at import breaks a server import: move the effect into a client-only
callback or condition it.
- Do not bundle
lit into a published package. The publishing page gives the reason: a bundled copy can put
several versions of Lit into one application, which npm cannot deduplicate, and pre-optimising blocks the
application's own optimisation; the SSR page also warns against inlining it and suggests conditional exports with a
"node" entry. Leave lit a normal dependency (own wording: mark it external in the build).
Output / checkpoint
Elements whose inputs, state, events and slots are listed, with no side effect in render(). No dedicated
checkpoint: gate (7) and review (8).
Guardrails
No comments in the code produced. Never an install: name the package, the person runs pnpm add <package>
(CONVENTIONS.md). Never untrusted input into unsafeHTML or unsafeCSS. Never a side effect in render(). A
rule with an API name is the pinned major's; read package.json before applying it to another.
Mechanical checks
Run these from the project root (grep is installed on any POSIX shell; where absent, search the same patterns with the editor). A hit is a candidate to read in context, not a verdict.
grep -rnE "unsafeHTML|unsafeCSS|unsafeSVG" src
grep -rnE "addEventListener\((['\"])(resize|scroll|keydown|keyup|message)" src
grep -rnE "removeEventListener" src
grep -rnE "document\.(querySelector|getElementById)" src
grep -rnE "@property\(\{ *type: *(Object|Array)" src
grep -rnE "(window|document|navigator|localStorage)\." src
- An outside
addEventListener with no removeEventListener in the same file is rule 3.2.
document.querySelector inside an element is rule 2.3.
Origin
Rewritten from the official Lit documentation (the lit/lit repository, packages/lit-dev-content, v3 docs;
the repository code is BSD-3-Clause), read from a shallow clone on 2026-10-02: components (properties, lifecycle,
events, shadow DOM, styles, rendering), template expressions and lists, and the server-rendering authoring page.
Mechanisms are the library's; wording and grouping are ours; no text copied. Facts that move, with their pin
(Lit 3): decorator and accessor syntax, the lifecycle method set run on the server. Not read, a stated gap:
directives other than those named, reactive controllers, context, tasks, localization, testing, the server-rendering
toolchain, and the raw custom-elements specification. Refresh with skills/source-freshness on the next major.
Written 2026-10-02, never run on real work.
Re-verified 2026-10-20 (🟡, nothing run). Licence read on 2026-10-20: the lit/lit repository is BSD-3-Clause
("The Lit Project Contributors"), which is outside the permitted licences of CONVENTIONS.md; so on this pass the
lit.dev pages were read as official product documentation, for facts only, with nothing copied, and the repository was
not opened for text. Pages read (lit 3.3.3 is the latest release): reactive properties, lifecycle, events, shadow DOM,
styles, template expressions, lists, built-in directives, decorators, SSR authoring, publishing. Confirmed: 1.1 to
1.4, 2.2 to 2.4, 3.2 (window and document listeners), 4.1 to 4.2, 5.1 (what renders nothing). Corrected: the
src="undefined" symptom (an empty string), the list of lifecycle callbacks that run on the server, the repeat
criterion, and the unsafe directives (now three). Marked as the block's own: render() purity, lowercase event
names, no removal for template listeners, escaping by default. Still unverified: the directive set beyond those
named, reactive controllers, context, tasks, localisation, the raw custom-elements specification, and the SSR toolchain
(still Lit Labs).
Independent verification 2026-10-21: 18 checked, 18 matched, 0 corrected, 0 unverified
1---2name: web-components-conventions3description: "Use when writing or reviewing custom elements with Lit 3: public reactive properties versus internal state, the update cycle and where side effects belong, events and listeners with cleanup, shadow DOM slots and styles, pure render methods, template escaping, and what breaks under server rendering."4paths: "**/*.ts, **/*.js"5metadata:6 source: https://skillmd.com/skills/techmefr/web-components-conventions7---89# web-components-conventions1011Step 6 of the pipeline (`WORKFLOW.md`), for custom elements written with Lit. **Status: a base to confront with real12work**, no in-house project behind it yet (same status as `go-conventions`). Pinned to Lit 3 (documentation read132026-10-02; registry latest that day: lit 3.3.3). The raw custom-elements specification beyond what Lit exposes, and14the design-system packaging around components, are not covered.1516## When17As soon as a file defining a custom element, a Lit template or a reactive property is written or modified, during18`code` (6) and `review` (8).1920## Steps2122### 1. Properties and state231. **A public reactive property is an input; internal reactive state is declared as state.** Inputs may be set from24 markup or by the owner; state is private to the element and never an attribute.252. **The `type` option of a property drives attribute conversion, not type checking.** Typing is the compiler's job.263. **Class fields can overwrite the reactive accessor.** With plain JavaScript fields, or TypeScript compiling to27 define semantics, a declared field shadows the property: initialise in the constructor, use `declare` or the28 `accessor` keyword, or turn off define semantics in the compiler options. Decorators need a compiler that29 supports them: the decorators page recommends the experimental form for production today, with `experimentalDecorators`30 and `useDefineForClassFields: false` (required from target ES2022), and says the standard form needs `accessor`31 fields and, on Babel, the `2023-05` decorator version.324. **The default change test is strict inequality.** Mutating an object or array in place triggers nothing: assign a33 new value, or call `requestUpdate()`. A custom `hasChanged` is for a real equality need.3435### 2. Update cycle361. **`render()` is pure**: properties in, template out; no state change, no side effect, no read of anything else. The37 lifecycle page says a property change in `render()` does not schedule another update and that Lit runs it on the38 server; the purity rule itself is the block's own.392. **Derived values are computed in `willUpdate()`**; changing a property up to and including `render()` does not40 schedule a new update. Changing one in `updated()` does, so a write there needs a stated reason.413. **DOM measurement and imperative work go in `updated()` or `firstUpdated()`**; reach shadow nodes through the42 render root or a query decorator, not through document-wide selectors.434. **Updates are asynchronous and batched**: several property writes give one render. Await `updateComplete` before44 reading the DOM or dispatching an event that depends on the new render.4546### 3. Events and listeners471. **Dispatch events in response to user interaction or an asynchronous change, not because the owner set a48 property.** Name events in lowercase (own convention: the events page uses lowercase, hyphenated names without making it a rule);49 an event leaves a shadow root only with `composed: true`, and reaches every ancestor only when `bubbles: true` is50 set too, so set them deliberately.512. **Listeners on `window`, `document` or other outside targets are added in `connectedCallback` and removed in52 `disconnectedCallback`.** Listeners declared in the element's own template (`@click`) are added when it renders; that they need no removal is the53 block's own reading, as the events page does not say it.5455### 4. Shadow DOM and styles561. **Children reach a named slot only with a matching `slot` attribute**; fallback content shows only when nothing is57 assigned.582. **Style the host with `:host`** so the defaults stay overridable from outside; expose theming through custom59 properties rather than by piercing the shadow root.603. **`unsafeCSS`, `unsafeHTML` and `unsafeSVG` take trusted input only.** Query-string and user-supplied values are61 never passed to them: the directives page names cross-site scripting, CSS injection and data exfiltration (it uses62 `innerHTML`), and the styles page shows injected CSS loading an image from a third party. The same page says only63 template-literal strings are parsed as HTML; "ordinary expressions are escaped" is an inference from that, not a64 statement.6566### 5. Templates and lists671. **A child expression renders primitives, templates, nodes and arrays; `''`, `null`, `undefined` and `nothing`68 render nothing.** An attribute that must be absent when a value is missing uses `nothing`, otherwise the attribute is69 rendered with an empty string (the expressions page says `null` and `undefined` in an attribute give an empty70 string, not a removal), so `src` goes out empty and the browser requests the page itself; `ifDefined(value)` is71 shorthand for `value ?? nothing`. A boolean in a child position renders as the text `true` or `false`.722. **Plain `map` is enough for lists**; use `repeat` with a key when updating a DOM node costs more than moving it, or73 when nodes hold state no expression controls (a checkbox's checked state): on a reorder `map` reassigns values in74 place, so the labels move and the checked state stays behind (lists page). The key must be unique per item.7576### 6. Server rendering771. **Only part of the lifecycle runs on the server**: the constructor, `hasChanged`, `willUpdate` and `render` do;78 `connectedCallback`, `disconnectedCallback`, `attributeChangedCallback`, `shouldUpdate`, `update`, `firstUpdated` and79 `updated` do not (SSR authoring page; the lifecycle page agrees for `willUpdate`, `updated` and `firstUpdated`). Browser80 APIs belong in client-only callbacks such as `updated()`, never in the constructor, `willUpdate()` or `render()`.81 The same page says these restrictions may change while Lit SSR is in Lit Labs.822. **A module with browser-API side effects at import breaks a server import**: move the effect into a client-only83 callback or condition it.843. **Do not bundle `lit` into a published package.** The publishing page gives the reason: a bundled copy can put85 several versions of Lit into one application, which npm cannot deduplicate, and pre-optimising blocks the86 application's own optimisation; the SSR page also warns against inlining it and suggests conditional exports with a87 `"node"` entry. Leave `lit` a normal dependency (own wording: mark it external in the build).8889## Output / checkpoint90Elements whose inputs, state, events and slots are listed, with no side effect in `render()`. No dedicated91checkpoint: `gate` (7) and `review` (8).9293## Guardrails94No comments in the code produced. Never an install: name the package, the person runs `pnpm add <package>`95(`CONVENTIONS.md`). Never untrusted input into `unsafeHTML` or `unsafeCSS`. Never a side effect in `render()`. A96rule with an API name is the pinned major's; read `package.json` before applying it to another.9798## Mechanical checks99100Run these from the project root (grep is installed on any POSIX shell; where absent, search the same patterns with the editor). A hit is a candidate to read in context, not a verdict.101102```103grep -rnE "unsafeHTML|unsafeCSS|unsafeSVG" src104grep -rnE "addEventListener\((['\"])(resize|scroll|keydown|keyup|message)" src105grep -rnE "removeEventListener" src106grep -rnE "document\.(querySelector|getElementById)" src107grep -rnE "@property\(\{ *type: *(Object|Array)" src108grep -rnE "(window|document|navigator|localStorage)\." src109```110111- An outside `addEventListener` with no `removeEventListener` in the same file is rule 3.2.112- `document.querySelector` inside an element is rule 2.3.113114## Origin115Rewritten from the official Lit documentation (the `lit/lit` repository, `packages/lit-dev-content`, v3 docs;116the repository code is BSD-3-Clause), read from a shallow clone on 2026-10-02: components (properties, lifecycle,117events, shadow DOM, styles, rendering), template expressions and lists, and the server-rendering authoring page.118Mechanisms are the library's; wording and grouping are ours; no text copied. **Facts that move, with their pin119(Lit 3):** decorator and accessor syntax, the lifecycle method set run on the server. **Not read, a stated gap:**120directives other than those named, reactive controllers, context, tasks, localization, testing, the server-rendering121toolchain, and the raw custom-elements specification. Refresh with `skills/source-freshness` on the next major.122Written 2026-10-02, never run on real work.123124**Re-verified 2026-10-20 (🟡, nothing run).** Licence read on 2026-10-20: the `lit/lit` repository is **BSD-3-Clause**125("The Lit Project Contributors"), which is outside the permitted licences of `CONVENTIONS.md`; so on this pass the126lit.dev pages were read as official product documentation, for facts only, with nothing copied, and the repository was127not opened for text. Pages read (lit 3.3.3 is the latest release): reactive properties, lifecycle, events, shadow DOM,128styles, template expressions, lists, built-in directives, decorators, SSR authoring, publishing. **Confirmed:** 1.1 to1291.4, 2.2 to 2.4, 3.2 (window and document listeners), 4.1 to 4.2, 5.1 (what renders nothing). **Corrected:** the130`src="undefined"` symptom (an empty string), the list of lifecycle callbacks that run on the server, the `repeat`131criterion, and the unsafe directives (now three). **Marked as the block's own:** `render()` purity, lowercase event132names, no removal for template listeners, escaping by default. **Still unverified:** the directive set beyond those133named, reactive controllers, context, tasks, localisation, the raw custom-elements specification, and the SSR toolchain134(still Lit Labs).135136Independent verification 2026-10-21: 18 checked, 18 matched, 0 corrected, 0 unverified