66 </h1 >
77</p >
88
9- ` htm ` is an implementation of JSX-like syntax in plain JavaScript, using [ Tagged Templates] .
10- It lets your build apps using Preact/React/etc directly in the browser.
11- JSX can be converted to ` htm ` with only a few tiny modifications.
12- Templates are parsed by the browser's HTML parser and cached, achieving minimal overhead.
9+ ` htm ` is ** JSX-like syntax in plain JavaScript** - no transpiler necessary.
10+
11+ Develop with React/Preact directly in the browser, then compile ` htm ` away for production.
12+
13+ It's built using [ Tagged Templates] and the browser's HTML parser.
1314
1415## ` htm ` by the numbers:
1516
1617🐣 ** 700 bytes** when used directly in the browser
1718
18- ⚛️ ** 500 bytes** when used with Preact _ (the magic of gzip 🌈)_
19+ ⚛️ ** 500 bytes** when used with Preact _ (thanks gzip 🌈)_
1920
2021🏅 ** 0 bytes** when compiled using [ babel-plugin-htm]
2122
2223
23- ## Syntax: Like JSX but more lit
24+ ## Syntax: like JSX but also lit
2425
2526The syntax is inspired by [ lit-html] , but includes features familiar to anyone who works with JSX:
2627
@@ -29,11 +30,14 @@ The syntax is inspired by [lit-html], but includes features familiar to anyone w
2930- Components: ` <${Foo}> ` _ (where ` Foo ` is a component reference)_
3031- Boolean attributes: ` <div draggable /> `
3132
32- ## Syntax: better than JSX?
33+
34+ ## Improvements over JSX
3335
3436` htm ` actually takes the JSX-style syntax a couple steps further!
37+
3538Here's some ergonomic features you get for free that aren't present in JSX:
3639
40+ - ** No transpiler necessary**
3741- HTML's optional quotes: ` <div class=foo> `
3842- HTML's self-closing tags: ` <img src=${url}> `
3943- Optional end-tags: ` <section><h1>this is the whole template! `
@@ -51,24 +55,29 @@ This meant giving up JSX, and the closest alternative was [Tagged Templates]. So
5155
5256` htm ` is published to npm, and accessible via the unpkg.com CDN:
5357
54- ** For npm:**
58+ ** via npm:**
5559
5660``` js
5761npm i htm
5862```
5963
60- ** To hotlink :**
64+ ** hotlinking from unpkg :** _ (no build tool needed!) _
6165
6266``` js
6367import htm from ' https://unpkg.com/htm?module'
64- import { html , render } from ' https://unpkg.com/htm/preact?module'
68+ const html = htm .bind (React .createElement );
69+ ```
70+
71+ ``` js
72+ // just want htm + preact in a single file? there's a highly-optimized version of that:
73+ import { html , render } from ' https://unpkg.com/htm/preact/standalone.mjs'
6574```
6675
6776## Usage
6877
6978Since ` htm ` is a generic library, we need to tell it what to "compile" our templates to.
7079
71- The target should be a function of the form ` h(tag, props, ...children) ` , and can return anything.
80+ The target should be a function of the form ` h(tag, props, ...children) ` _ ( [ hyperscript ] ) _ , and can return anything.
7281
7382``` js
7483// this is our hyperscript function. for now, it just returns an description object.
@@ -117,20 +126,20 @@ It's a single HTML file, and there's no build or tooling. You can edit it with n
117126<html lang =" en" >
118127 <title >htm Demo</title >
119128 <script type =" module" >
120- import { html , Component , render } from ' https://unpkg.com/htm/preact?module ' ;
129+ import { html , Component , render } from ' https://unpkg.com/htm/preact/standalone.mjs ' ;
121130
122131 class App extends Component {
123132 addTodo () {
124- const { todos } = this .state ;
133+ const { todos = [] } = this .state ;
125134 this .setState ({ todos: todos .concat (` Item ${ todos .length } ` ) });
126135 }
127136 render ({ page }, { todos = [] }) {
128137 return html `
129138 <div class =" app" >
130- <${ Header} name="MyApp: ${ page} " />
139+ <${ Header} name="ToDo's ( ${ page} ) " />
131140 <ul >
132141 ${ todos .map (todo => html `
133- <li >{todo}</li >
142+ <li >$ { todo} </li >
134143 ` )}
135144 </ul >
136145 <button onClick =${this.addTodo.bind(this)} >Add Todo</button >
@@ -140,14 +149,14 @@ It's a single HTML file, and there's no build or tooling. You can edit it with n
140149 }
141150 }
142151
143- render (html ` <${ App} page="To-Do's " />` , document .body );
152+ render (html ` <${ App} page="All " />` , document .body );
144153 </script >
145154</html >
146155```
147156
148- How nifty is that?
157+ ** Here's a [ live version ] ( https://htm-demo-preact.glitch.me/ ) . **
149158
150- Notice there's only one import - here we're using the prebuilt Preact integration since it's easier to import and a little bit smaller.
159+ 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.
151160
152161The same example works fine without the prebuilt version, just using two imports:
153162
@@ -157,7 +166,7 @@ import htm from 'htm';
157166
158167const html = htm .bind (h);
159168
160- render (html ` <${ App} page="To-Do's " />` , document .body );
169+ render (html ` <${ App} page="All " />` , document .body );
161170```
162171
163172## Other Uses
@@ -204,3 +213,4 @@ console.log(html`
204213[ lit-html VSCode extension ] : https://marketplace.visualstudio.com/items?itemName=bierner.lit-html
205214[ vhtml ] : https://github.com/developit/vhtml
206215[ jsxobj ] : https://github.com/developit/jsxobj
216+ [ hyperscript ] : https://github.com/hyperhype/hyperscript
0 commit comments