LitElement is currently in development. It's on the fast track to a 1.0 release, so we encourage you to use it and give us your feedback, but there are things that haven't been finalized yet and you can expect some changes.
LitElement uses lit-html to render into the
element's Shadow DOM
and adds API to help manage element properties and attributes. LitElement reacts to changes in properties
and renders declaratively using lit-html. See the lit-html guide
for additional information on how to create templates for lit-element.
-
Setup properties: LitElement supports observable properties that cause the element to update. These properties can be declared in a few ways:
- As class fields with the
@property()decorator, if you're using a compiler that supports them, like TypeScript or Babel. - With a static
propertiesgetter. - By manually writing getters and setters. This can be useful if tasks should
be performed when a property is set, for example validation. Call
requestUpdate(name, oldValue)in the setter to trigger an update and use any configured property options.
Properties can be given an
optionsargument which is an object that describes how to process the property. This can be done either in the@property({...})decorator or in the object returned from thepropertiesgetter, e.g.static get properties { return { foo: {...} }.Property options include:
attribute: Indicates how and whether the property becomes an observed attribute. If the value isfalse, the property is not added to the staticobservedAttributesgetter. Iftrueor absent, the lowercased property name is observed (e.g.fooBarbecomesfoobar). If a string, the string value is observed (e.gattribute: 'foo-bar').converter: Indicates how to convert the attribute to/from a property. The value can be a function used for both serialization and deserialization, or it can be an object with individual functions via the optional keys,fromAttributeandtoAttribute. A defaultconverteris used if none is provided; it supportsBoolean,String,Number,Object, andArray.type: Indicates the type of the property. This is used only as a hint for theconverterto determine how to convert the attribute to/from a property. Note, when a property changes and the converter is used to update the attribute, the property is never updated again as a result of the attribute changing, and vice versa.reflect: Indicates whether the property should reflect to its associated attribute (as determined by the attribute option). Iftrue, when the property is set, the attribute which name is determined according to the rules for theattributeproperty option will be set to the value of the property converted using the rules from thetypeandconverterproperty options.hasChanged: A function that indicates whether a property should be considered changed when it is set and thus result in an update. The function should take thenewValueandoldValueand returntrueif an update should be requested.
- As class fields with the
-
React to changes: LitElement reacts to changes in properties and attributes by asynchronously rendering, ensuring changes are batched. This reduces overhead and maintains consistent state.
-
Declarative rendering LitElement uses
lit-htmlto declaratively describe how an element should render. Thenlit-htmlensures that updates are fast by creating the static DOM once and smartly updating only the parts of the DOM that change. Pass a JavaScript string to thehtmltag function, describing dynamic parts with standard JavaScript template expressions:- static elements:
html`<div>Hi</div>` - expression:
html`<div>${this.disabled ? 'Off' : 'On'}</div>` - property:
html`<x-foo .bar="${this.bar}"></x-foo>` - attribute:
html`<div class="${this.color} special"></div>` - boolean attribute:
html`<input type="checkbox" ?checked=${checked}>` - event handler:
html`<button @click="${this._clickHandler}"></button>`
- static elements: