A powerful JSX compiler and Vite plugin that transforms JSX into native Lit templates at compile time with zero runtime overhead.
π View Full Documentation | π Getting Started | π¦ Installation
lit-jsx brings the familiar JSX syntax to the Lit ecosystem while maintaining the performance and capabilities that make Lit exceptional.
// Write familiar JSX
function TodoItem({ todo, onToggle, onDelete }) {
return (
<div classList={{ completed: todo.completed }}>
<input
type="checkbox"
checked={as.prop(todo.completed)}
disabled={as.bool(todo.readonly)}
onchange={() => onToggle(todo.id)}
/>
<span>{todo.text}</span>
<button onclick={() => onDelete(todo.id)}>Delete</button>
</div>
);
}
// Compiles to efficient Lit templates
html`
<div class=${classMap({ completed: todo.completed })}>
<input
type="checkbox"
.checked=${todo.completed}
?disabled=${todo.readonly}
@change=${() => onToggle(todo.id)}
/>
<span>${todo.text}</span>
<button @click=${() => onDelete(todo.id)}>Delete</button>
</div>
`- β‘ Zero Runtime Overhead: Pure compile-time transformation to native Lit templates
- π― Type-Safe: Comprehensive TypeScript support with native DOM type mappings for accurate IntelliSense and type checking
- π§ Vite Integration: Seamless setup with the included Vite plugin
- π¨ Lit Ecosystem: Works with all Lit directives, custom elements, and patterns
- ποΈ Flexible Binding: Fine-grained control over attribute, property, and boolean bindings
- π·οΈ Dynamic Tags: Support for conditional element types with static template optimization
- π¦ Function Components: Full support for composable function components
- π Custom Elements: Use LitElement classes directly in JSX with automatic generic type support
- π§© Library Components: Built-in
For,Show, andChoosecomponents for common rendering patterns
npm install @arcmantle/lit-jsx lit-html
# or
pnpm add @arcmantle/lit-jsx lit-html
# or
yarn add @arcmantle/lit-jsx lit-htmlFor complete setup instructions, see the Installation Guide and Getting Started Guide.
// vite.config.ts
import { litJsx } from '@arcmantle/lit-jsx/vite-jsx-preserve';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
litJsx({
legacyDecorators: true
})
],
});{
"compilerOptions": {
"jsx": "preserve",
"jsxImportSource": "@arcmantle/lit-jsx"
}
}import { LitElement } from 'lit';
import { For, Show, Choose } from '@arcmantle/lit-jsx';
export class MyComponent extends LitElement {
render() {
return (
<div>
<h1>Hello lit-jsx!</h1>
<p>JSX compiled to Lit templates with utility components</p>
<Show when={this.items.length > 0}>
{(length) => (
<For each={this.items}>
{(item, index) => <div>{item}</div>}
</For>
)}
</Show>
</div>
);
}
}For complete documentation, visit the full documentation site.
- Tag names (like
<counter-element>) work directly without special attributes - Classes (like
<Counter />) require thestaticattribute - Optional: Enable
useImportDiscovery: truefor automatic detection
- Default: Attribute binding (
value={x}) as.prop(): Property binding (.property=${x})as.bool(): Boolean attribute binding (?disabled=${x})
classList/class={{...}}: Conditional classes withclassMapstyleList/style={{...}}: Dynamic styles withstyleMaponclick,onchange, etc.: Event handlersref: Element referencesdirective: Apply Lit directives{...props}: Spread attributes
Stateless components that receive props and return JSX. Support TypeScript, generics, children, and all JSX features.
Use LitElement classes directly in JSX with full type safety and generic support. Classes require the static attribute.
Use as.tag() for conditional element types with optimized static templates.
Built-in components for common patterns:
<For>: List rendering with keys and separators<Show>: Conditional rendering with optional fallback<Choose>: Switch-like multi-condition rendering
For comprehensive guides, examples, and best practices, see the full documentation.
- Lit Directives: Use all Lit directives (
when,repeat,guard, etc.) - Performance: Optimization tips and compiled templates
- TypeScript: Advanced typing patterns
Plugin options:
legacyDecorators: Enable legacy decorator supportuseCompiledTemplates: Compile-time template optimization (default: true)useImportDiscovery: Auto-detect custom elements (default: false)
Full compatibility with:
- LitElement and reactive properties
- All Lit directives
- Web Components standards
- TypeScript with native DOM types
Migrating from React or Lit templates? See the migration guide.
Contributions, issues or requests are welcome!
Apache-2.0
....