lit-html Templates
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
-
ReferenceError: html is not defined
cause The `html` tag function was not imported or is not in scope.fixAdd `import { html } from 'lit-html';` to the top of your module. -
TypeError: render is not a function
cause The `render` function was not imported, imported incorrectly, or you are trying to use a CommonJS `require()` import in a modern ESM project.fixEnsure `import { render } from 'lit-html';` is used. If using CommonJS, verify your environment supports it or migrate to ESM. -
Uncaught SyntaxError: Cannot use import statement outside a module
cause Your JavaScript file is being loaded as a script, but it uses ES module syntax (e.g., `import` statements).fixAdd `type="module"` to your script tag in HTML (e.g., `<script type="module" src="./my-app.js"></script>`) or ensure your build process correctly transpiles and bundles for your target environment.
Warnings
- breaking `lit-html` moved to being ESM-only. CommonJS `require()` is no longer supported for importing modules.
- breaking Lit 3.0 (and by extension lit-html 3.0) removed many deprecated APIs and required TypeScript 5.0 or newer. Node.js 16 support was also dropped.
- gotcha Directly manipulating DOM elements within a container managed by `lit-html` can lead to unexpected behavior and template desynchronization, as `lit-html` manages the lifecycle and updates of its DOM nodes.
- gotcha Mixing `lit-html` template rendering with other client-side rendering libraries (e.g., React, Vue) in the same DOM container without careful isolation can lead to conflicts and unpredictable UI behavior.
Install
-
npm install lit-html -
yarn add lit-html -
pnpm add lit-html
Imports
- html
const html = require('lit-html').html;import { html } from 'lit-html'; - render
const render = require('lit-html').render;import { render } from 'lit-html'; - TemplateResult
import { TemplateResult } from 'lit-html';import type { TemplateResult } from 'lit-html';
Quickstart
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);