lit-html Templates

3.3.2 · active · verified Sun Apr 19

lit-html is a concise and efficient JavaScript library for creating declarative HTML templates using standard JavaScript template literals. It enables developers to render and dynamically update DOM with high performance, as it only modifies the specific parts of the DOM that have changed, avoiding the overhead of a virtual DOM. Currently at version 3.3.2, lit-html is actively maintained as a core part of the Lit project, which focuses on building fast and lightweight web components. It differentiates itself through its tight integration with native browser APIs, small bundle size, and a highly optimized rendering engine. While it can be used standalone, most users developing web components with the Lit library are encouraged to import its functionalities through the main `lit` package for a more integrated development experience. Its release cadence typically aligns with the broader Lit ecosystem updates, often seeing patch releases and minor versions together.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates defining a dynamic lit-html template with event handling and efficiently updating the DOM.

import { html, render } from 'lit-html';

// Define a template function that returns a lit-html TemplateResult
const greetingTemplate = (name, count) => html`
  <div>
    <h1>Hello, ${name}!</h1>
    <p>You have clicked the button ${count} times.</p>
    <button @click=${() => updateCount(name, count + 1)}>Click Me</button>
  </div>
`;

let currentCount = 0;

// Function to update the count and re-render the template
function updateCount(name, newCount) {
  currentCount = newCount;
  render(greetingTemplate(name, currentCount), document.body);
}

// Initial render to the document body
updateCount('World', 0);

view raw JSON →