Skip to content

Commit 7561eb0

Browse files
committed
Restore the basic styled DOM example
1 parent 2f28029 commit 7561eb0

10 files changed

Lines changed: 205 additions & 0 deletions

File tree

‎.changeset/dom-public-elements.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/ui': patch
3+
---
4+
5+
Export Document, Element, and Text for applications that build terminal UI trees directly.

‎.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/*'

‎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 exec bsh test examples/basic/src/app.test.ts
40+
```

‎examples/basic/package.json‎

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

‎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.documentElement!.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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createUI } from '@clack/ui';
2+
import { createApp } from './app.ts';
3+
4+
const controller = new AbortController();
5+
const ui = await createUI({
6+
height: 7,
7+
input: process.stdin,
8+
output: process.stdout,
9+
signal: controller.signal,
10+
});
11+
12+
const stop = (): void => controller.abort();
13+
14+
try {
15+
ui.document.documentElement!.appendChild(createApp(ui.document));
16+
process.stdin.once('data', stop);
17+
process.stdin.once('end', stop);
18+
process.once('SIGINT', stop);
19+
process.stdin.resume();
20+
await ui.main();
21+
} finally {
22+
controller.abort();
23+
process.stdin.off('data', stop);
24+
process.stdin.off('end', stop);
25+
process.off('SIGINT', stop);
26+
process.stdin.pause();
27+
}

‎examples/basic/tsconfig.json‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"paths": {
5+
"@clack/ui": ["../../packages/ui/src/index.ts"]
6+
},
7+
"composite": false,
8+
"declaration": false,
9+
"declarationMap": false,
10+
"noEmit": true,
11+
"types": [
12+
"node"
13+
]
14+
},
15+
"include": ["src"]
16+
}

‎packages/ui/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export { createUI } from './ui.ts';
22
export {
3+
Document,
4+
Element,
5+
Text,
36
createHTMLDocument,
47
HTMLDocument,
58
HTMLElement,

‎pnpm-lock.yaml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)