To use Lit in Astro, install lit, define your element in a .ts file, and import that file from a <script> in the .astro component that renders the tag. Astro bundles the script, the browser upgrades every <my-element> it finds, and you pass data through attributes. The old @astrojs/lit integration, which server-rendered Lit, is deprecated and not maintained for current Astro versions, so Astro Lit components run client-side.
Key Takeaways
- Install only
lit; no Astro integration or client directive is needed.- Register custom elements by importing them in a component
<script>.- Pass strings and numbers as attributes; complex data via JSON or properties.
- Put fallback content and sizing in the light DOM to avoid empty boxes before upgrade.
Install
# terminal
npm install lit
Define a Lit element
// src/components/elements/rating-stars.ts
import { LitElement, html, css } from 'lit';
export class RatingStars extends LitElement {
static properties = {
value: { type: Number, reflect: true },
max: { type: Number },
};
declare value: number;
declare max: number;
static styles = css`
:host { display: inline-flex; gap: 0.125rem; min-height: 1.5rem; }
button { background: none; border: 0; font-size: 1.25rem; cursor: pointer; color: #d1d5db; }
button[aria-pressed='true'] { color: #f59e0b; }
`;
constructor() {
super();
this.value = 0;
this.max = 5;
}
private set(n: number) {
this.value = n;
this.dispatchEvent(new CustomEvent('rating-change', { detail: n, bubbles: true, composed: true }));
}
render() {
return html`${Array.from({ length: this.max }, (_, i) => html`
<button type="button" aria-label="${i + 1} stars" aria-pressed=${i < this.value}
@click=${() => this.set(i + 1)}>★</button>`)}`;
}
}
customElements.define('rating-stars', RatingStars);
This uses static properties and declare instead of decorators, so it works with Astro’s default TypeScript settings and no extra compiler flags.
Use it in an Astro component
---
// src/components/Rating.astro
interface Props { value?: number; label: string }
const { value = 0, label } = Astro.props;
---
<label class="rating">
<span>{label}</span>
<rating-stars value={value} max="5">
<span class="fallback">{value} out of 5</span>
</rating-stars>
</label>
<script>
import './elements/rating-stars';
</script>
Astro renders <rating-stars> as plain HTML with its attributes. The script registers the element, and the browser upgrades every instance on the page. Astro processes and bundles the script once per page, however many ratings you render.
The fallback span is light DOM content. It shows before the element upgrades, and because the Lit template has no <slot>, it disappears from view afterwards. It also gives the element some size before JavaScript runs.
Passing data
- Strings and numbers: attributes, as above. Lit converts them with the
typeyou declared. - Booleans: present or absent,
<my-el disabled>. Declare{ type: Boolean }. - Objects and arrays: serialize to an attribute,
data={JSON.stringify(items)}, and declare{ type: Object }or{ type: Array }; Lit parses JSON for those types.
Listening to events
Lit elements dispatch normal DOM events, so a plain script, or any island, can listen:
<script>
document.addEventListener('rating-change', (e) => {
console.log('New rating', (e as CustomEvent<number>).detail);
});
</script>
composed: true lets the event cross the shadow root boundary.
Styling and Shadow DOM
Lit styles live inside the shadow root, so Astro’s scoped styles and Tailwind classes on the page don’t reach the element’s internals. Style from outside with CSS custom properties, which do cross the boundary:
/* page CSS */
rating-stars { --star-on: #f59e0b; }
Read them inside with color: var(--star-on, #f59e0b). Use ::part() for more control.
No server rendering
Without SSR, the element’s internal markup appears only after JavaScript runs. Keep important text in the light DOM or in surrounding Astro markup so it’s in the HTML, and reserve space with min-height to avoid layout shift. For simpler cases you may not need Lit at all; our guide to Astro web components shows vanilla custom elements, and Astro script tags explains how scripts are bundled. The folder structure docs show where components live, and our Astro themes work with custom elements the same way.
FAQ
Does Astro support Lit?
Yes, client-side. Install lit, define elements in a script imported by an Astro component, and use the tags in markup. The former @astrojs/lit SSR integration is deprecated.
Do Lit components need a client directive in Astro?
No. Client directives are for framework islands. Custom elements upgrade as soon as their definition script runs, which Astro bundles from the component’s <script>.
Why don’t my Tailwind classes style the inside of a Lit element?
Lit renders into Shadow DOM, which page styles don’t reach. Use the component’s static styles, CSS custom properties or ::part() instead.