Skip to content

Commit 3969b70

Browse files
committed
declare and layout based on element style
1 parent 07d4868 commit 3969b70

18 files changed

Lines changed: 1377 additions & 20 deletions

‎examples/basic/README.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Basic example
2+
3+
A DOM version of the original basic panel, using `div`, `span`, text nodes, and
4+
direct `element.style` assignments.
5+
6+
From the repository root:
7+
8+
```sh
9+
pnpm install
10+
pnpm build
11+
pnpm --filter @examples/basic start
12+
```
13+
14+
The panel appears in the terminal's alternate screen while the example runs.
15+
Press Enter or Ctrl+C to finish and restore the previous screen:
16+
17+
```text
18+
┌──────────────────────┐
19+
│ @clack/ui │
20+
│ │
21+
│ status ● ready │
22+
│ version 0.0.0 │
23+
│ render @bomb.sh/tty │
24+
└──────────────────────┘
25+
```
26+
27+
The title is cyan, labels are gray, status is green, remaining values inherit
28+
white from the panel, and borders are blue. Each label and value is a separate
29+
`span` in a flex row; this does not need mixed-color runs inside one text flow.
30+
31+
Borders occupy their own cells in the CSS box model, so the panel has one cell
32+
of horizontal padding and zero vertical padding. Together with its one-cell
33+
border, this preserves the original example's 24-column, 7-row appearance.
34+
35+
The tests render the same `createApp()` tree through the UI source, checking
36+
layout and colors. Run them with:
37+
38+
```sh
39+
pnpm test examples/basic/src/app.test.ts
40+
```

‎examples/basic/package.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@examples/basic",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"start": "node --experimental-strip-types src/index.ts"
7+
},
8+
"dependencies": {
9+
"@clack/ui": "workspace:*"
10+
}
11+
}

‎examples/basic/src/app.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { createTestUI } from '../../../packages/ui/test/ui.ts';
3+
import { createApp } from './app.ts';
4+
5+
const panel = [
6+
'┌──────────────────────┐',
7+
'│ @clack/ui │',
8+
'│ │',
9+
'│ status ● ready │',
10+
'│ version 0.0.0 │',
11+
'│ render @bomb.sh/tty │',
12+
'└──────────────────────┘',
13+
];
14+
15+
describe('basic example', () => {
16+
it('renders the original panel with styled labels and inherited value colors', async () => {
17+
await using ui = await createTestUI({ cols: 32, rows: 9 });
18+
const { document, terminal } = ui;
19+
document.body.appendChild(createApp(document));
20+
21+
await expect
22+
.poll(() => terminal.screen.getLines().map((line) => line.trimEnd()), { timeout: 2000 })
23+
.toEqual([...panel, '', '']);
24+
25+
const runs = [
26+
{ row: 1, col: 2, content: '@clack/ui', color: { r: 0, g: 255, b: 255 } },
27+
{ row: 3, col: 2, content: 'status ', color: { r: 128, g: 128, b: 128 } },
28+
{ row: 3, col: 10, content: '● ready', color: { r: 0, g: 128, b: 0 } },
29+
{ row: 4, col: 2, content: 'version', color: { r: 128, g: 128, b: 128 } },
30+
{ row: 4, col: 10, content: '0.0.0', color: { r: 255, g: 255, b: 255 } },
31+
{ row: 5, col: 2, content: 'render ', color: { r: 128, g: 128, b: 128 } },
32+
{ row: 5, col: 10, content: '@bomb.sh/tty', color: { r: 255, g: 255, b: 255 } },
33+
];
34+
for (const run of runs) {
35+
for (const [offset, char] of [...run.content].entries()) {
36+
const cell = terminal.getCell(run.row, run.col + offset);
37+
expect(cell.char).toBe(char);
38+
expect(cell.fg).toMatchObject(run.color);
39+
}
40+
}
41+
42+
for (const [row, line] of panel.entries()) {
43+
for (const [col, char] of [...line].entries()) {
44+
if ('┌┐└┘─│'.includes(char)) {
45+
expect(terminal.getCell(row, col).fg).toMatchObject({ r: 0, g: 0, b: 255 });
46+
}
47+
}
48+
}
49+
});
50+
});

‎examples/basic/src/app.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { Document, Element, ColorValue } from '@clack/ui';
2+
3+
/* oxlint-disable bombshell-dev/exported-function-async -- Builds a detached DOM tree synchronously. */
4+
/** Build the basic panel using elements, text nodes, and inline declarations. */
5+
export function createApp(document: Document): Element {
6+
const panel = document.createElement('div');
7+
panel.style.flexDirection = 'column';
8+
panel.style.gap = '1px';
9+
panel.style.paddingLeft = '1px';
10+
panel.style.paddingRight = '1px';
11+
panel.style.color = 'white';
12+
panel.style.borderStyle = 'solid';
13+
panel.style.borderColor = 'blue';
14+
panel.style.borderTopWidth = '1px';
15+
panel.style.borderRightWidth = '1px';
16+
panel.style.borderBottomWidth = '1px';
17+
panel.style.borderLeftWidth = '1px';
18+
panel.appendChild(span('@clack/ui', 'cyan'));
19+
20+
const details = document.createElement('div');
21+
details.style.flexDirection = 'column';
22+
details.appendChild(row('status ', span('● ready', 'green')));
23+
details.appendChild(row('version', span('0.0.0')));
24+
details.appendChild(row('render ', span('@bomb.sh/tty')));
25+
panel.appendChild(details);
26+
27+
return panel;
28+
29+
function span(content: string, color?: ColorValue): Element {
30+
const element = document.createElement('span');
31+
if (color !== undefined) {
32+
element.style.color = color;
33+
}
34+
element.appendChild(document.createTextNode(content));
35+
return element;
36+
}
37+
38+
function row(label: string, value: Element): Element {
39+
const element = document.createElement('div');
40+
element.style.gap = '1px';
41+
element.appendChild(span(label, 'gray'));
42+
element.appendChild(value);
43+
return element;
44+
}
45+
}

‎examples/basic/src/index.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createUI } from '@clack/ui';
2+
import { createApp } from './app.ts';
3+
4+
const ui = await createUI({
5+
height: 7,
6+
input: process.stdin,
7+
output: process.stdout,
8+
});
9+
10+
const { promise: stopped, resolve: stop } = Promise.withResolvers<void>();
11+
12+
try {
13+
ui.document.body.appendChild(createApp(ui.document));
14+
process.stdin.once('data', stop);
15+
process.stdin.once('end', stop);
16+
process.once('SIGINT', stop);
17+
process.stdin.resume();
18+
await Promise.race([ui.main(), stopped]);
19+
} finally {
20+
process.stdin.off('data', stop);
21+
process.stdin.off('end', stop);
22+
process.off('SIGINT', stop);
23+
process.stdin.pause();
24+
await ui[Symbol.asyncDispose]();
25+
}

‎examples/basic/tsconfig.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"composite": false,
5+
"declaration": false,
6+
"declarationMap": false,
7+
"noEmit": true,
8+
"types": ["node"]
9+
}
10+
}

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
"format": "bsh format",
3333
"format:check": "bsh format --check",
3434
"lint": "bsh lint .",
35-
"test": "bsh test"
35+
"test": "bsh test --exclude '**/node_modules/**' --exclude '**/dist/**'"
3636
},
3737
"devDependencies": {
3838
"@bomb.sh/tools": "^0.6.1",
3939
"@termless/core": "^0.10.2",
4040
"@termless/test": "^0.10.2",
4141
"@types/node": "^22.20.0",
42+
"@typescript/native-preview": "7.0.0-dev.20260623.1",
4243
"node-pty": "1.2.0-beta.15",
4344
"vitest": "^4.1.9"
4445
},

‎packages/ui/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
"test": "bsh test"
3636
},
3737
"dependencies": {
38-
"@bomb.sh/tty": "catalog:"
38+
"@bomb.sh/tty": "catalog:",
39+
"@standard-schema/spec": "^1.1.0",
40+
"hueful": "^0.8.1"
3941
},
4042
"devEngines": {
4143
"packageManager": {

‎packages/ui/src/dom.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import type { RenderApi } from './render.ts';
2+
import { StyleDeclaration } from './style.ts';
23

34
/**
45
* Container type for valid names of createElement()
56
*/
67
export interface Elements {
78
body: Element;
9+
div: Element;
10+
span: Element;
811
}
912

1013
export class Document {
1114
readonly implementation: DOMImplementation;
1215
readonly body: Element;
16+
1317
constructor(implementation: DOMImplementation) {
1418
this.implementation = implementation;
1519
this.body = new Element(this, 'body');
@@ -32,6 +36,7 @@ export class Node {
3236
readonly nodeName: string;
3337
readonly ownerDocument: Document;
3438
readonly childNodes: Node[];
39+
3540
constructor(document: Document, nodeName: string) {
3641
this.ownerDocument = document;
3742
this.nodeName = nodeName;
@@ -48,11 +53,15 @@ export class Node {
4853
}
4954

5055
export class Element extends Node {
51-
tagName: string;
56+
readonly tagName: string;
57+
readonly style: StyleDeclaration;
5258

5359
constructor(document: Document, tagName: keyof Elements) {
5460
super(document, tagName.toUpperCase());
5561
this.tagName = tagName;
62+
this.style = new StyleDeclaration(() => {
63+
document.implementation.render.request();
64+
});
5665
}
5766

5867
get children(): Element[] {

‎packages/ui/src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export { createUI } from './ui.ts';
2+
export { Document, Element, Text } from './dom.ts';
3+
export type { ColorValue, BorderWidthValue, FlexDirectionValue, DisplayValue, LengthValue, BorderStyleValue } from './style.ts';

0 commit comments

Comments
 (0)