Skip to content

Lit and Tooling

The vanilla Custom Elements API lets you build your own HTML elements, but as a component grows more complex, managing state, re-rendering the DOM, and scoped styles by hand gets increasingly tedious.

Lit is a small library (~5 KB, verified against lit.dev, Lit 3.x) from Google that sits on top of standard Custom Elements. Lit adds reactive properties, declarative templates via tagged template literals, and scoped styles, in a concise and efficient package.

When a reactive property changes, Lit does not immediately re-render. Instead it schedules a microtask update, checks whether an update is needed, runs your render() method, diffs the result with the previous render, and then calls the updated() hook if you defined one.

flowchart LR
  PropChange["Property
changes"] --> Schedule["Schedule
microtask
update"]
  Schedule --> ShouldUpdate["shouldUpdate()"]
  ShouldUpdate --> Render["render()
lit-html diff"]
  Render --> Updated["updated()
optional hook"]
Lit reactive update cycle
LessonWhat you learn
Why LitVanilla pain points and when Lit helps
LitElementReactive properties, render, scoped styles
Using in FrameworksDrop web components into React, Vue, Angular
Distributingnpm publishing, Custom Elements Manifest, browser support

Here is the simplest possible Lit element to confirm the library loads and renders correctly in the playground runner.

What does Lit add on top of the standard Custom Elements API?
Approximately how large is the Lit library?
What triggers a re-render in a LitElement?