Lit Web Components Best Practices
An Agent Plugin (spec v1.0.0) containing comprehensive best practices for building Lit web components, optimized for AI-assisted code generation, review, and refactoring.
Overview
This skill contains 34 rules across 9 categories, prioritized by impact to guide automated refactoring and code generation:
| Category | Rules | Focus |
|---|---|---|
| Component Structure | 4 | Properties, state, TypeScript patterns |
| Rendering | 5 | Templates, directives, performance |
| Styling | 4 | Static styles, theming, CSS parts |
| Events | 3 | Custom events, naming, cleanup |
| Lifecycle | 4 | Callbacks, timing, async patterns |
| Accessibility | 4 | ARIA, focus, form association, ElementInternals |
| Performance | 4 | Updates, caching, lazy loading |
| Composition & Data | 3 | Reactive controllers, @lit/task, @lit/context |
| Security | 1 | Untrusted markup in templates |
Installation
Agent Plugins-compatible clients
This repository is a conformant Agent Plugin: clone it (or point your client at it) and the
client discovers skills/lit-best-practices/ from plugin.json.
Claude Code
Install as a plugin (manifest at .claude-plugin/plugin.json, skills auto-discovered from
skills/), or copy the skill directly:
npx skills add artmsilva/lit-best-practices
Manual Installation
Copy skills/lit-best-practices/ into your project's skills directory, or paste its SKILL.md
into a conversation.
Structure
.
├── plugin.json # Agent Plugins v1.0.0 manifest
├── .claude-plugin/plugin.json # Claude Code plugin manifest
├── README.md # This file
└── skills/
└── lit-best-practices/
├── SKILL.md # Lightweight index + quick reference
├── AGENTS.md # High-level guidance for AI agents
└── rules/ # Individual rule files (loaded on-demand)
├── 1-1-use-decorators.md
├── 2-1-pure-render.md
└── ...
The rules are split into individual files so agents can load only the rules relevant to the current task, saving context.
Usage
The skill automatically activates when Claude detects tasks involving:
- Lit web components
- Custom elements
- Shadow DOM
- Reactive properties
- Web component testing
Example Prompts
"Create a Lit component for a dropdown menu with keyboard navigation"
"Review this Lit component for accessibility issues"
"Refactor this component to follow Lit best practices"
"Add form association to this custom input component"
Priority Levels
Rules are categorized by impact:
| Priority | Description | Action |
|---|---|---|
| CRITICAL | Major performance or correctness issues | Fix immediately |
| HIGH | Significant impact on maintainability/performance | Address in current PR |
| MEDIUM | Best practice violations | Address when touching related code |
| LOW | Style preferences and micro-optimizations | Consider during refactoring |
Key Patterns Covered
Component Structure
- TypeScript decorators for properties
- Separating public API (
@property) from internal state (@state) - Property reflection guidelines
- Default values and type safety
Rendering
- Pure
render()methods - Using
nothingfor conditional content repeat()directive for keyed listscache()directive for view switching- Derived state in
willUpdate()
Styling
- Static styles (never inline
<style>) :hostelement styling patterns- CSS custom properties for theming
- CSS parts for deep customization
Events
- Composed events for shadow DOM
- Naming conventions
- Event listener cleanup
Lifecycle
- Correct
supercall ordering firstUpdatedfor DOM operationswillUpdatevsupdatedusage- Async patterns with
updateComplete
Accessibility
delegatesFocusfor interactive components- ARIA roles and states
- Keyboard interaction
- Form-associated custom elements
Performance
- Custom
hasChangedfunctions - Property update batching
- Lazy loading
- Memoization patterns
Composition & Data
- Reactive controllers instead of mixins
@lit/taskfor async data and abort handling@lit/contextinstead of prop drilling
Security
unsafeHTML/unsafeSVGsanitization rules- URL binding sinks
Reference Resources
- Lit Documentation
- Agent Plugins Specification
- Open Web Components
- Lion Web Components
- Shoelace
- Adobe Spectrum Web Components
- web.dev Custom Elements Best Practices
Contributing
Contributions welcome! Please ensure:
- Rules include clear incorrect/correct examples
- Impact level is justified
- TypeScript examples are properly typed
- Accessibility implications are considered
License
MIT