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.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install 25e2a55c-35b8-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
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.
常见问题
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.
引用来源 (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
讨论
相关资产
SFML — Simple and Fast Multimedia Library for C++
A cross-platform C++ library providing a simple interface to graphics, audio, networking, and system hardware for games and multimedia applications.
NextUI — Beautiful Fast React Component Library with Tailwind CSS
A modern React UI library built on Tailwind CSS and React Aria that provides accessible, customizable, and visually polished components with built-in dark mode and animation support.
Day.js — Fast 2KB Date Library for JavaScript
An immutable date-time library for JavaScript that serves as a modern, lightweight alternative to Moment.js with the same familiar API.
Calibre-Web — Self-Hosted eBook Library & Reader
Calibre-Web provides a polished web interface for browsing, reading, and managing your Calibre eBook library from any device with a browser.