developit /htm
Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.
★ 9,051⑂ 181👁 70⑃ 104● 163▤ 16
+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
#76Ampersand escapes in text content handled differently to JSX11 comments · 2019-03-15#175Shouldn’t multiple root elements become a fragment?8 comments · 2020-08-22#129False arguments serialized as strings8 comments · 2019-10-09#52How to use with server side rendering?8 comments · 2018-12-24#124Dynamic prop/attr names7 comments · 2019-09-24#240different attribute handling in div/span etc. and svg6 comments · 2023-02-16#239just a small note: standalone bundle of htm with preact and preact/signals5 comments · 2023-02-10#234 isnt rendering in htm5 comments · 2023-01-03
Latest release
3.1.1 3.1.1 2022-04-26TypeScript 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