Directives
A directive that renders the items of an async iterable[1], appending new values after previous values, similar to the built-in support for iterables.
Import
Signature
asyncAppend(value, mapper?): (part: Part) => Promise<void>
Parameters
- value
AsyncIterable<T>
An async iterable
- mapper?
(v: T, index?: number) => unknown
An optional function that maps from (value, index) to another value. Useful for generating templates for each item in the iterable.
Details
Async iterables are objects with a [Symbol.asyncIterator] method, which returns an iterator who's next()
method returns a Promise. When a new value is available, the Promise resolves and the value is appended to the Part controlled by the directive. If another value other than this directive has been set on the Part, the iterable will no longer be listened to and new values won't be written to the Part.
A directive that renders the items of an async iterable[1], replacing previous values with new values, so that only one value is ever rendered at a time.
Import
Signature
asyncReplace(value, mapper?): (part: Part) => Promise<void>
Parameters
- value
AsyncIterable<T>
An async iterable
- mapper?
(v: T, index?: number) => unknown
An optional function that maps from (value, index) to another value. Useful for generating templates for each item in the iterable.
Details
Async iterables are objects with a [Symbol.asyncIterator] method, which returns an iterator who's next()
method returns a Promise. When a new value is available, the Promise resolves and the value is rendered to the Part controlled by the directive. If another value other than this directive has been set on the Part, the iterable will no longer be listened to and new values won't be written to the Part.
Enables fast switching between multiple templates by caching the DOM nodes and TemplateInstances produced by the templates.
Import
Signature
cache(value): (part: Part) => void
Parameters
- value
unknown
Details
Example:
A directive that applies CSS classes. This must be used in the class
attribute and must be the only part used in the attribute. It takes each property in the classInfo
argument and adds the property name to the element's class
if the property value is truthy; if the property value is falsey, the property name is removed from the element's class
. For example {foo: bar}
applies the class foo
if the value of bar
is truthy.
Import
Signature
classMap(classInfo): (part: Part) => void
Parameters
- classInfo
ClassInfo
Import
Prevents re-render of a template function until a single value or an array of values changes.
Import
Signature
guard(value, f): (part: Part) => void
Parameters
- value
unknown
the value to check before re-rendering
- f
() => unknown
the template function
Details
Example:
In this case, the template only renders if either user.id
or company.id
changes.
guard() is useful with immutable data patterns, by preventing expensive work until data updates.
Example:
In this case, items are mapped over only when the array reference changes.
For AttributeParts, sets the attribute if the value is defined and removes the attribute if the value is undefined.
Import
Signature
ifDefined(value): (part: Part) => void
Parameters
- value
unknown
Details
For other part types, this directive is a no-op.
Checks binding values against live DOM values, instead of previously bound values, when determining whether to update the value.
Import
Signature
live(value): (part: AttributePart | PropertyPart | BooleanAttributePart) => void
Parameters
- value
unknown
Details
This is useful for cases where the DOM value may change from outside of lit-html, such as with a binding to an <input>
element's value
property, a content editable elements text, or to a custom element that changes it's own properties or attributes.
In these cases if the DOM value changes, but the value set through lit-html bindings hasn't, lit-html won't know to update the DOM value and will leave it alone. If this is not what you want—if you want to overwrite the DOM value with the bound value no matter what—use the live()
directive:
html<input .value=${live(x)}>
live()
performs a strict equality check agains the live DOM value, and if the new value is equal to the live value, does nothing. This means that live()
should not be used when the binding will cause a type conversion. If you use live()
with an attribute binding, make sure that only strings are passed in, or the binding will update every render.
A directive that repeats a series of values (usually TemplateResults
) generated from an iterable, and updates those items efficiently when the iterable changes based on user-provided keys
associated with each item.
Import
Signature
repeat(items, keyFnOrTemplate, template?): DirectiveFn
Parameters
- items
Iterable<T>
- keyFnOrTemplate
KeyFn<T> | ItemTemplate<T>
- template?
ItemTemplate<T>
Details
Note that if a keyFn
is provided, strict key-to-DOM mapping is maintained, meaning previous DOM for a given key is moved into the new position if needed, and DOM will never be reused with values for different keys (new DOM will always be created for new keys). This is generally the most efficient way to use repeat
since it performs minimum unnecessary work for insertions and removals.
IMPORTANT: If providing a keyFn
, keys must be unique for all items in a given call to repeat
. The behavior when two or more items have the same key is undefined.
If no keyFn
is provided, this directive will perform similar to mapping items to values, and DOM will be reused against potentially different items.
Import
Type
(item: T, index: number) => unknown
Import
Type
(item: T, index: number) => unknown
A directive that applies CSS properties to an element.
Import
Signature
styleMap(styleInfo): (part: Part) => void
Parameters
- styleInfo
StyleInfo
Details
styleMap
can only be used in the style
attribute and must be the only expression in the attribute. It takes the property names in the styleInfo
object and adds the property values as CSS properties. Property names with dashes (-
) are assumed to be valid CSS property names and set on the element's style object using setProperty()
. Names without dashes are assumed to be camelCased JavaScript property names and set on the element's style object using property assignment, allowing the style object to translate JavaScript-style names to CSS property names.
For example styleMap({backgroundColor: 'red', 'border-top': '5px', '--size': '0'})
sets the background-color
, border-top
and --size
properties.
Import
Renders the content of a template element as HTML.
Import
Signature
templateContent(template): (part: Part) => void
Parameters
- template
HTMLTemplateElement
Details
Note, the template should be developer controlled and not user controlled. Rendering a user-controlled template with this directive could lead to cross-site-scripting vulnerabilities.
Renders the result as HTML, rather than text.
Import
Signature
unsafeHTML(value): (part: Part) => void
Parameters
- value
unknown
Details
Note, this is unsafe to use with any user-provided input that hasn't been sanitized or escaped, as it may lead to cross-site-scripting vulnerabilities.
Renders the result as SVG, rather than text.
Import
Signature
unsafeSVG(value): (part: Part) => void
Parameters
- value
unknown
Details
Note, this is unsafe to use with any user-provided input that hasn't been sanitized or escaped, as it may lead to cross-site-scripting vulnerabilities.
Renders one of a series of values, including Promises, to a Part.
Import
Signature
until(args): (part: Part) => void
Parameters
- args
Array<unknown>
Details
Values are rendered in priority order, with the first argument having the highest priority and the last argument having the lowest priority. If a value is a Promise, low-priority values will be rendered until it resolves.
The priority of values can be used to create placeholder content for async data. For example, a Promise with pending content can be the first, highest-priority, argument, and a non_promise loading indicator template can be used as the second, lower-priority, argument. The loading indicator will render immediately, and the primary content will render when the Promise resolves.
Example:
const content = fetch('./content.txt').then(r => r.text()); html${until(content, html
<span>Loading...</span>)}