Overview
A directive is a special value you put in an expression. It runs instead of being written as text. Import these from mates next to html — the same names lit-html uses, except cache, which is exported as litCache.
classMap belongs in class=. ifDefined and live belong on an attribute or property. until, litCache, guard, keyed, and unsafeHTML belong in a child expression. repeat is on the Lists page.
What you might have used in JSX
|
Need
|
Helper
|
Notes
|
|---|---|---|
| className object |
classMap
|
class=${classMap({ card: true, on: active })}
|
| Omit an optional attr |
ifDefined
| Removes the attribute when the value is undefined. null still writes “null”. |
| Force an input’s value |
live
| Writes the property even when the previous value looks unchanged. |
| key on one panel |
keyed
| Remounts when the key changes. See Lists. |
| Keep / skip a subtree |
litCache / guard
| litCache keeps DOM across a conditional. guard skips work until its values change. |
| Promise placeholder |
until
| Shows the fallback until the promise settles. |
| dangerouslySetInnerHTML |
unsafeHTML
| Trusted markup only. |
| Leave the DOM alone |
noChange
| A sentinel exported from mates beside html. |
classMap and ifDefined
classMap turns truthy keys into classes. ifDefined omits the attribute when the value is undefined.
live() writes the property every update
live(value) forces the DOM property to match, which matters when a reset must overwrite what the user typed.
until() while a promise is pending
until(promise, fallback) shows the fallback, then replaces it with the settled value.