JSX-like syntax using tagged template literals for Virtual DOM without transpilation
npx @tessl/cli install tessl/npm-htm@3.1.0HTM provides JSX-like syntax using tagged template literals for Virtual DOM creation, eliminating the need for transpilation. It's a lightweight library (under 600 bytes) that enables component-based user interfaces with any hyperscript-compatible renderer like React, Preact, or custom rendering functions.
npm install htmESM (preferred):
import htm from "htm";
// Create bound template function
const html = htm.bind(h); // where h is your hyperscript functionCommonJS:
const htm = require("htm");
const html = htm.bind(h);UMD (browser):
<script src="https://unpkg.com/htm/dist/htm.umd.js"></script>
<script>
const html = htm.bind(h);
</script>import htm from "htm";
// Your hyperscript function (h)
const h = (tag, props, ...children) => ({ tag, props, children });
// Bind HTM to your hyperscript function
const html = htm.bind(h);
// Use tagged template syntax
const vdom = html`
<div class="container">
<h1>Hello, ${name}!</h1>
<button onclick=${handleClick}>Click me</button>
</div>
`;
// Works with components
const MyComponent = ({ title }) => html`<h2>${title}</h2>`;
const app = html`<${MyComponent} title="Welcome" />`;
// Multiple root elements
const items = html`
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
`;HTM consists of several key components:
The primary HTM function factory that creates tagged template functions bound to any hyperscript-compatible renderer.
const htm: {
bind<HResult>(
h: (type: any, props: Record<string, any>, ...children: any[]) => HResult
): (strings: TemplateStringsArray, ...values: any[]) => HResult | HResult[];
};Key Features:
h(type, props, ...children)Pre-configured HTM integration for Preact applications with optimized imports and type definitions.
// From htm/preact
declare const html: (strings: TemplateStringsArray, ...values: any[]) => VNode;
declare function render(tree: VNode, parent: HTMLElement): void;
declare class Component { /* Preact Component class */ }Features:
htmlStreamlined HTM integration for React applications with createElement binding.
// From htm/react
declare const html: (strings: TemplateStringsArray, ...values: any[]) => React.ReactElement;Features:
htmlAdvanced customization through custom hyperscript functions and build-time integrations.
Use Cases:
HTM supports extensive HTML-like syntax features:
<div class="foo">content</div><img src="photo.jpg" /><Component /><div class=${className}>${content}</div><input disabled />disabled: true<${Component} prop="value" /><div ...${props} additional="value" /><div>1</div><div>2</div><><child1/><child2/></><div>content</><!-- This is a comment --><${Component}>children</${Component}>HTM provides multiple export configurations for different use cases:
| Export Path | Purpose | Bundle Size |
|---|---|---|
| Core library | ~600 bytes |
| Size-optimized | ~400 bytes |
| Preact integration | Includes Preact |
| Preact + all hooks | Includes Preact + hooks |
| React integration | Requires React |
All variants support multiple module formats (ESM, CommonJS, UMD) and include TypeScript definitions.
// Core HTM type
interface HTM {
bind<HResult>(
h: (type: any, props: Record<string, any>, ...children: any[]) => HResult
): (strings: TemplateStringsArray, ...values: any[]) => HResult | HResult[];
}
// Template function signature
type TemplateFunction<T> = (
strings: TemplateStringsArray,
...values: any[]
) => T | T[];
// Hyperscript function signature
type HyperscriptFunction<T> = (
type: any,
props: Record<string, any> | null,
...children: any[]
) => T;