Repository navigation
Add a basic DOM styling example #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clack/ui': patch | ||
| --- | ||
|
|
||
| Export Document, Element, and Text for applications that build terminal UI trees directly. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,4 @@ jobs: | |
| secrets: inherit | ||
| with: | ||
| publish: './packages/*' | ||
| template: './examples/*' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Basic example | ||
|
|
||
| A DOM version of the original basic panel, using `div`, `span`, text nodes, and | ||
| direct `element.style` assignments. | ||
|
|
||
| From the repository root: | ||
|
|
||
| ```sh | ||
| pnpm install | ||
| pnpm build | ||
| pnpm --filter @examples/basic start | ||
| ``` | ||
|
|
||
| The panel appears in the terminal's alternate screen while the example runs. | ||
| Press Enter or Ctrl+C to finish and restore the previous screen: | ||
|
|
||
| ```text | ||
| ┌──────────────────────┐ | ||
| │ @clack/ui │ | ||
| │ │ | ||
| │ status ● ready │ | ||
| │ version 0.0.0 │ | ||
| │ render @bomb.sh/tty │ | ||
| └──────────────────────┘ | ||
| ``` | ||
|
|
||
| The title is cyan, labels are gray, status is green, remaining values inherit | ||
| white from the panel, and borders are blue. Each label and value is a separate | ||
| `span` in a flex row; this does not need mixed-color runs inside one text flow. | ||
|
|
||
| Borders occupy their own cells in the CSS box model, so the panel has one cell | ||
| of horizontal padding and zero vertical padding. Together with its one-cell | ||
| border, this preserves the original example's 24-column, 7-row appearance. | ||
|
|
||
| The tests render the same `createApp()` tree through the UI source, checking | ||
| layout and colors. Run them with: | ||
|
|
||
| ```sh | ||
| pnpm exec bsh test examples/basic/src/app.test.ts | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "name": "@examples/basic", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "start": "node --experimental-strip-types src/index.ts", | ||
| "lint": "bsh lint" | ||
| }, | ||
| "dependencies": { | ||
| "@clack/ui": "workspace:*" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createTestUI } from '../../../packages/ui/test/ui.ts'; | ||
| import { createApp } from './app.ts'; | ||
|
|
||
| const panel = [ | ||
| '┌──────────────────────┐', | ||
| '│ @clack/ui │', | ||
| '│ │', | ||
| '│ status ● ready │', | ||
| '│ version 0.0.0 │', | ||
| '│ render @bomb.sh/tty │', | ||
| '└──────────────────────┘', | ||
| ]; | ||
|
|
||
| describe('basic example', () => { | ||
| it('renders the original panel with styled labels and inherited value colors', async () => { | ||
| await using ui = await createTestUI({ cols: 32, rows: 9 }); | ||
| const { document, terminal } = ui; | ||
| document.documentElement!.appendChild(createApp(document)); | ||
|
|
||
| await expect | ||
| .poll(() => terminal.screen.getLines().map((line) => line.trimEnd()), { timeout: 2000 }) | ||
| .toEqual([...panel, '', '']); | ||
|
|
||
| const runs = [ | ||
| { row: 1, col: 2, content: '@clack/ui', color: { r: 0, g: 255, b: 255 } }, | ||
| { row: 3, col: 2, content: 'status ', color: { r: 128, g: 128, b: 128 } }, | ||
| { row: 3, col: 10, content: '● ready', color: { r: 0, g: 128, b: 0 } }, | ||
| { row: 4, col: 2, content: 'version', color: { r: 128, g: 128, b: 128 } }, | ||
| { row: 4, col: 10, content: '0.0.0', color: { r: 255, g: 255, b: 255 } }, | ||
| { row: 5, col: 2, content: 'render ', color: { r: 128, g: 128, b: 128 } }, | ||
| { row: 5, col: 10, content: '@bomb.sh/tty', color: { r: 255, g: 255, b: 255 } }, | ||
| ]; | ||
| for (const run of runs) { | ||
| for (const [offset, char] of [...run.content].entries()) { | ||
| const cell = terminal.getCell(run.row, run.col + offset); | ||
| expect(cell.char).toBe(char); | ||
| expect(cell.fg).toMatchObject(run.color); | ||
| } | ||
| } | ||
|
|
||
| for (const [row, line] of panel.entries()) { | ||
| for (const [col, char] of [...line].entries()) { | ||
| if ('┌┐└┘─│'.includes(char)) { | ||
| expect(terminal.getCell(row, col).fg).toMatchObject({ r: 0, g: 0, b: 255 }); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import type { Document, Element, ColorValue } from '@clack/ui'; | ||
|
|
||
| /* oxlint-disable bombshell-dev/exported-function-async -- Builds a detached DOM tree synchronously. */ | ||
| /** Build the basic panel using elements, text nodes, and inline declarations. */ | ||
| export function createApp(document: Document): Element { | ||
| const panel = document.createElement('div'); | ||
| panel.style.flexDirection = 'column'; | ||
| panel.style.gap = '1px'; | ||
| panel.style.paddingLeft = '1px'; | ||
| panel.style.paddingRight = '1px'; | ||
| panel.style.color = 'white'; | ||
| panel.style.borderStyle = 'solid'; | ||
| panel.style.borderColor = 'blue'; | ||
| panel.style.borderTopWidth = '1px'; | ||
| panel.style.borderRightWidth = '1px'; | ||
| panel.style.borderBottomWidth = '1px'; | ||
| panel.style.borderLeftWidth = '1px'; | ||
| panel.appendChild(span('@clack/ui', 'cyan')); | ||
|
|
||
| const details = document.createElement('div'); | ||
| details.style.flexDirection = 'column'; | ||
| details.appendChild(row('status ', span('● ready', 'green'))); | ||
| details.appendChild(row('version', span('0.0.0'))); | ||
| details.appendChild(row('render ', span('@bomb.sh/tty'))); | ||
| panel.appendChild(details); | ||
|
|
||
| return panel; | ||
|
|
||
| function span(content: string, color?: ColorValue): Element { | ||
| const element = document.createElement('span'); | ||
| if (color !== undefined) { | ||
| element.style.color = color; | ||
| } | ||
| element.appendChild(document.createTextNode(content)); | ||
| return element; | ||
| } | ||
|
|
||
| function row(label: string, value: Element): Element { | ||
| const element = document.createElement('div'); | ||
| element.style.gap = '1px'; | ||
| element.appendChild(span(label, 'gray')); | ||
| element.appendChild(value); | ||
| return element; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { createUI } from '@clack/ui'; | ||
| import { createApp } from './app.ts'; | ||
|
|
||
| const controller = new AbortController(); | ||
| const ui = await createUI({ | ||
| height: 7, | ||
| input: process.stdin, | ||
| output: process.stdout, | ||
| signal: controller.signal, | ||
| }); | ||
|
|
||
| const stop = (): void => controller.abort(); | ||
|
|
||
| try { | ||
| ui.document.documentElement!.appendChild(createApp(ui.document)); | ||
| process.stdin.once('data', stop); | ||
| process.stdin.once('end', stop); | ||
| process.once('SIGINT', stop); | ||
| process.stdin.resume(); | ||
| await ui.main(); | ||
| } finally { | ||
| controller.abort(); | ||
| process.stdin.off('data', stop); | ||
| process.stdin.off('end', stop); | ||
| process.off('SIGINT', stop); | ||
| process.stdin.pause(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "compilerOptions": { | ||
| "paths": { | ||
| "@clack/ui": ["../../packages/ui/src/index.ts"] | ||
| }, | ||
| "composite": false, | ||
| "declaration": false, | ||
| "declarationMap": false, | ||
| "noEmit": true, | ||
| "types": [ | ||
| "node" | ||
| ] | ||
| }, | ||
| "include": ["src"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.