Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 39 ยท Tooling and Patterns

Lit and Web Component Libraries

See what libraries such as Lit add on top of raw custom elements: reactive properties, templates and batched updates. Includes a runnable mini-base-class stand-in.

By now you have written components with nothing but the browser. You have also probably felt the repetition: reading attributes, converting types, reflecting properties, wiring a render method, batching updates. Component libraries package those patterns so you write less code. The best known today is Lit, a small library (a few kilobytes) built directly on custom elements and shadow DOM. Others include Stencil (a compiler), FAST, Shoelace and Spectrum Web Components (component collections), and Polymer, the ancestor of Lit.

The site runs your examples in an isolated sandbox without a package manager or build step, so real Lit code in this lesson is read-only. To let you run something, we build a tiny hand-written stand-in called MiniElement that copies the core ideas. It is a teaching tool, not Lit, and it lacks almost everything that makes Lit fast and robust.

What a library adds

Chore in raw custom elements What Lit does
Declare observedAttributes and parse types static properties = { count: { type: Number } }
Reflect property to attribute reflect: true
Re-render when state changes Properties are reactive and render is batched in a microtask
Build the DOM by hand Tagged template literals html with efficient DOM diffing
Attach events @click=${this.onClick} in the template
Share styles static styles = css... using adopted stylesheets
Property binding .items=${list} sets a property, ?disabled=${flag} toggles an attribute

A Lit component (read-only)

This is real Lit 3 code. It would need npm install lit and a bundler, or an import from a CDN module in a normal page.

JavaScript
import { LitElement, html, css } from "lit";

class CounterButton extends LitElement {
  static properties = {
    count: { type: Number },
    label: { type: String },
  };

  static styles = css`
    button { font: inherit; padding: 6px 12px; }
  `;

  constructor() {
    super();
    this.count = 0;
    this.label = "Clicks";
  }

  render() {
    return html`
      <button @click=${() => this.count++}>
        ${this.label}: ${this.count}
      </button>
    `;
  }
}
customElements.define("counter-button", CounterButton);

Read it carefully: there is no attachShadow, no observedAttributes, no attributeChangedCallback, and no manual render call. Setting this.count++ schedules an update; the html template updates only the parts that changed.

A runnable stand-in: MiniElement

Here is the idea reduced to about thirty lines: declared properties become accessors that schedule one batched render, and attributes are converted by type.

MiniElement: a stand-in for the Lit core idea

Click the inner button to increment, or press the outside button: two property changes cause a single render. A real library does the same but updates only changed DOM nodes instead of replacing the whole innerHTML, which would reset focus, scroll and text selection inside the shadow root.

What a library adds

Reactive properties Declare once
Templates Diff the DOM
Batched updates One render

Same custom elements underneath

1 / 2

Choosing: raw, Lit, or a compiler

  • Raw custom elements: no dependencies, ideal for a few small components, learning, or when bundle size is extreme. Expect more boilerplate.
  • Lit: a small runtime (around 5 KB compressed), the common choice for design systems. It still produces standard custom elements.
  • Compilers (Stencil, Svelte custom elements, Vue defineCustomElement): write in your framework's syntax and ship web components. Convenient, but the output pulls in each tool's runtime.
  • Ready-made collections (Shoelace, Spectrum, Material Web): use them instead of building basic controls yourself.

Questions to ask: How many components? Who consumes them (one app or many frameworks)? Do you need server rendering? Does the team already have a framework?

Using libraries safely

  1. Pin versions and check browser support matches your needs.
  2. Check accessibility: a library does not make a component accessible; the lessons on ARIA and keyboard support still apply.
  3. Avoid lock-in: expose your public API as plain attributes, properties and events, so consumers do not depend on the library.
  4. Know the escape hatch: since libraries produce real custom elements, you can mix library and raw components on one page.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Lit produce?

    Lit components are real custom elements, usable like any other tag.
  2. 2. Why does the MiniElement stand-in batch renders in a microtask?

    Batching merges many synchronous changes into a single update.
  3. 3. What is a weakness of replacing shadowRoot.innerHTML on every render?

    Rebuilding the whole subtree discards DOM state; real libraries update only changed nodes.
  4. 4. Which is a reason to choose raw custom elements?

    For a few small components, the platform alone avoids dependencies and bundle cost.

Summary

  • Libraries like Lit remove boilerplate: reactive properties, templating, styles and batched updates.
  • They build on standard custom elements and shadow DOM, so output stays interoperable.
  • Real Lit code is read-only here; MiniElement is a labelled stand-in for the core idea.
  • Choose by component count, consumers and team needs, and keep your public API library-neutral.

Next: Testing Web Components