Overview
classes(input) is an element directive that owns a set of class tokens and diffs them on each update — only tokens that changed are added or removed.
Input can be a string, a Record<string, any> (truthy values apply the key), or an array of strings, falsy skips, and tuples [cond, class] / [cond, a, b].
classes vs classMap vs class=
|
API
|
Position
|
Notes
|
|---|---|---|
class=${…}
| Attribute binding | Full class string each render — you manage the whole value. |
classMap({…})
| class= binding |
lit-html directive. Use as class=${classMap(...)}. See /docs/lit-html/directives.
|
${classes(...)}
| Element directive | Mates directive inside the opening tag. Diffs owned tokens; supports array tuples. |
Dynamic class bindings
The classes directive is applied directly on an element attribute slot. Pass an object — keys are class names, values are booleans — or a mixed array of strings and conditionals. Only the diff is applied to the DOM.
Array + tuple form
Mix strings, falsy skips, and [cond, a, b] ternaries in one classes() call.