Skip to content

Commit f2bdc36

Browse files
authored
docs: Add HTM example to No-Build docs, reword some things (#1224)
1 parent 6b99d15 commit f2bdc36

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

content/en/guide/v10/no-build-workflows.md

+42-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ applications without them.
1010

1111
No-build workflows are a way to develop web applications while forgoing build tooling, instead relying on the browser
1212
to facilitate module loading and execution. This is a great way to get started with Preact and can continue to work
13-
very well at all scales, but isn't entirely without difficulties.
13+
very well at all scales.
1414

1515
---
1616

@@ -20,14 +20,14 @@ very well at all scales, but isn't entirely without difficulties.
2020

2121
## Import Maps
2222

23-
An [Import Map](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) is a newer feature
23+
An [Import Map](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) is a newer browser feature
2424
that allows you to control how browsers resolve module specifiers, often to convert bare specifiers such as `preact`
2525
to a CDN URL like `https://esm.sh/preact`. While many do prefer the aesthetics import maps can provide, there are also
26-
objective advantages to the centralization of dependencies, such as easier versioning, reduced/removed duplication, and
26+
objective advantages to the centralization of dependencies such as easier versioning, reduced/removed duplication, and
2727
better access to more powerful CDN features.
2828

29-
This isn't to say you need import maps, but for those choosing to forgo build tooling, they are a great option to at least
30-
be aware of.
29+
We do generally recommend using import maps for those choosing to forgo build tooling as they work around some issues
30+
you may encounter using bare CDN URLs in your import specifiers (more on that below).
3131

3232
### Basic Usage
3333

@@ -55,9 +55,7 @@ utilize import maps, but a basic example looks like the following:
5555
import { html } from 'htm/preact';
5656
5757
export function App() {
58-
return html`
59-
<h1>Hello, World!</h1>
60-
`;
58+
return html`<h1>Hello, World!</h1>`;
6159
}
6260
6361
render(html`<${App} />`, document.getElementById('app'));
@@ -70,10 +68,9 @@ We create a `<script>` tag with a `type="importmap"` attribute, and then define
7068
inside of it as JSON. Later, in a `<script type="module">` tag, we can import these modules using bare specifiers,
7169
similar to what you'd see in Node.
7270

73-
> **Note:** We use `?external=preact` in the example above as https://esm.sh will helpfully provide the
71+
> **Important:** We use `?external=preact` in the example above as https://esm.sh will helpfully provide the
7472
> module you're asking for as well as its dependencies -- for `htm/preact`, this means also providing a
75-
> copy of `preact`. However, Preact and many other libraries need to be used as singletons (only a single
76-
> active instance at a time) which creates a problem.
73+
> copy of `preact`. However, Preact must be used only as a singleton with only a single copy included in your app.
7774
>
7875
> By using `?external=preact`, we tell `esm.sh` that it shouldn't provide a copy of `preact`, we can handle
7976
> that ourselves. Therefore, the browser will use our importmap to resolve `preact`, using the same Preact
@@ -121,3 +118,37 @@ query parameter, but other CDNs may work differently.
121118
}
122119
</script>
123120
```
121+
122+
## HTM
123+
124+
Whilst JSX is generally the most popular way to write Preact applications, it requires a build step to convert the non-standard syntax into something browsers and other runtimes can understand natively. Writing `h`/`createElement` calls by hand can be a bit tedious though with less than ideal ergonomics, so we instead recommend a JSX-like alternative called [HTM](https://github.com/developit/htm).
125+
126+
Instead of requiring a build step (though it can use one, see [`babel-plugin-htm`](https://github.com/developit/htm/tree/master/packages/babel-plugin-htm)), HTM uses [Tagged Templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) syntax, a feature of JavaScript that's been around since 2015 and is supported in all modern browsers. This is an increasingly popular way to write Preact apps and is likely the most popular for those choosing to forgo a build step.
127+
128+
HTM supports all standard Preact features, including Components, Hooks, Signals, etc., the only difference being the syntax used to write the "JSX" return value.
129+
130+
```js
131+
// --repl
132+
import { render } from 'preact';
133+
// --repl-before
134+
import { useState } from 'preact/hooks';
135+
import { html } from 'htm/preact';
136+
137+
function Button({ action, children }) {
138+
return html`<button onClick=${action}>${children}</button>`;
139+
}
140+
141+
function Counter() {
142+
const [count, setCount] = useState(0);
143+
144+
return html`
145+
<div class="counter-container">
146+
<${Button} action=${() => setCount(count + 1)}>Increment<//>
147+
<input readonly value=${count} />
148+
<${Button} action=${() => setCount(count - 1)}>Decrement<//>
149+
</div>
150+
`;
151+
}
152+
// --repl-after
153+
render(<Counter />, document.getElementById('app'));
154+
```

0 commit comments

Comments
 (0)