Skip to content

Commit 9ee992c

Browse files
committed
✨ react: add adapter and hello-world example
1 parent 9d87a66 commit 9ee992c

16 files changed

Lines changed: 812 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@examples/hello-world-react",
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-react": "workspace:*",
13+
"react": "^19.2.0"
14+
},
15+
"devDependencies": {
16+
"@types/node": "^22.20.0",
17+
"@types/react": "^19.2.0",
18+
"tsx": "^4.19.0"
19+
}
20+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// oxlint-disable bombshell-dev/exported-function-async -- React components render synchronously.
2+
import { fixed, grow, percent } from '@bomb.sh/tty';
3+
import { rgba } from '@clack/ui-react';
4+
import { type ReactElement, useState } from 'react';
5+
6+
/** Render the interactive hello-world example. */
7+
export function HelloWorld(): ReactElement {
8+
const [say, setSay] = useState('Hello');
9+
const [to, setTo] = useState('World');
10+
11+
return (
12+
<box
13+
layout={{
14+
direction: 'ttb',
15+
gap: 1,
16+
padding: { top: 1, bottom: 1, left: 2, right: 2 },
17+
width: fixed(40),
18+
}}
19+
border={{ color: blue, top: 1, right: 1, bottom: 1, left: 1 }}
20+
>
21+
<text color={cyan}>
22+
{say}, {to}!
23+
</text>
24+
<box layout={{ direction: 'ttb', width: grow() }}>
25+
<box layout={{ direction: 'ltr', gap: 1, width: grow() }}>
26+
<box layout={{ width: percent(0.3) }}>
27+
<text color={gray}>say:</text>
28+
</box>
29+
<box layout={{ width: percent(0.3) }}>
30+
<text color={gray}>to:</text>
31+
</box>
32+
</box>
33+
<box layout={{ direction: 'ltr', gap: 1, width: grow() }}>
34+
<input onInput={(event) => setSay(event.value)} />
35+
<input onInput={(event) => setTo(event.value)} />
36+
</box>
37+
</box>
38+
</box>
39+
);
40+
}
41+
42+
const blue = rgba(0, 0, 238);
43+
const cyan = rgba(0, 205, 205);
44+
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-react';
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-react",
9+
"noEmit": true,
10+
"types": ["node"]
11+
}
12+
}

‎packages/ui-react/package.json‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"name": "@clack/ui-react",
3+
"version": "0.0.0",
4+
"keywords": [
5+
"bombshell",
6+
"cli",
7+
"react"
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+
"react-reconciler": "^0.33.0"
49+
},
50+
"devDependencies": {
51+
"@bomb.sh/tools": "^0.5.4",
52+
"@types/node": "^22.20.0",
53+
"@types/react": "^19.2.0",
54+
"@types/react-reconciler": "^0.33.0",
55+
"react": "^19.2.0",
56+
"vitest": "^4.1.9"
57+
},
58+
"peerDependencies": {
59+
"react": "^19.2.0"
60+
},
61+
"devEngines": {
62+
"packageManager": {
63+
"name": "pnpm",
64+
"version": "^10.7.0",
65+
"onFail": "error"
66+
},
67+
"runtime": {
68+
"name": "node",
69+
"version": "^22.14.0",
70+
"onFail": "error"
71+
}
72+
},
73+
"knip": {
74+
"entry": [
75+
"src/index.ts",
76+
"src/jsx-runtime.ts",
77+
"src/jsx-dev-runtime.ts"
78+
]
79+
}
80+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { type FunctionComponent, createElement } from 'react';
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 { ReactNode } from 'react';
3+
import { describe, expectTypeOf, test } from 'vitest';
4+
import type { JSX } from './jsx-runtime.ts';
5+
6+
interface ReactProbeEvent {
7+
readonly value: number;
8+
}
9+
10+
interface ReactProbeProps {
11+
count: number;
12+
onprobe?: (event: ReactProbeEvent) => void;
13+
}
14+
15+
declare module '@clack/ui/elements' {
16+
interface HostElements {
17+
'@test/react-probe': ReactProbeProps;
18+
}
19+
}
20+
21+
describe('React 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<ReactNode | undefined>();
30+
});
31+
32+
test('maps lowercase host event properties to React 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/react-probe']['count']>().toEqualTypeOf<number>();
43+
expectTypeOf<NonNullable<JSX.IntrinsicElements['@test/react-probe']['onProbe']>>()
44+
.parameter(0)
45+
.toEqualTypeOf<ReactProbeEvent>();
46+
});
47+
});

‎packages/ui-react/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 { HostElement } from '@clack/ui/elements';
3+
import type { Key, ReactNode, Ref } from 'react';
4+
5+
type ReactProperties<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?: ReactNode;
13+
key?: Key | null;
14+
ref?: Ref<HostElement>;
15+
};
16+
17+
export type HostElements = {
18+
[Name in keyof CoreHostElements]: ReactProperties<CoreHostElements[Name]>;
19+
};
20+
21+
export type BoxProps = HostElements['box'];
22+
export type InputProps = HostElements['input'];
23+
export type TextProps = HostElements['text'];

‎packages/ui-react/src/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { rgba } from '@clack/ui';
2+
export { Box, Text } from './components.ts';
3+
export type { BoxProps, HostElements, InputProps, TextProps } from './elements.ts';
4+
export { createRoot, type Root, type RootOptions } from './root.ts';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type * as React from 'react';
2+
import type { HostElements } from './index.ts';
3+
4+
export { Fragment, jsxDEV } from 'react/jsx-dev-runtime';
5+
6+
export declare namespace JSX {
7+
type ElementType = React.JSX.ElementType;
8+
interface Element extends React.JSX.Element {}
9+
interface ElementClass extends React.JSX.ElementClass {}
10+
interface ElementAttributesProperty extends React.JSX.ElementAttributesProperty {}
11+
interface ElementChildrenAttribute extends React.JSX.ElementChildrenAttribute {}
12+
type LibraryManagedAttributes<Component, Props> = React.JSX.LibraryManagedAttributes<
13+
Component,
14+
Props
15+
>;
16+
interface IntrinsicAttributes extends React.JSX.IntrinsicAttributes {}
17+
interface IntrinsicClassAttributes<Instance> extends React.JSX
18+
.IntrinsicClassAttributes<Instance> {}
19+
interface IntrinsicElements extends HostElements {}
20+
}

0 commit comments

Comments
 (0)