A simple base class for creating fast, lightweight web components
npx @tessl/cli install tessl/npm-lit-element@4.2.0LitElement is a simple base class for creating fast, lightweight web components. It extends the Lit project's ReactiveElement with lit-html templating capabilities, providing reactive properties, lifecycle callbacks, scoped CSS styling, and declarative HTML templating for building efficient custom elements.
npm install lit-elementimport { LitElement, html, css } from "lit-element";CommonJS:
const { LitElement, html, css } = require("lit-element");Decorators (for TypeScript):
import { customElement, property, state, query } from "lit-element/decorators.js";import { LitElement, html, css } from "lit-element";
import { customElement, property } from "lit-element/decorators.js";
@customElement("my-element")
class MyElement extends LitElement {
static styles = css`
:host {
display: block;
padding: 16px;
}
.highlight {
color: blue;
}
`;
@property({ type: String })
name = "World";
@property({ type: Number })
count = 0;
render() {
return html`
<div class="highlight">
<h1>Hello, ${this.name}!</h1>
<p>Count: ${this.count}</p>
<button @click=${this._increment}>Increment</button>
</div>
`;
}
private _increment() {
this.count++;
}
}LitElement is built on several key architectural components:
The main LitElement class that extends ReactiveElement with lit-html templating functionality.
class LitElement extends ReactiveElement {
readonly renderOptions: RenderOptions;
protected createRenderRoot(): Element | ShadowRoot;
protected update(changedProperties: PropertyValues): void;
protected render(): unknown;
connectedCallback(): void;
disconnectedCallback(): void;
}Reactive property management with decorators, type conversion, and change detection for building reactive custom elements.
function property(options?: PropertyDeclaration): PropertyDecorator;
function state(options?: StateDeclaration): PropertyDecorator;
interface PropertyDeclaration {
type?: TypeHint;
attribute?: boolean | string;
reflect?: boolean;
converter?: AttributeConverter;
noAccessor?: boolean;
hasChanged?: HasChanged;
}HTML templating with lit-html providing efficient rendering, event binding, and directive support.
function html(strings: TemplateStringsArray, ...values: unknown[]): TemplateResult;
function svg(strings: TemplateStringsArray, ...values: unknown[]): SVGTemplateResult;
function render(value: unknown, container: Element | DocumentFragment, options?: RenderOptions): RootPart;
const nothing: symbol;
const noChange: symbol;Scoped CSS system with shadow DOM support, providing isolated styling and efficient style application.
function css(strings: TemplateStringsArray, ...values: (CSSResult | number)[]): CSSResult;
function unsafeCSS(value: string): CSSResult;
class CSSResult {
readonly cssText: string;
readonly strings?: TemplateStringsArray;
readonly values?: readonly CSSResult[];
}
type CSSResultGroup = CSSResult | CSSResultArray;
interface CSSResultArray extends Array<CSSResultGroup> {}TypeScript decorators for enhanced development experience with declarative property and element configuration.
function customElement(tagName: string): ClassDecorator;
function property(options?: PropertyDeclaration): PropertyDecorator;
function state(options?: StateDeclaration): PropertyDecorator;
function query(selector: string, cache?: boolean): PropertyDecorator;
function queryAll(selector: string): PropertyDecorator;Built-in directives for advanced templating patterns, conditional rendering, and performance optimization.
function repeat<T>(
items: Iterable<T>,
keyFn: KeyFn<T>,
template: ItemTemplate<T>
): DirectiveResult<typeof RepeatDirective>;
function classMap(classInfo: ClassInfo): DirectiveResult<typeof ClassMapDirective>;
function styleMap(styleInfo: StyleInfo): DirectiveResult<typeof StyleMapDirective>;
function when<T, F>(
condition: boolean,
trueCase: () => T,
falseCase?: () => F
): T | F | typeof nothing;
function map<T>(items: Iterable<T>, template: (item: T, index: number) => unknown): DirectiveResult<typeof MapDirective>;
function join<T>(items: Iterable<T>, joiner: unknown): DirectiveResult<typeof JoinDirective>;
function until(promise: Promise<unknown>, ...defaultContent: unknown[]): DirectiveResult<typeof UntilDirective>;
function asyncAppend(asyncIterable: AsyncIterable<unknown>): DirectiveResult<typeof AsyncAppendDirective>;
function asyncReplace(asyncIterable: AsyncIterable<unknown>): DirectiveResult<typeof AsyncReplaceDirective>;Browser compatibility support for older browsers without native web components support, including ShadyCSS and ShadyDOM integration.
import "lit-element/polyfill-support.js";interface PropertyValues extends Map<PropertyKey, unknown> {}
interface RenderOptions {
host?: object;
renderBefore?: ChildNode | null;
isConnected?: boolean;
}
type TypeHint =
| typeof String
| typeof Number
| typeof Boolean
| typeof Array
| typeof Object;
interface HasChanged {
(value: unknown, oldValue: unknown): boolean;
}
interface AttributeConverter<Type = unknown, TypeHint = unknown> {
fromAttribute?(value: string | null, type?: TypeHint): Type;
toAttribute?(value: Type, type?: TypeHint): unknown;
}
interface ReactiveController {
hostConnected?(): void;
hostDisconnected?(): void;
hostUpdate?(): void;
hostUpdated?(): void;
}
interface ReactiveControllerHost {
addController(controller: ReactiveController): void;
removeController(controller: ReactiveController): void;
requestUpdate(name?: PropertyKey, oldValue?: unknown, options?: object): void;
readonly updateComplete: Promise<boolean>;
}