classMap directive efficiently adds and removes CSS classes on an element by mapping property names in a ClassInfo object to truthy or falsy values. Only properties with truthy values are added as classes; falsy values cause the class to be removed.
Import
Signature
Types
Parameters
An object whose keys are CSS class names and whose values determine whether each class is applied. A truthy value adds the class; a falsy value (including
0, '', false) removes it.Return type
DirectiveResult — an opaque value consumed by lit-html’s template engine.
Constraints
Example
baseis a static class and is never touched byclassMap.has-iconis always present because its value istrue.activeanddisabledare toggled based on component state.
When to use
- You need to toggle multiple classes based on component state or properties.
- You want a clean, declarative alternative to string concatenation or ternary expressions in a
classattribute.
When not to use
- When you only need a single conditional class — a simple ternary expression is more readable:
class="${this.active ? 'active' : ''}". - Outside of a
classattribute —classMapwill throw if placed elsewhere.