Lit — Simple Library for Fast Lightweight Web Components
Lit is a simple library for building fast, lightweight web components. Built by Google on top of the standard Web Components APIs, it provides reactive properties, scoped styles, and a declarative templating system in about 5KB.
What it is
Lit is a library built by Google for creating web components using standard Web Components APIs. It adds reactive properties, scoped CSS styles, and a declarative HTML templating system on top of the native Custom Elements and Shadow DOM specifications. The entire library is about 5KB gzipped.
Lit targets frontend developers who want framework-agnostic UI components. Components built with Lit work in any framework (React, Vue, Angular) or with no framework at all, because they compile down to native browser APIs.
How it saves time or tokens
This workflow provides the npm install and a working component template. Instead of learning the raw Custom Elements API and Shadow DOM boilerplate, you get a class-based component model with automatic re-rendering when properties change. The included counter example is a complete, runnable component.
How to use
- Install Lit:
npm i lit
- Create a web component:
import { LitElement, html, css } from 'lit';
import { customElement, property } from 'lit/decorators.js';
@customElement('my-counter')
export class MyCounter extends LitElement {
static styles = css`
button { padding: 8px 16px; font-size: 16px; }
span { margin: 0 12px; font-weight: bold; }
`;
@property({ type: Number }) count = 0;
render() {
return html`
<button @click=${() => this.count--}>-</button>
<span>${this.count}</span>
<button @click=${() => this.count++}>+</button>
`;
}
}
- Use the component in any HTML page:
<script type="module" src="./my-counter.js"></script>
<my-counter count="5"></my-counter>
Example
import { LitElement, html, css } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
@customElement('user-card')
export class UserCard extends LitElement {
static styles = css`
:host { display: block; border: 1px solid #e5e7eb; border-radius: 8px; padding: 16px; }
.name { font-size: 1.2em; font-weight: 600; }
.email { color: #6b7280; }
`;
@property() name = '';
@property() email = '';
@state() private expanded = false;
render() {
return html`
<div class="name">${this.name}</div>
<div class="email">${this.email}</div>
<button @click=${() => this.expanded = !this.expanded}>
${this.expanded ? 'Less' : 'More'}
</button>
${this.expanded ? html`<slot></slot>` : ''}
`;
}
}
Related on TokRepo
- AI coding tools -- Frontend development tools
- Design tools -- UI component design resources
Common pitfalls
- Lit uses Shadow DOM by default, which scopes styles. Global CSS does not penetrate shadow boundaries. Use CSS custom properties (variables) for theming across components.
- TypeScript decorators (@customElement, @property) require experimentalDecorators in tsconfig.json. Without it, compilation fails.
- Lit components must be registered with unique tag names. Using a name that conflicts with existing HTML elements or other custom elements causes errors.
Frequently Asked Questions
Lit produces native web components that work without a framework runtime. React requires the React library to render components. Lit components are smaller and framework-agnostic, while React has a larger ecosystem and more community resources.
Yes. Lit components are standard HTML custom elements. In React, use them like any HTML tag. In Vue, register them as custom elements. No wrappers or adapters needed because they use native browser APIs.
About 5KB gzipped. This includes the template system, reactive properties, and scoped styles. It is significantly smaller than React (40KB+) or Angular (100KB+).
Yes. Lit provides an SSR package (@lit-labs/ssr) that renders components to HTML strings on the server. The client-side library then hydrates the rendered HTML for interactivity.
Yes. Lit is maintained by Google and used in production by Google, Adobe, ING Bank, and other organizations. It is the successor to the Polymer library and has been stable since version 2.0.
Citations (3)
- Lit Official Site— Lit is built by Google on Web Components standards
- Lit GitHub— About 5KB gzipped library size
- MDN Web Components— Web Components browser APIs: Custom Elements and Shadow DOM
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.