Skip to content

Commit d0b7b1b

Browse files
committed
✨ layout: render box and text element trees
1 parent c0a23d5 commit d0b7b1b

7 files changed

Lines changed: 236 additions & 5 deletions

File tree

‎examples/basic/src/index.ts‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { box, text, createRoot, rgba } from '@clack/ui';
1+
import { type BoxProps, type TextProps, rgba } from '@clack/ui';
2+
import { useBoxElement } from '../../../packages/ui/src/elements/box.ts';
3+
import { useTextElement } from '../../../packages/ui/src/elements/text.ts';
4+
import { createHost, type HostElement } from '../../../packages/ui/src/host.ts';
5+
import { layout } from '../../../packages/ui/src/layout.ts';
6+
import { RenderRoot } from '../../../packages/ui/src/render/createRoot.ts';
27

38
const cyan = rgba(0, 205, 205);
49
const green = rgba(0, 205, 0);
510
const white = rgba(229, 229, 229);
611
const gray = rgba(127, 127, 127);
712
const blue = rgba(0, 0, 238);
813

9-
// Framework-free: build a tree with `box` / `text`, render it once.
10-
const root = await createRoot();
14+
const host = createHost();
15+
useBoxElement(host);
16+
useTextElement(host);
1117

1218
const app = box(
1319
{
@@ -23,8 +29,34 @@ const app = box(
2329
),
2430
);
2531

26-
root.render(app);
32+
host.insertBefore(host.element, app);
2733

28-
function row(label: string, value: ReturnType<typeof text>) {
34+
const renderer = new RenderRoot();
35+
await renderer.ready();
36+
renderer.render(...layout(app.node!));
37+
38+
function box(properties: Omit<BoxProps, 'key'>, ...children: HostElement[]): HostElement {
39+
const element = host.createElement('box');
40+
setProperties(element, properties);
41+
for (const child of children) {
42+
host.insertBefore(element, child);
43+
}
44+
return element;
45+
}
46+
47+
function text(properties: TextProps, content: string): HostElement {
48+
const element = host.createElement('text');
49+
setProperties(element, properties);
50+
host.insertBefore(element, host.createLiteral(content));
51+
return element;
52+
}
53+
54+
function row(label: string, value: HostElement): HostElement {
2955
return box({ layout: { direction: 'ltr', gap: 1 } }, text({ color: gray }, label), value);
3056
}
57+
58+
function setProperties(element: HostElement, properties: object): void {
59+
for (const [name, value] of Object.entries(properties)) {
60+
host.setProperty(element, name, value);
61+
}
62+
}

‎packages/ui/src/elements/box.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { open, close, text, type OpenElement } from '@bomb.sh/tty';
2+
import { id } from '../core.ts';
3+
import { type Host } from '../host.ts';
4+
import { LayoutApi, layout } from '../layout.ts';
5+
import { getElement } from '../elements.ts';
6+
7+
export type BoxProps = Omit<OpenElement, 'directive' | 'id'>;
8+
9+
declare module '@clack/ui/elements' {
10+
interface HostElements {
11+
box: BoxProps;
12+
}
13+
}
14+
15+
export function useBoxElement(host: Host) {
16+
LayoutApi.around(host.root, {
17+
*layout([node], next) {
18+
const element = getElement(node);
19+
if (element.name === 'box') {
20+
let content = '';
21+
yield open(id(node), element.properties);
22+
for (const child of element.children) {
23+
if (child.type === 'element') {
24+
if (content !== '') {
25+
yield text(content);
26+
content = '';
27+
}
28+
yield* layout(child.node!);
29+
} else {
30+
content += child.content;
31+
}
32+
}
33+
if (content !== '') {
34+
yield text(content);
35+
}
36+
yield close();
37+
} else {
38+
return yield* next(node);
39+
}
40+
},
41+
});
42+
}

‎packages/ui/src/elements/text.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { text, type Text } from '@bomb.sh/tty';
2+
import { type Host } from '../host.ts';
3+
import { getElement } from '../elements.ts';
4+
import { LayoutApi } from '../layout.ts';
5+
6+
export type TextProps = Omit<Text, 'directive' | 'content'>;
7+
8+
declare module '@clack/ui/elements' {
9+
interface HostElements {
10+
text: TextProps;
11+
}
12+
}
13+
14+
/**
15+
* Install "text" element behavior into a host.
16+
*
17+
* Text elements ignore non-textual children like "box" or "input" and will
18+
* always return an iteration of tty `text()` directives
19+
*/
20+
export function useTextElement(host: Host) {
21+
LayoutApi.around(host.root, {
22+
*layout([node], next) {
23+
const element = getElement(node);
24+
if (element.name === 'text') {
25+
let content = '';
26+
for (const child of element.children) {
27+
if (child.type === 'literal') {
28+
content += child.content;
29+
}
30+
}
31+
yield text(content, element.properties);
32+
} else {
33+
return yield* next(node);
34+
}
35+
},
36+
});
37+
}

‎packages/ui/src/host.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// oxlint-disable max-params
2+
import { text } from '@bomb.sh/tty';
23
import { type Node, create, createApi, createContext, destroy, LifecycleApi } from './core.ts';
34
import {
45
getElement,
@@ -7,6 +8,7 @@ import {
78
type HostElement,
89
type HostLiteral,
910
} from './elements.ts';
11+
import { LayoutApi, layout } from './layout.ts';
1012

1113
export interface Host {
1214
root: Node;
@@ -31,6 +33,7 @@ export function createHost(): Host {
3133
};
3234

3335
setElement(root, element);
36+
useRootLayout(root, element);
3437

3538
LifecycleApi.around(root, {
3639
destroy([node], next) {
@@ -158,3 +161,28 @@ function detach(child: HostElement): void {
158161
destroy(child.node);
159162
}
160163
}
164+
165+
function useRootLayout(root: Node, element: HostElement): void {
166+
LayoutApi.around(root, {
167+
*layout([node], next) {
168+
if (node !== root) {
169+
return yield* next(node);
170+
}
171+
let content = '';
172+
for (const child of element.children) {
173+
if (child.type === 'element') {
174+
if (content !== '') {
175+
yield text(content);
176+
content = '';
177+
}
178+
yield* layout(child.node!);
179+
} else {
180+
content += child.content;
181+
}
182+
}
183+
if (content !== '') {
184+
yield text(content);
185+
}
186+
},
187+
});
188+
}

‎packages/ui/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { rgba } from '@bomb.sh/tty';
22
export { box, type BoxProps } from './components/box.ts';
33
export { text, type TextProps } from './components/text.ts';
4+
export { layout, LayoutApi } from './layout.ts';
45
export { createRoot, type RenderRoot, type RenderRootOptions } from './render/createRoot.ts';

‎packages/ui/src/layout.test.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { id } from './core.ts';
2+
import { close, open, text as ttyText } from '@bomb.sh/tty';
3+
import { describe, expect, test } from 'vitest';
4+
import { useBoxElement } from './elements/box.ts';
5+
import { useTextElement } from './elements/text.ts';
6+
import { createHost } from './host.ts';
7+
import { layout } from './layout.ts';
8+
9+
describe('layout', () => {
10+
describe('box', () => {
11+
test('lays out host children in element order', () => {
12+
const host = createHost();
13+
useBoxElement(host);
14+
15+
const parent = host.createElement('box');
16+
const first = host.createElement('box');
17+
const second = host.createElement('box');
18+
19+
host.insertBefore(parent, first);
20+
host.insertBefore(parent, second);
21+
host.insertBefore(host.element, parent);
22+
host.insertBefore(parent, second, first);
23+
24+
expect([...layout(host.root)]).toEqual([
25+
open(id(parent.node!)),
26+
open(id(second.node!)),
27+
close(),
28+
open(id(first.node!)),
29+
close(),
30+
close(),
31+
]);
32+
});
33+
34+
test('coalesces runs of text literals around child elements', () => {
35+
const host = createHost();
36+
useBoxElement(host);
37+
38+
const parent = host.createElement('box');
39+
const child = host.createElement('box');
40+
host.insertBefore(parent, host.createLiteral('Hello'));
41+
host.insertBefore(parent, host.createLiteral(', '));
42+
host.insertBefore(parent, child);
43+
host.insertBefore(parent, host.createLiteral('World'));
44+
host.insertBefore(parent, host.createLiteral('!'));
45+
host.insertBefore(host.element, parent);
46+
47+
expect([...layout(host.root)]).toEqual([
48+
open(id(parent.node!)),
49+
ttyText('Hello, '),
50+
open(id(child.node!)),
51+
close(),
52+
ttyText('World!'),
53+
close(),
54+
]);
55+
});
56+
});
57+
58+
describe('text', () => {
59+
test('coalesces literals with the enclosing properties', () => {
60+
const host = createHost();
61+
useTextElement(host);
62+
63+
const element = host.createElement('text');
64+
host.setProperty(element, 'color', 1);
65+
host.insertBefore(element, host.createLiteral('Hello'));
66+
host.insertBefore(element, host.createLiteral(', '));
67+
host.insertBefore(element, host.createLiteral('World!'));
68+
host.insertBefore(host.element, element);
69+
70+
expect([...layout(element.node!)]).toEqual([ttyText('Hello, World!', { color: 1 })]);
71+
});
72+
73+
test('coalesces text literals at the host root', () => {
74+
const host = createHost();
75+
host.insertBefore(host.element, host.createLiteral('Hello'));
76+
host.insertBefore(host.element, host.createLiteral(', world!'));
77+
78+
expect([...layout(host.root)]).toEqual([ttyText('Hello, world!')]);
79+
});
80+
});
81+
});

‎packages/ui/src/layout.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { createApi } from './core.ts';
2+
import { type Op } from '@bomb.sh/tty';
3+
4+
export const LayoutApi = createApi('layout', {
5+
layout(_): Iterable<Op> {
6+
return [];
7+
},
8+
});
9+
10+
export const { layout } = LayoutApi.methods;

0 commit comments

Comments
 (0)