Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/preact-integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': minor
---

Export DOM classes and UI types for direct integration with frameworks such as Preact.
1 change: 1 addition & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ jobs:
secrets: inherit
with:
publish: './packages/*'
template: './examples/*'
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ UI provides web-based bindings, which allows us to support any JavaScript framew
npm install @clack/ui
```

## Bindings

| binding | version | changelog |
| --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
| [`@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) |

## Documentation

Coming soon 🙈
Expand Down
16 changes: 16 additions & 0 deletions examples/hello-world-preact/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Hello Preact

A Preact component mounted directly with Preact's `render()` on a nested Clack element,
without styles or a global `document`. It keeps `Hello ` on screen while erasing and
typing `World!`, `DOM!`, `Preact!`, and `Clack!` in a repeating loop.

From the workspace root:

```sh
pnpm install
pnpm build
pnpm --filter @examples/hello-world-preact start
```

Press Ctrl+C to exit. Unmounting cancels the animation timer, and stopping the UI
restores the terminal buffer and cursor.
19 changes: 19 additions & 0 deletions examples/hello-world-preact/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@examples/hello-world-preact",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"start": "node --import tsx src/main.tsx",
"dev": "tsx watch src/main.tsx",
"lint": "bsh lint"
},
"dependencies": {
"@clack/ui": "workspace:*",
"preact": "11.0.1"
},
"devDependencies": {
"@types/node": "^22.20.0",
"tsx": "^4.19.0"
}
}
34 changes: 34 additions & 0 deletions examples/hello-world-preact/src/animation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export interface GreetingFrame {
text: string;
delay: number;
}

const prefix = 'Hello ';
const greetings = ['World!', 'DOM!', 'Preact!', 'Clack!'];
const pause = 900;
const erase = 60;
const type = 100;

/** Each frame says what to show and how long to leave it on screen. */
export function* greetingFrames(): Generator<GreetingFrame, never, void> {
let index = 0;
const suffix = [...greetings[index]!];
yield { text: prefix + suffix.join(''), delay: pause };

while (true) {
while (suffix.length > 0) {
suffix.pop();
yield { text: prefix + suffix.join(''), delay: erase };
}

index = (index + 1) % greetings.length;
const next = [...greetings[index]!];
for (const character of next) {
suffix.push(character);
yield {
text: prefix + suffix.join(''),
delay: suffix.length === next.length ? pause : type,
};
}
}
}
22 changes: 22 additions & 0 deletions examples/hello-world-preact/src/greeting.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect, useState } from 'preact/hooks';
import { greetingFrames } from './animation.ts';

export function Greeting() {
const [text, setText] = useState('Hello World!');

useEffect(() => {
const animation = greetingFrames();
let timer: ReturnType<typeof setTimeout>;

function advance() {
const frame = animation.next().value;
setText(frame.text);
timer = setTimeout(advance, frame.delay);
}

advance();
return () => clearTimeout(timer);
}, []);

return <span>{text}</span>;
}
32 changes: 32 additions & 0 deletions examples/hello-world-preact/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createUI } from '@clack/ui';
import { render } from 'preact';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh shit! It just dawned on me that we don't even need adapters anymore. Siiiiiiick

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Batteries included!

import { Greeting } from './greeting.tsx';

const controller = new AbortController();
const stop = () => controller.abort();
process.once('SIGINT', stop);
process.once('SIGTERM', stop);

try {
const ui = await createUI({
signal: controller.signal,
input: process.stdin,
output: process.stdout,
width: process.stdout.columns ?? 80,
height: process.stdout.rows ?? 24,
});

// A root can be mounted on any Clack element, not just the document root.
const mount = ui.document.createElement('div');
ui.document.documentElement!.appendChild(mount);
try {
render(<Greeting />, mount);
await ui.main();
} finally {
render(null, mount);
}
} finally {
controller.abort();
process.off('SIGINT', stop);
process.off('SIGTERM', stop);
}
9 changes: 9 additions & 0 deletions examples/hello-world-preact/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node"],
"jsx": "react-jsx",
"jsxImportSource": "preact"
},
"include": ["src"]
}
3 changes: 2 additions & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
}
},
"devDependencies": {
"@bomb.sh/web-platform-tests": "workspace:*"
"@bomb.sh/web-platform-tests": "workspace:*",
"preact": "11.0.1"
}
}
6 changes: 5 additions & 1 deletion packages/ui/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export { createUI } from './ui.ts';
export { createUI, type UI, type UIOptions } from './ui.ts';
export {
createHTMLDocument,
Document,
Element,
Node,
Text,
HTMLDocument,
HTMLElement,
HTMLHtmlElement,
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The script builds the private WPT harness, runs local Vitest tests, and runs the
upstream cases selected by [`wpt.config.ts`](../wpt.config.ts). Use `pnpm test`
from the workspace root to run all workspace tests.

[`preact.test.tsx`](preact.test.tsx) checks direct Preact rendering, updates, events, cleanup, and terminal repainting.

## Terminal fixtures

[`createTestUI`](ui.ts) starts the real UI with an in-process
Expand Down
Loading
Loading