Skip to content

Commit b5ba4af

Browse files
authored
Demonstrate direct Preact rendering
1 parent 9f55dcb commit b5ba4af

19 files changed

Lines changed: 829 additions & 44 deletions

File tree

‎.changeset/preact-integration.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/ui': minor
3+
---
4+
5+
Export DOM classes and UI types for direct integration with frameworks such as Preact.

‎.github/workflows/check.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
persist-credentials: false
4141
- uses: jdx/mise-action@c2a87611a18de5b3828c5652fe268e992400cb5c # v4.3.0
4242
- run: pnpm install --frozen-lockfile
43+
- run: pnpm --filter @bomb.sh/web-platform-tests build
4344
- run: pnpm test
4445

4546
dom-wpt:

‎.github/workflows/preview.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ jobs:
1111
secrets: inherit
1212
with:
1313
publish: './packages/*'
14+
template: './examples/*'

‎CONTRIBUTING.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ You need the exact Node and pnpm versions listed under `devEngines` in the root
1515

1616
Any version manager works. If you use [mise](https://mise.jdx.dev), which is what our lint, format, and test checks use, export `MISE_IDIOMATIC_VERSION_FILE_ENABLE_TOOLS=node,pnpm` and run `mise install` so it reads those versions from `package.json`.
1717

18-
Then install the dependencies:
18+
Then install the dependencies and build the workspace packages, including WPT:
1919

2020
```sh
2121
pnpm install
22+
pnpm build
2223
```
2324

2425
## Commands you'll use
2526

26-
Run these from the repo root. You don't need to build before you run the tests.
27+
Run these from the repo root. Rebuild after changing shared WPT sources; `pnpm --filter @bomb.sh/web-platform-tests build` rebuilds just that package. Repeated test runs reuse the existing build.
2728

2829
| Command | What it does |
2930
| ------------------------------------------ | ----------------------------------------- |

‎README.md‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ UI provides web-based bindings, which allows us to support any JavaScript framew
1515
npm install @clack/ui
1616
```
1717

18-
## Bindings
19-
20-
| binding | version | changelog |
21-
| --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
22-
| [`@clack/ui-preact`](./packages/preact) | [![npm](https://img.shields.io/npm/v/@clack/ui-preact?style=for-the-badge)](https://www.npmx.dev/package/@clack/ui-preact) | [./packages/preact/CHANGELOG.md](./packages/preact/CHANGELOG.md) |
23-
2418
## Documentation
2519

2620
Coming soon 🙈
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Hello Preact
2+
3+
A Preact component mounted directly with Preact's `render()` on a nested Clack element,
4+
without styles or a global `document`. It keeps `Hello ` on screen while erasing and
5+
typing `World!`, `DOM!`, `Preact!`, and `Clack!` in a repeating loop.
6+
7+
From the workspace root:
8+
9+
```sh
10+
pnpm install
11+
pnpm build
12+
pnpm --filter @examples/hello-world-preact start
13+
```
14+
15+
Press Ctrl+C to exit. Unmounting cancels the animation timer, and disposing the UI
16+
restores the terminal buffer and cursor.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@examples/hello-world-preact",
3+
"version": "0.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"start": "node --import tsx src/main.tsx",
8+
"dev": "tsx watch src/main.tsx",
9+
"lint": "bsh lint"
10+
},
11+
"dependencies": {
12+
"@clack/ui": "workspace:*",
13+
"preact": "11.0.1"
14+
},
15+
"devDependencies": {
16+
"@types/node": "^22.20.0",
17+
"tsx": "^4.19.0"
18+
}
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export interface GreetingFrame {
2+
text: string;
3+
delay: number;
4+
}
5+
6+
const prefix = 'Hello ';
7+
const greetings = ['World!', 'DOM!', 'Preact!', 'Clack!'];
8+
const pause = 900;
9+
const erase = 60;
10+
const type = 100;
11+
12+
/** Each frame says what to show and how long to leave it on screen. */
13+
export function* greetingFrames(): Generator<GreetingFrame, never, void> {
14+
let index = 0;
15+
const suffix = [...greetings[index]!];
16+
yield { text: prefix + suffix.join(''), delay: pause };
17+
18+
while (true) {
19+
while (suffix.length > 0) {
20+
suffix.pop();
21+
yield { text: prefix + suffix.join(''), delay: erase };
22+
}
23+
24+
index = (index + 1) % greetings.length;
25+
const next = [...greetings[index]!];
26+
for (const character of next) {
27+
suffix.push(character);
28+
yield {
29+
text: prefix + suffix.join(''),
30+
delay: suffix.length === next.length ? pause : type,
31+
};
32+
}
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useEffect, useState } from 'preact/hooks';
2+
import { greetingFrames } from './animation.ts';
3+
4+
export function Greeting() {
5+
const [text, setText] = useState('Hello World!');
6+
7+
useEffect(() => {
8+
const animation = greetingFrames();
9+
let timer: ReturnType<typeof setTimeout>;
10+
11+
function advance() {
12+
const frame = animation.next().value;
13+
setText(frame.text);
14+
timer = setTimeout(advance, frame.delay);
15+
}
16+
17+
advance();
18+
return () => clearTimeout(timer);
19+
}, []);
20+
21+
return <span>{text}</span>;
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { createUI } from '@clack/ui';
2+
import { render } from 'preact';
3+
import { Greeting } from './greeting.tsx';
4+
5+
const shutdown = Promise.withResolvers<void>();
6+
const stop = () => shutdown.resolve();
7+
process.once('SIGINT', stop);
8+
process.once('SIGTERM', stop);
9+
10+
try {
11+
await using ui = await createUI({
12+
input: process.stdin,
13+
output: process.stdout,
14+
width: process.stdout.columns ?? 80,
15+
height: process.stdout.rows ?? 24,
16+
});
17+
18+
// A root can be mounted on any Clack element, not just the document root.
19+
const mount = ui.document.createElement('div');
20+
ui.document.documentElement.appendChild(mount);
21+
try {
22+
render(<Greeting />, mount);
23+
await Promise.race([ui.main(), shutdown.promise]);
24+
} finally {
25+
render(null, mount);
26+
}
27+
} finally {
28+
process.off('SIGINT', stop);
29+
process.off('SIGTERM', stop);
30+
}

0 commit comments

Comments
 (0)