Skip to content

Commit 8dbb336

Browse files
committed
Add support for Preact
1 parent 1aa1fee commit 8dbb336

25 files changed

Lines changed: 1000 additions & 24 deletions

‎.changeset/preact-integration.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clack/ui': minor
3+
'@clack/ui-preact': minor
4+
---
5+
6+
Add Preact 11 bindings for mounting into Clack elements, with Clack-specific JSX types and disposable roots. Export the DOM classes and UI types needed by framework integrations from `@clack/ui`.

‎.gitattributes‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Unified diffs deliberately contain space-prefixed context lines and blank lines.
2+
*.patch -whitespace

‎.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/*'
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 on a nested Clack element, without styles or a global
4+
`document`. It keeps `Hello ` on screen while erasing and typing `World!`, `DOM!`,
5+
`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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
"@clack/ui-preact": "workspace:*",
14+
"preact": "11.0.1"
15+
},
16+
"devDependencies": {
17+
"@types/node": "^22.20.0",
18+
"tsx": "^4.19.0"
19+
}
20+
}
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createUI } from '@clack/ui';
2+
import { createRoot } from '@clack/ui-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+
using root = createRoot(mount);
22+
root.render(<Greeting />);
23+
24+
await Promise.race([ui.main(), shutdown.promise]);
25+
} finally {
26+
process.off('SIGINT', stop);
27+
process.off('SIGTERM', stop);
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["node"],
5+
"jsx": "react-jsx",
6+
"jsxImportSource": "@clack/ui-preact"
7+
},
8+
"include": ["src"]
9+
}

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"build": "pnpm --filter \"@clack/*\" run build",
3232
"format": "bsh format",
3333
"format:check": "bsh format --check",
34-
"lint": "bsh lint .",
35-
"test": "bsh test"
34+
"lint": "bsh lint && pnpm -r lint",
35+
"test": "vitest run"
3636
},
3737
"devDependencies": {
3838
"@bomb.sh/tools": "^0.8.0",

0 commit comments

Comments
 (0)