Skip to content

Commit 4a9de24

Browse files
committed
✨ preact: add adapter and hello-world example
1 parent 6d79891 commit 4a9de24

16 files changed

Lines changed: 707 additions & 0 deletions

File tree

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+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"start": "tsx src/index.tsx",
7+
"dev": "tsx watch src/index.tsx"
8+
},
9+
"dependencies": {
10+
"@bomb.sh/tty": "^0.7.0",
11+
"@clack/ui": "workspace:*",
12+
"@clack/ui-preact": "workspace:*",
13+
"preact": "11.0.0-beta.2"
14+
},
15+
"devDependencies": {
16+
"@types/node": "^22.20.0",
17+
"tsx": "^4.19.0"
18+
}
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// oxlint-disable bombshell-dev/exported-function-async -- Preact components render synchronously.
2+
import { fixed, grow, percent } from '@bomb.sh/tty';
3+
import { rgba } from '@clack/ui-preact';
4+
import type { VNode } from 'preact';
5+
import { useState } from 'preact/hooks';
6+
7+
/** Render the interactive hello-world example. */
8+
export function HelloWorld(): VNode {
9+
const [say, setSay] = useState('Hello');
10+
const [to, setTo] = useState('World');
11+
12+
return (
13+
<box
14+
layout={{
15+
direction: 'ttb',
16+
gap: 1,
17+
padding: { top: 1, bottom: 1, left: 2, right: 2 },
18+
width: fixed(40),
19+
}}
20+
border={{ color: blue, top: 1, right: 1, bottom: 1, left: 1 }}
21+
>
22+
<text color={cyan}>
23+
{say}, {to}!
24+
</text>
25+
<box layout={{ direction: 'ttb', width: grow() }}>
26+
<box layout={{ direction: 'ltr', gap: 1, width: grow() }}>
27+
<box layout={{ width: percent(0.3) }}>
28+
<text color={gray}>say:</text>
29+
</box>
30+
<box layout={{ width: percent(0.3) }}>
31+
<text color={gray}>to:</text>
32+
</box>
33+
</box>
34+
<box layout={{ direction: 'ltr', gap: 1, width: grow() }}>
35+
<input onInput={(event) => setSay(event.value)} />
36+
<input onInput={(event) => setTo(event.value)} />
37+
</box>
38+
</box>
39+
</box>
40+
);
41+
}
42+
43+
const blue = rgba(0, 0, 238);
44+
const cyan = rgba(0, 205, 205);
45+
const gray = rgba(127, 127, 127);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { stdin, stdout } from 'node:process';
2+
import { createUI } from '@clack/ui';
3+
import { createRoot } from '@clack/ui-preact';
4+
import { HelloWorld } from './app.tsx';
5+
6+
await using ui = await createUI({ input: stdin, output: stdout });
7+
const root = createRoot(ui.host.element);
8+
root.render(<HelloWorld />);
9+
await ui.main();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "@bomb.sh/tools/tsconfig.json",
3+
"compilerOptions": {
4+
"composite": false,
5+
"declaration": false,
6+
"declarationMap": false,
7+
"jsx": "react-jsx",
8+
"jsxImportSource": "@clack/ui-preact",
9+
"noEmit": true,
10+
"types": ["node"]
11+
}
12+
}

‎packages/ui-preact/package.json‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"name": "@clack/ui-preact",
3+
"version": "0.0.0",
4+
"keywords": [
5+
"bombshell",
6+
"cli",
7+
"preact"
8+
],
9+
"homepage": "https://bomb.sh/docs/ui",
10+
"license": "MIT",
11+
"author": {
12+
"name": "Bombshell",
13+
"email": "oss@bomb.sh",
14+
"url": "https://bomb.sh"
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/bombshell-dev/ui.git"
19+
},
20+
"type": "module",
21+
"exports": {
22+
".": {
23+
"types": "./dist/index.d.mts",
24+
"import": "./dist/index.mjs"
25+
},
26+
"./jsx-runtime": {
27+
"types": "./dist/jsx-runtime.d.mts",
28+
"import": "./dist/jsx-runtime.mjs"
29+
},
30+
"./jsx-dev-runtime": {
31+
"types": "./dist/jsx-dev-runtime.d.mts",
32+
"import": "./dist/jsx-dev-runtime.mjs"
33+
},
34+
"./package.json": "./package.json"
35+
},
36+
"publishConfig": {
37+
"access": "public"
38+
},
39+
"scripts": {
40+
"dev": "bsh dev",
41+
"build": "bsh build --dts",
42+
"format": "bsh format",
43+
"lint": "bsh lint",
44+
"test": "bsh test"
45+
},
46+
"dependencies": {
47+
"@clack/ui": "workspace:*"
48+
},
49+
"devDependencies": {
50+
"@bomb.sh/tools": "^0.5.4",
51+
"@types/node": "^22.20.0",
52+
"preact": "11.0.0-beta.2",
53+
"vitest": "^4.1.9"
54+
},
55+
"peerDependencies": {
56+
"preact": ">=11.0.0-beta.2 <12"
57+
},
58+
"devEngines": {
59+
"packageManager": {
60+
"name": "pnpm",
61+
"version": "^10.7.0",
62+
"onFail": "error"
63+
},
64+
"runtime": {
65+
"name": "node",
66+
"version": "^22.14.0",
67+
"onFail": "error"
68+
}
69+
},
70+
"knip": {
71+
"entry": [
72+
"src/index.ts",
73+
"src/jsx-runtime.ts",
74+
"src/jsx-dev-runtime.ts"
75+
]
76+
}
77+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { type FunctionComponent, createElement } from 'preact';
2+
import type { BoxProps, TextProps } from './elements.ts';
3+
4+
export type { BoxProps, TextProps } from './elements.ts';
5+
6+
/** A layout container backed by the Host's `box` element. */
7+
export const Box: FunctionComponent<BoxProps> = (props) => createElement('box', props);
8+
9+
/** A styled text scope backed by the Host's `text` element. */
10+
export const Text: FunctionComponent<TextProps> = (props) => createElement('text', props);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { HostElements as CoreHostElements, InputEvent } from '@clack/ui';
2+
import type { ComponentChildren } from 'preact';
3+
import { describe, expectTypeOf, test } from 'vitest';
4+
import type { JSX } from './jsx-runtime.ts';
5+
6+
interface PreactProbeEvent {
7+
readonly value: number;
8+
}
9+
10+
interface PreactProbeProps {
11+
count: number;
12+
onprobe?: (event: PreactProbeEvent) => void;
13+
}
14+
15+
declare module '@clack/ui/elements' {
16+
interface HostElements {
17+
'@test/preact-probe': PreactProbeProps;
18+
}
19+
}
20+
21+
describe('Preact host elements', () => {
22+
test('maps box and text properties from the core registry', () => {
23+
expectTypeOf<JSX.IntrinsicElements['box']['layout']>().toEqualTypeOf<
24+
CoreHostElements['box']['layout']
25+
>();
26+
expectTypeOf<JSX.IntrinsicElements['text']['color']>().toEqualTypeOf<
27+
CoreHostElements['text']['color']
28+
>();
29+
expectTypeOf<JSX.IntrinsicElements['box']['children']>().toEqualTypeOf<ComponentChildren>();
30+
});
31+
32+
test('maps lowercase host event properties to Preact casing', () => {
33+
expectTypeOf<NonNullable<JSX.IntrinsicElements['input']['onInput']>>()
34+
.parameter(0)
35+
.toEqualTypeOf<InputEvent>();
36+
expectTypeOf<
37+
'oninput' extends keyof JSX.IntrinsicElements['input'] ? true : false
38+
>().toEqualTypeOf<false>();
39+
});
40+
41+
test('derives custom elements from core registry augmentation', () => {
42+
expectTypeOf<JSX.IntrinsicElements['@test/preact-probe']['count']>().toEqualTypeOf<number>();
43+
expectTypeOf<NonNullable<JSX.IntrinsicElements['@test/preact-probe']['onProbe']>>()
44+
.parameter(0)
45+
.toEqualTypeOf<PreactProbeEvent>();
46+
});
47+
});

‎packages/ui-preact/src/elements.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { HostElements as CoreHostElements } from '@clack/ui';
2+
import type { ComponentChildren, Key, Ref } from 'preact';
3+
import type { ElementHandle } from './facade.ts';
4+
5+
type PreactProperties<Properties> = {
6+
[Name in keyof Properties as Name extends string
7+
? Name extends `on${infer Type}`
8+
? `on${Capitalize<Type>}`
9+
: Name
10+
: Name]: Properties[Name];
11+
} & {
12+
children?: ComponentChildren;
13+
key?: Key | null;
14+
ref?: Ref<ElementHandle>;
15+
};
16+
17+
export type HostElements = {
18+
[Name in keyof CoreHostElements]: PreactProperties<CoreHostElements[Name]>;
19+
};
20+
21+
export type BoxProps = HostElements['box'];
22+
export type InputProps = HostElements['input'];
23+
export type TextProps = HostElements['text'];

0 commit comments

Comments
 (0)