Skip to main content

Decorators

Decorators are special functions that can modify the behavior of classes, class methods, and class fields. Lit uses decorators to provide declarative APIs for things like registering elements, reactive properties, and queries.

Decorators are a stage 2 proposal for addition to the ECMAScript standard, which means they're neither finalized nor implemented in browsers yet. Compilers like Babel and TypeScript provide support for proposed features like decorators by compiling them into standard JavaScript a browser can run.

See the Enabling decorators section for more information.

Lit supplies a set of decorators that reduce the amount of boilerplate code you need to write when defining a component. For example, the @customElement and @property decorators make a basic element definition more compact:

The @customElement decorator defines a custom element, equivalent to calling:

The @property decorator declares a reactive property.

See Reactive properties for more information about configuring properties.

DecoratorSummaryMore Info
@customElementDefines a custom elementAbove
@eventOptionsAdds event listener options.Events
@propertyDefines a public property.Properties
@stateDefines a private state propertyProperties
@queryDefines a property that returns an element in the component template.Shadow DOM
@queryAllDefines a property that returns a list of elements in the component template.Shadow DOM
@queryAsyncDefines a property that returns a promise that resolves to an element in the component template.Shadow DOM
@queryAssignedNodesDefines a property that returns the children assigned to a specific slot.Shadow DOM

You can import all the lit decorators via the lit/decorators.js module:

To reduce the amount of code needed to run the component, decorators can be imported individually into component code. All decorators are available at lit/decorators/<decorator-name>. For example,

To use decorators, you need to build your code with a compiler such as TypeScript or Babel.

In the future when decorators become a native web platform feature, this may no longer be necessary.

To use decorators with TypeScript, enable the experimentalDecorators compiler option.

Enabling emitDecoratorMetadata is not required and not recommended.

If you're compiling JavaScript with Babel, you can enable decorators by adding the following plugins:

To enable the plugins, add code like this to your Babel configuration:

Currently the older legacy mode of Babel decorators is not supported, but this may change as Babel evolves. See the Babel documentation if you want to experiment.

Avoiding issues with class fields

Permalink to Avoiding issues with class fields

Class fields are a stage 3 proposal for addition to the ECMAScript standard. They currently have a problematic interaction with the decorators proposal in some circumstances.

There are generally no issues when using TypeScript. However, it's important to ensure that the useDefineForClassFields setting in your tsconfig is set to false. This is currently the default setting.

When using Babel, class fields should only be used for properties that are defined with a decorator.

Using the `static properties` syntax along with class fields is not supported.

The following is ok:

but this is not supported:

When using TypeScript with Babel, it's important to order the TypeScript transform before the decorators transform in your Babel config as follows:

The allowDeclareFields setting is generally not needed, but it can be useful if you want to define a reactive property without using a decorator. For example,