Skip to content

Commit f5a32a8

Browse files
committed
Readme updates for 2.0
1 parent c190cf0 commit f5a32a8

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

README.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111

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

14-
It's built using [Tagged Templates] and the browser's HTML parser. Works in [all modern browsers].
14+
It uses standard JavaScript [Tagged Templates] and works in [all modern browsers].
1515

1616
## `htm` by the numbers:
1717

18-
🐣 **700 bytes** when used directly in the browser
18+
🐣 **< 700 bytes** when used directly in the browser
1919

20-
⚛️ **500 bytes** when used with Preact _(thanks gzip 🌈)_
20+
⚛️ **< 500 bytes** when used with Preact _(thanks gzip 🌈)_
2121

2222
🏅 **0 bytes** when compiled using [babel-plugin-htm]
2323

2424

2525
## Syntax: like JSX but also lit
2626

27-
The syntax is inspired by [lit-html], but includes features familiar to anyone who works with JSX:
27+
The syntax you write when using HTM is as close as possible to JSX:
2828

29-
- Rest spread: `<div ...${props}>`
29+
- Spread props: `<div ...${props}>`
3030
- Self-closing tags: `<div />`
3131
- Components: `<${Foo}>` _(where `Foo` is a component reference)_
3232
- Boolean attributes: `<div draggable />`
@@ -40,17 +40,8 @@ Here's some ergonomic features you get for free that aren't present in JSX:
4040

4141
- **No transpiler necessary**
4242
- HTML's optional quotes: `<div class=foo>`
43-
- HTML's self-closing tags: `<img src=${url}>`
44-
- Optional end-tags: `<section><h1>this is the whole template!`
4543
- Component end-tags: `<${Footer}>footer content<//>`
46-
- Support for HTML comments: `<div><!-- don't delete this! --></div>`
47-
- Syntax highlighting and language support via the [lit-html VSCode extension].
48-
49-
## Project Status
50-
51-
The original goal for `htm` was to create a wrapper around Preact that felt natural for use untranspiled in the browser. I wanted to use Virtual DOM, but I wanted to eschew build tooling and use ES Modules directly.
52-
53-
This meant giving up JSX, and the closest alternative was [Tagged Templates]. So, I wrote this library to patch up the differences between the two as much as possible. As it turns out, the technique is framework-agnostic, so it should work great with most Virtual DOM libraries.
44+
- Syntax highlighting and language support via the [literaly VSCode extension].
5445

5546
## Installation
5647

@@ -78,12 +69,12 @@ import { html, render } from 'https://unpkg.com/htm/preact/standalone.mjs'
7869

7970
Since `htm` is a generic library, we need to tell it what to "compile" our templates to.
8071

81-
The target should be a function of the form `h(tag, props, ...children)` _([hyperscript])_, and can return anything.
72+
The target should be a function of the form `h(type, props, ...children)` _([hyperscript])_, and can return anything.
8273

8374
```js
8475
// this is our hyperscript function. for now, it just returns a description object.
85-
function h(tag, props, ...children) {
86-
return { tag, props, children };
76+
function h(type, props, ...children) {
77+
return { type, props, children };
8778
}
8879
```
8980

@@ -102,15 +93,15 @@ Here's the whole thing for clarity:
10293
```js
10394
import htm from 'htm';
10495

105-
function h(tag, props, ...children) {
106-
return { tag, props, children };
96+
function h(type, props, ...children) {
97+
return { type, props, children };
10798
}
10899

109100
const html = htm.bind(h);
110101

111102
console.log( html`<h1 id=hello>Hello world!</h1>` );
112103
// {
113-
// tag: 'h1',
104+
// type: 'h1',
114105
// props: { id: 'hello' },
115106
// children: ['Hello world!']
116107
// }
@@ -159,11 +150,13 @@ It's a single HTML file, and there's no build or tooling. You can edit it with n
159150
</html>
160151
```
161152

162-
**Here's a [live version](https://htm-demo-preact.glitch.me/).**
153+
[⚡️ **See live version**](https://htm-demo-preact.glitch.me/)
154+
155+
[⚡️ **Try this on CodeSandbox**](https://codesandbox.io/s/x7pmq32j6q)
163156

164-
**Here is a [Sandbox](https://codesandbox.io/s/x7pmq32j6q) you can play with**
157+
How nifty is that?
165158

166-
How nifty is that? Notice there's only one import - here we're using the prebuilt Preact integration since it's easier to import and a bit smaller.
159+
Notice there's only one import - here we're using the prebuilt Preact integration since it's easier to import and a bit smaller.
167160

168161
The same example works fine without the prebuilt version, just using two imports:
169162

@@ -192,7 +185,7 @@ console.log( html`<h1 id=hello>Hello world!</h1>` );
192185
// '<h1 id="hello">Hello world!</h1>'
193186
```
194187

195-
**Webpack configuration via [jsxobj]:** ([details here](https://webpack.js.org/configuration/configuration-languages/#babel-and-jsx))
188+
**Webpack configuration via [jsxobj]:** ([details here](https://webpack.js.org/configuration/configuration-languages/#babel-and-jsx)) _(never do this)_
196189

197190
```js
198191
import htm from 'htm';
@@ -214,9 +207,17 @@ console.log(html`
214207
// }
215208
```
216209

210+
## Project Status
211+
212+
The original goal for `htm` was to create a wrapper around Preact that felt natural for use untranspiled in the browser. I wanted to use Virtual DOM, but I wanted to eschew build tooling and use ES Modules directly.
213+
214+
This meant giving up JSX, and the closest alternative was [Tagged Templates]. So, I wrote this library to patch up the differences between the two as much as possible. As it turns out, the technique is framework-agnostic, so it should work great with most Virtual DOM libraries.
215+
216+
As of 2.0.0, `htm` is stable, well-tested and ready for production use.
217+
217218
[Tagged Templates]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates
218219
[lit-html]: https://github.com/Polymer/lit-html
219-
[babel-plugin-htm]: https://www.npmjs.com/package/babel-plugin-htm
220+
[babel-plugin-htm]: https://github.com/developit/htm/tree/master/packages/babel-plugin-htm
220221
[lit-html VSCode extension]: https://marketplace.visualstudio.com/items?itemName=bierner.lit-html
221222
[vhtml]: https://github.com/developit/vhtml
222223
[jsxobj]: https://github.com/developit/jsxobj

packages/babel-plugin-htm/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,33 @@ React.createElement("div", { id: "foo" }, "hello ", you)
2929
The target "hyperscript" function to compile elements to (see [Babel docs]).
3030
Defaults to: `"h"`.
3131

32+
### `tag=html`
33+
34+
By default, `babel-plugin-htm` will process all Tagged Templates with a tag function named `html`. To use a different name, use the `tag` option in your Babel configuration:
35+
36+
```js
37+
{"plugins":[
38+
["babel-plugin-htm", {
39+
"tag": "myCustomHtmlFunction"
40+
}]
41+
]}
42+
```
43+
44+
### `useBuiltIns=false`
45+
46+
`babel-plugin-htm` transforms prop spreads (`<a ...${b}>`) into `Object.assign()` calls. For browser support reasons, Babel's standard `_extends` helper is used by default. To use native `Object.assign` directly, pass `{useBuiltIns:true}`.
47+
48+
### `variableArity=true`
49+
50+
By default, `babel-plugin-htm` transpiles to the same output as JSX would, which assumes a target function of the form `h(type, props, ...children)`. If, for the purposes of optimization or simplification, you would like all calls to `h()` to be passed exactly 3 arguments, specify `{variableArity:false}` in your Babel config:
51+
52+
```js
53+
html`<div />` // h('div', null, [])
54+
html`<div a />` // h('div', { a: true }, [])
55+
html`<div>b</div>` // h('div', null, ['b'])
56+
html`<div a>b</div>` // h('div', { a: true }, ['b'])
57+
```
58+
3259
### `pragma=false` _(experimental)_
3360

3461
Setting `pragma` to `false` changes the output to be plain objects instead of `h()` function calls:

0 commit comments

Comments
 (0)