Skip to content

How to understand a Lit (web components) codebase

Lit builds standard web components. Each component is a class extending `LitElement`, registered as a custom HTML tag, with reactive properties and an `html` tagged template. Components talk through attributes, properties and DOM events, so they work in any framework or none.

How to tell it's Lit (web components)

  • lit in package.json
  • Classes extending LitElement with @customElement('my-tag') or customElements.define
  • html and css tagged template literals

Where things usually live

src/components/*.ts
One custom element per file, usually named after its tag.
src/index.ts
Imports every component so the tags are registered.
src/styles/
Shared css templates.
custom-elements.json
Optional manifest describing each element's API, generated by an analyzer.
index.html or demo/
Pages that use the elements, for development.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. The file that registers all elements
  2. The top-level element used in the page
  3. custom-elements.json, if present, for a quick map of properties and events

How a request moves through the code

  1. 1Importing a component module registers its custom tag with the browser.
  2. 2When the tag appears in HTML, the browser creates the element and Lit renders its render() template into the shadow DOM.
  3. 3Setting a reactive property (@property or @state) schedules an efficient re-render.
  4. 4Children notify parents by dispatching CustomEvents that bubble up the DOM.

Questions to ask about the repository

  • List each custom element with its public properties and the events it fires.
  • How does data flow between these components: properties, events or a shared context?
  • Which styles are shared, and how do they pierce or respect the shadow DOM?

What trips people up

  • Shadow DOM isolates styles, so global CSS doesn't reach inside components unless it is designed to, through CSS custom properties or ::part.
  • Events must set composed: true to cross shadow boundaries; a listener that never fires is often missing this.
  • Attributes are strings and properties can be any type; check which one a parent is setting.