You're viewing docs for an older version of Lit. Click here for the latest version.

Custom directives

Writes attribute values to the DOM for a group of AttributeParts bound to a single attribute. The value is only set once even if there are multiple parts for an attribute.

Import

Methods and properties

new AttributeCommitter(element, name, strings): AttributeCommitter

Permalink to constructor View source
Parameters
element
MDN Element
name
string
strings
ReadonlyArray<string>

readonly name: string

Permalink to name View source

readonly parts: ReadonlyArray<AttributePart>

Permalink to parts View source

readonly strings: ReadonlyArray<string>

Permalink to strings View source

A Part that controls all or part of an attribute value.

Import

Methods and properties

Parameters
committer
AttributeCommitter

Commits the current part value, causing it to actually be written to the DOM.

Details

Directives are run at the start of commit, so that if they call part.setValue(...) synchronously that value will be used in the current commit, and there's no need to call part.commit() within the directive. If directives set a part value asynchronously, then they must call part.commit() manually.

Sets the current part value, but does not write it to the DOM.

Parameters
value
unknown

Implements a boolean attribute, roughly as defined in the HTML specification.

Import

Details

If the value is truthy, then the attribute is present with a value of ''. If the value is falsey, the attribute is removed.

Methods and properties

new BooleanAttributePart(element, name, strings): BooleanAttributePart

Permalink to constructor View source
Parameters
element
MDN Element
name
string
strings
ReadonlyArray<string>

readonly name: string

Permalink to name View source

readonly strings: ReadonlyArray<string>

Permalink to strings View source

Commits the current part value, causing it to actually be written to the DOM.

Details

Directives are run at the start of commit, so that if they call part.setValue(...) synchronously that value will be used in the current commit, and there's no need to call part.commit() within the directive. If directives set a part value asynchronously, then they must call part.commit() manually.

Sets the current part value, but does not write it to the DOM.

Parameters
value
unknown

Import

Signature

createMarker(): Comment

Import

Type

DefaultTemplateProcessor

Creates Parts when a template is instantiated.

Import

Methods and properties

handleAttributeExpressions(element, name, strings, options): ReadonlyArray<Part>

Permalink to handleAttributeExpressions View source

Create parts for an attribute-position binding, given the event, attribute name, and string literals.

Parameters
element
MDN Element

The element containing the binding

name
string

The attribute name

strings
Array<string>

The string literals. There are always at least two strings, event for fully-controlled bindings with a single expression.

options
RenderOptions

Create parts for a text-position binding.

Parameters
options
RenderOptions

Brands a function as a directive factory function so that lit-html will call the function during template rendering, rather than passing as a value.

Import

Signature

directive(f): F

Parameters

f
F

The directive factory function. Must be a function that returns a function of the signature (part: Part) => void. The returned function will be called with the part object.

Details

A directive is a function that takes a Part as an argument. It has the signature: (part: Part) => void.

A directive factory is a function that takes arguments for data and configuration and returns a directive. Users of directive usually refer to the directive factory as the directive. For example, "The repeat directive".

Usually a template author will invoke a directive factory in their template with relevant arguments, which will then return a directive function.

Here's an example of using the repeat() directive factory that takes an array and a function to render an item:

When repeat is invoked, it returns a directive function that closes over items and the template function. When the outer template is rendered, the return directive function is called with the Part for the expression. repeat then performs it's custom logic to render multiple items.

Import

Methods and properties

new EventPart(element, eventName, eventContext?): EventPart

Permalink to constructor View source
Parameters
element
MDN Element
eventName
string
eventContext?
EventTarget

readonly eventContext?: EventTarget

Permalink to eventContext View source

readonly eventName: string

Permalink to eventName View source

value: undefined | EventHandlerWithOptions

Permalink to value View source

Commits the current part value, causing it to actually be written to the DOM.

Details

Directives are run at the start of commit, so that if they call part.setValue(...) synchronously that value will be used in the current commit, and there's no need to call part.commit() within the directive. If directives set a part value asynchronously, then they must call part.commit() manually.

Parameters
event
Event

Sets the current part value, but does not write it to the DOM.

Parameters
value
undefined | EventHandlerWithOptions

Import

Signature

isDirective(o): o

Parameters

o
unknown

Import

Signature

isIterable(value): value

Parameters

value
unknown

Import

Signature

isPrimitive(value): value

Parameters

value
unknown

Import

Signature

isTemplatePartActive(part): boolean

Parameters

part
TemplatePart

A sentinel value that signals that a value was handled by a directive and should not be written to the DOM.

Import

Type

symbol

A Part that controls a location within a Node tree. Like a Range, NodePart has start and end locations and can set and update the Nodes between those locations.

Import

Details

NodeParts support several value types: primitives, Nodes, TemplateResults, as well as arrays and iterables of those types.

Methods and properties

Parameters
options
RenderOptions

appendInto(container): void

Permalink to appendInto View source

Appends this part into a container.

Parameters
container
Node
Details

This part must be empty, as its contents are not automatically moved.

Appends this part into a parent part.

Parameters
part
NodePart
Details

This part must be empty, as its contents are not automatically moved.

clear(startNode): void

Permalink to clear View source
Parameters
startNode
Node

Commits the current part value, causing it to actually be written to the DOM.

Details

Directives are run at the start of commit, so that if they call part.setValue(...) synchronously that value will be used in the current commit, and there's no need to call part.commit() within the directive. If directives set a part value asynchronously, then they must call part.commit() manually.

Inserts this part after the ref node (between ref and ref's next sibling). Both ref and its next sibling must be static, unchanging nodes such as those that appear in a literal section of a template.

Parameters
ref
Node
Details

This part must be empty, as its contents are not automatically moved.

Inserts this part after the ref part.

Parameters
ref
NodePart
Details

This part must be empty, as its contents are not automatically moved.

Sets the current part value, but does not write it to the DOM.

Parameters
value
unknown

Import

Type

WeakMap<Node, NodePart>

Sets attribute values for PropertyParts, so that the value is only set once even if there are multiple parts for a property.

Import

Details

If an expression controls the whole property value, then the value is simply assigned to the property under control. If there are string literals or multiple expressions, then the strings are expressions are interpolated into a string first.

Methods and properties

new PropertyCommitter(element, name, strings): PropertyCommitter

Permalink to constructor View source
Parameters
element
MDN Element
name
string
strings
ReadonlyArray<string>

readonly name: string

Permalink to name View source

readonly parts: ReadonlyArray<AttributePart>

Permalink to parts View source

readonly single: boolean

Permalink to single View source

readonly strings: ReadonlyArray<string>

Permalink to strings View source

Import

Methods and properties

new PropertyPart(committer): PropertyPart

Permalink to constructor
Parameters
committer
AttributeCommitter

Commits the current part value, causing it to actually be written to the DOM.

Details

Directives are run at the start of commit, so that if they call part.setValue(...) synchronously that value will be used in the current commit, and there's no need to call part.commit() within the directive. If directives set a part value asynchronously, then they must call part.commit() manually.

Sets the current part value, but does not write it to the DOM.

Parameters
value
unknown

Removes nodes, starting from start (inclusive) to end (exclusive), from container.

Import

Signature

removeNodes(container, start, end): void

Parameters

container
Node
start
null | Node
end
null | Node

Reparents nodes, starting from start (inclusive) to end (exclusive), into another container (could be the same container), before before. If before is null, it appends the nodes to the container.

Import

Signature

reparentNodes(container, start, end, before): void

Parameters

container
Node
start
null | Node
end
null | Node
before
null | Node

An updatable Template that tracks the location of dynamic parts.

Import

Methods and properties

new Template(result, element): Template

Permalink to constructor View source

readonly parts: Array<TemplatePart>

Permalink to parts View source

Import

Type

Map<string, TemplateCache>

The default TemplateFactory which caches Templates keyed on result.type and result.strings.

Import

Signature

templateFactory(result): Template

Parameters

result
TemplateResult

An instance of a Template that can be attached to the DOM and updated with new values.

Import

Methods and properties

new TemplateInstance(template, processor, options): TemplateInstance

Permalink to constructor View source
Parameters
template
Template
processor
TemplateProcessor
options
RenderOptions
Parameters
values
ReadonlyArray<unknown>

Import

Type

(part: Part) => void

The Part interface represents a dynamic part of a template instance rendered by lit-html.

Import

Methods and properties

readonly value: unknown

Permalink to value View source

commit(): void

Permalink to commit

Commits the current part value, causing it to actually be written to the DOM.

Details

Directives are run at the start of commit, so that if they call part.setValue(...) synchronously that value will be used in the current commit, and there's no need to call part.commit() within the directive. If directives set a part value asynchronously, then they must call part.commit() manually.

setValue(value): void

Permalink to setValue

Sets the current part value, but does not write it to the DOM.

Parameters
value
unknown

The value that will be committed.

Import

Methods and properties

handleAttributeExpressions(element, name, strings, options): ReadonlyArray<Part>

Permalink to handleAttributeExpressions

Create parts for an attribute-position binding, given the element, attribute name, and string literals.

Parameters
element
MDN Element

The element containing the binding

name
string

The attribute name, including a possible prefix. The name may be prefixed by . (for a property binding), @ (for an event binding) or ? (for a boolean attribute binding).

strings
ReadonlyArray<string>

The array of literal strings that form the static part of the attribute value. There are always at least two strings, even for fully-controlled bindings with a single expression. For example, for the binding attr="${e1}-${e2}", the strings array includes three strings (['', '-', ''])—the text before the first expression (the empty string), the text between the two expressions ('-'), and the text after the last expression (another empty string).

options
RenderOptions

handleTextExpression(options): NodePart

Permalink to handleTextExpression

Create parts for a text-position binding.

Parameters
options
RenderOptions