Skip to content
Tutorials Center
My learning

Topics

Lit and Web Components Tutorial

Learn Lit and Web Components step by step: 38 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 What Are Web Components? Learn what web components are, the four browser features behind them, why they work in every framework, and build a tiny working component with no libraries.
  2. 2 What Is Lit? Meet Lit, the 5 KB library for building fast web components. See how it compares with vanilla custom elements, what its packages do, and run a counter in seconds.
  3. 3 Your First Lit Component Build your first Lit component step by step. Learn the anatomy of a LitElement class: properties, styles, render, events, and how to use the finished element in HTML.
  4. 4 Lit Setup, CDN and Tooling Choose how to run Lit: a CDN import, an import map, or npm with Vite. Learn the project layout, TypeScript decorator settings, editor tooling and dev versus production builds.

Custom Elements

  1. 5 Custom Elements: The Basics Learn how to build a custom element from scratch: extend HTMLElement, pick a valid tag name, register it with customElements.define, and use it like any built-in tag.
  2. 6 Custom Element Lifecycle Callbacks Understand the four lifecycle callbacks of a custom element: connectedCallback, disconnectedCallback, attributeChangedCallback and adoptedCallback, with live examples and cleanup rules.
  3. 7 Attributes vs Properties in Custom Elements Learn the difference between HTML attributes and JavaScript properties, how to sync them with getters and setters, and why Lit treats them as two ways to set the same value.
  4. 8 The Template Element Learn how the template element holds inert HTML, how to clone it with importNode and cloneNode, and how Lit's html tag builds on the same idea for fast rendering.
  5. 9 Upgrading and Defining Elements See how the browser upgrades unknown tags when you call customElements.define, how to wait with whenDefined, avoid layout flashes with :defined, and extend built-in elements.

Shadow DOM and Styling

  1. 10 Shadow DOM Basics Learn what shadow DOM is, how attachShadow creates a private tree, what open and closed modes mean, how style scoping works and how Lit creates the shadow root for you.
  2. 11 Styling Lit Components Style Lit components with the css tag and static styles: scoped rules, :host and :host-context, dynamic classes, style bindings, unsafeCSS and why inline styles still have a place.
  3. 12 CSS Custom Properties and Parts Make shadow DOM components themeable: expose CSS custom properties with defaults, share design tokens, open specific elements with part and exportparts, and choose between them.
  4. 13 Shared and Adopted Stylesheets Share CSS between web components with constructable stylesheets and adoptedStyleSheets, reuse css tag fragments in Lit, build a design-token module and handle global styles safely.
  5. 14 Shadow DOM Events and Focus Understand how events cross shadow boundaries: retargeting, composed and bubbles, composedPath, which built-in events escape, plus delegatesFocus, tabindex and focus behavior.

Templates and Directives

  1. 15 Lit Templates and Bindings Master Lit templates: the html tag, child expressions, attribute, boolean attribute, property and event bindings with the dot, question mark and at prefixes, and how updates stay fast.
  2. 16 Conditional Rendering in Lit Show and hide content in Lit templates with ternaries, logical operators, the nothing value, early returns, the when and choose directives, and learn when to use hidden or CSS instead.
  3. 17 Lists and Repeating Templates Render lists in Lit with Array.map and the repeat directive: understand keys, DOM reuse, reordering with state preserved, empty states, filtering, sorting and when map is enough.
  4. 18 Built-in Lit Directives A tour of Lit's built-in directives: classMap, styleMap, ifDefined, live, ref, createRef, keyed, cache, guard, until, unsafeHTML and more, with runnable examples and when to use each.
  5. 19 Template Composition and Reuse Break big Lit templates into small pieces: helper render methods, template functions, child components, SVG and MathML templates, static html and render() into any container.

Reactive Properties

  1. 20 Reactive Properties and Internal State Learn how Lit reactive properties and internal state trigger re-renders, how static properties work, why class fields break reactivity, and how to use hasChanged.
  2. 21 Attributes, Reflection and Converters in Lit Understand how Lit maps HTML attributes to properties, the Boolean, Number, Object and Array types, reflect, attribute names, and writing your own converter.
  3. 22 The Lit Update Lifecycle Explained Follow a Lit update from property change to DOM, and learn when to use willUpdate, shouldUpdate, firstUpdated, updated, requestUpdate and updateComplete.
  4. 23 Reactive Controllers in Lit Build reusable, stateful logic for Lit components with reactive controllers: hostConnected, hostUpdate, requestUpdate, a timer, a mouse tracker and a media query controller.
  5. 24 Loading Async Data in Lit Components Load data in Lit components without race conditions: model pending, complete and error states, cancel stale requests with AbortController and see how @lit/task does it.

Events and Slots

  1. 25 Events in Lit: Listening and Dispatching Handle DOM events with @ bindings in Lit and dispatch your own CustomEvents, including bubbles, composed, detail payloads, retargeting and event naming conventions.
  2. 26 Slots and Component Composition in Lit Compose Lit components with the slot element: default and named slots, fallback content, slotchange events, assignedElements, ::slotted styling and common slot pitfalls.
  3. 27 Component Communication Patterns in Lit Learn how Lit components talk to each other: properties down, events up, shared stores with controllers, mediator components and when to avoid reaching into children.
  4. 28 The Context Protocol and @lit/context Share data with deeply nested Lit components without prop drilling. Learn how the context protocol works, then use createContext, ContextProvider and ContextConsumer.

Forms and Accessibility

  1. 29 Form-Associated Custom Elements with Lit Build custom form controls in Lit that submit with the form, validate and reset. Learn formAssociated, ElementInternals, setFormValue, setValidity and form lifecycle callbacks.
  2. 30 Accessibility Basics for Lit Components Make Lit and web components accessible: native elements first, ARIA roles and states, delegatesFocus, labels across shadow DOM, keyboard support and ElementInternals ARIA.
  3. 31 Accessible Widget Patterns: Tabs, Dialogs and Menus Build keyboard-accessible web component widgets in Lit: roving tabindex tabs, a modal dialog with focus return, a live-region toast and a listbox with aria-activedescendant.

Testing and Tooling

  1. 32 Testing Basics for Lit and Web Components Learn how to test Lit components in a real browser: create a fixture, wait for updateComplete, query the shadow DOM, assert and clean up. Includes a tiny test runner you can run here.
  2. 33 Testing Slots, Keyboard Input and Controllers in Lit Go beyond basic component tests: check slotted content, simulate keyboard input, test reactive controllers with a fake host and fake timers, and keep tests reliable.
  3. 34 Building and Publishing Lit Components Turn a Lit component into a reusable package: project layout, TypeScript and bundling, package.json exports, the Custom Elements Manifest, naming rules and release checks.

Projects and Practice

  1. 35 Project: Build a Todo List with Lit Build a complete todo list in Lit with two cooperating components: add, toggle, delete and filter tasks, with keyed lists, custom events and accessible controls.
  2. 36 Project: Build an Accessible Tabs Component Build a reusable, accessible tabs component in Lit using named slots, ARIA roles, roving tabindex, arrow-key navigation, a change event and CSS custom properties for theming.
  3. 37 Project: Sortable, Filterable Data Table in Lit Build a data table web component in Lit with sorting, text filtering, pagination, a column definition API, custom cell renderers and proper table accessibility.
  4. 38 Lit Practice: Exercises and Common Mistakes Learn the most common Lit and web component mistakes by running broken code and its fix: mutation, attributes vs properties, missing super calls, leaks, styling and events.