developit /htm

Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.

★ 9,051⑂ 181👁 70⑃ 104● 163▤ 16
JavaScriptApache-2.0Created 2018-07-30
TA的项目
下载:3.1.13.1.1 ↓3.1.12022-04-26
+0
日增长
9,051
stars
181
forks
70
watchers
104
prs
163
issues
16
releases

README

HTM (Hyperscript Tagged Markup)
  


  


htm is JSX-like syntax in plain JavaScript - no transpiler necessary.

Develop with React/Preact directly in the browser, then compile htm away for production.

It uses standard JavaScript [Tagged Templates] and works in [all modern browsers].

 htm by the numbers:

🐣  instead of 
- Self-closing tags: 
- Components:  instead of  _(where Foo is a component reference)_
- Boolean attributes: 


 Improvements over JSX

htm actually takes the JSX-style syntax a couple steps further!

Here's some ergonomic features you get for free that aren't present in JSX:

- No transpiler necessary
- HTML's optional quotes: 
- Component end-tags: footer content
- Syntax highlighting and language support via the [lit-html VSCode extension] and [vim-jsx-pretty plugin].
- Multiple root element (fragments): 
- Support for HTML-style comments: 

 Installation

htm is published to npm, and accessible via the unpkg.com CDN:

via npm:

js
npm i htm


hotlinking from unpkg: _(no build tool needed!)_

js
import htm from 'https://unpkg.com/htm?module'
const html = htm.bind(React.createElement);


js
// just want htm + preact in a single file? there's a highly-optimized version of that:
import { html, render } from 'https://unpkg.com/htm/preact/standalone.module.js'


 Usage

If you're using Preact or React, we've included off-the-shelf bindings to make your life easier.
They also have the added benefit of sharing a template cache across all modules.

js
import { render } from 'preact';
import { html } from 'htm/preact';
render(htmlHello!, document.body);


Similarly, for React:

js
import ReactDOM from 'react-dom';
import { html } from 'htm/react';
ReactDOM.render(htmlHello!, document.body);


 Advanced Usage

Since htm is a generic library, we need to tell it what to "compile" our templates to.
You can bind htm to any function of the form h(type, props, ...children) _([hyperscript])_.
This function can return anything - htm never looks at the return value.

Here's an example h() function that returns tree nodes:

js
function h(type, props, ...children) {
  return { type, props, children };
}


To use our custom h() function, we need to create our own html tag function by binding htm to our h() function:

js
import htm from 'htm';

const html = htm.bind(h);


Now we have an html() template tag that can be used to produce objects in the format we created above.

Here's the whole thing for clarity:

js
import htm from 'htm';

function h(type, props, ...children) {
  return { type, props, children };
}

const html = htm.bind(h);

console.log( htmlHello world! );
// {
//   type: 'h1',
//   props: { id: 'hello' },
//   children: ['Hello world!']
// }


If the template has multiple element at the root level
the output is an array of h results:

js
console.log(html
  Hello
  World!
);
// [
//   {
//     type: 'h1',
//     props: { id: 'hello' },
//     children: ['Hello']
//   },
//   {
//     type: 'div',
//     props: { class: 'world' },
/

Top issues

Latest release

3.1.1 3.1.1 2022-04-26
TypeScript Changes

 Fix nodenext module support from TypeScript 4.7 by @wight554 in https://github.com/developit/htm/pull/221

 Docs and CI stuff
 Skip running tests on outdated Node 10 by @marvinhagemeister in https://github.com/developit/htm/pull/215
 Fixed babel-plugin-htm's documentation of import options by @drjayvee in https://github.com/developit/htm/pull/210

 New Contributors
 @marvinhagemeister made their first contribution in https://github.com/developit/htm/pull/215
 @wight554 made their first contribution in https://github.com/developit/htm/pull/221
 @drjayvee made the

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON