Skip to content

Instantly share code, notes, and snippets.

@scarf005
Created May 28, 2024 00:53
Show Gist options
  • Save scarf005/19b8de6583f01edc427b6273b251a385 to your computer and use it in GitHub Desktop.
Save scarf005/19b8de6583f01edc427b6273b251a385 to your computer and use it in GitHub Desktop.
Simplest preact app without any build step
<!DOCTYPE html>
<html>
<head>
<title>Look ma, no build step</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
// @ts-check
/** @typedef {import("https://esm.sh/preact@10.22.0").VNode} VNode */
import { h, render } from "https://esm.sh/v128/preact@10.22.0"
import { signal } from "https://esm.sh/@preact/signals@1.2.3?deps=preact@10.22.0"
import htm from "https://esm.sh/htm@3.1.1"
const html =
/** @type {(strings: TemplateStringsArray, ...values: any[]) => VNode} */ (
htm.bind(h)
)
/** @type {(props: { children: VNode }) => VNode} */
const Foo = ({ children }) =>
html`
<div>
<h1>Foo</h1>
${children}
</div>
`
const App = () => {
const count = signal(0)
return html`
<div>
<${Foo}>
<p>Count: ${count}</p>
<//>
<button onClick=${() => count.value--}>Decrement</button>
<button onClick=${() => count.value++}>Increment</button>
</div>
`
}
const root = /**@type {HTMLElement} */ (document.querySelector("#root"))
render(html`<${App} />`, root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment