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)
litinpackage.json- Classes extending
LitElementwith@customElement('my-tag')orcustomElements.define htmlandcsstagged 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
csstemplates. - 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
- The file that registers all elements
- The top-level element used in the page
custom-elements.json, if present, for a quick map of properties and events
How a request moves through the code
- Importing a component module registers its custom tag with the browser.
- When the tag appears in HTML, the browser creates the element and Lit renders its
render()template into the shadow DOM. - Setting a reactive property (
@propertyor@state) schedules an efficient re-render. - Children 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: trueto 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.