Overview
attr(map) and style(map) are element directives that apply a record of attributes or CSS properties and update only what changed.
Both accept plain values and tuple forms: [condition, value] (apply when truthy) and [condition, a, b] (ternary). style also accepts CSS custom properties like "--token".
attr / style vs native bindings
|
API
|
Use when
|
Notes
|
|---|---|---|
attr=${val} / ?attr
| One attribute | Native lit-html bindings — great for single attrs. |
${attr({…})}
| Many attrs / conditionals | Boolean false removes the attribute (never sets the string 'false'). |
style="…${x}…"
| Simple inline | String rebuild each render. |
${style({…})}
| Dynamic map | Merges properties; clears previously owned keys that become null/undefined. |
attr and style directives
attr sets HTML attributes (including boolean ones like disabled) and style applies inline CSS properties — both update incrementally on every render.
Conditional attr tuple
Use [cond, value] to apply or remove an attribute.