Terminal
A compact code or command panel with keyboard scrolling and copy feedback.
Demo
Usage
import { Terminal } from '@tuttn/slant'
<Terminal title="Working tree" code={'git status --short\ngit diff --stat'} />
// Output with ANSI colour, its exit code, and long output folded to its
// first and last lines (20 of each with fold={true}).
<Terminal title="npm test" code={output} exitCode={1} fold={{ head: 10, tail: 10 }} />
// Control the fold, or start it open with defaultExpanded.
const [open, setOpen] = useState(false)
<Terminal title="Build output" code={log} fold expanded={open} onExpandedChange={setOpen} />
// A read-only log without a copy action; escapes shown as raw text.
<Terminal title="Build output" code={buildLog} copy={false} ansi={false} />
// Limit the viewport for longer output; scrolling stays native.
<Terminal title="notes.json" code={json} className="source-preview" />
.source-preview { --sl-terminal-max-height: 16rem; }Props
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | — | The source text. With `ansi` on, its SGR escapes colour it and every other escape is dropped; with `ansi={false}` it is shown and copied exactly as supplied. |
title | string | messages.terminal | The visible filename or command label. |
copy | boolean | true | Offers a button to copy the source. |
ansi | boolean | true | Reads ANSI escapes in `code`. SGR sequences (`ESC[…m`) set the eight colours (30–37, 90–97), their backgrounds (40–47, 100–107), bold, dim, italic, underline and inverse; 256-colour and truecolor take the nearest of the eight. Other escapes (cursor moves, OSC titles and links) are removed, and Copy writes the text without any escapes. |
exitCode | number | — | The command's exit code, as a badge in the header: success for 0, danger for anything else. |
fold | boolean | { /** * Lines kept above the fold. * @default 20 */ head?: number /** * Lines kept below the fold. * @default 20 */ tail?: number } | false | Folds long output: more than `head` + `tail` + 1 lines shows the first `head` and the last `tail`, with a button between them that shows the rest. `true` keeps 20 of each. Copy always writes all of it. |
expanded | boolean | — | Whether folded output is open. Pass it to control the fold; leave it out to let the terminal keep its own. |
defaultExpanded | boolean | false | Whether folded output starts open, uncontrolled. |
onExpandedChange | (expanded: boolean) => void | — | Called with the next state when the fold button opens or closes the output. |
…rest | Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'title'> | — | className, style, other attributes and the ref go on the outer div. |
Keyboard
| Tab | moves to Copy, then to the output when it scrolls, then to the fold button |
| Enter / Space | on Copy, copies the text; on the fold button, shows the hidden lines or folds them again |
| ← → ↑ ↓ | on the output, scrolls it when it overflows |
Accessibility
- The code viewport is named by the title and joins the tab order only when scrolling is needed.
- Copy has a visible label; a polite status region announces success or a useful failure message.
- Colour is never the only signal: the text reads the same without it. Every ANSI colour is a token that holds 4.5:1 against the terminal in every theme, Paper included, and a coloured background always carries body text.
- The exit code is text in its badge (“Exit code 1”), not only a colour.
- The fold button is a real button with
aria-expanded, named by its text:messages.terminalShowLines(n)while folded,messages.terminalFoldLineswhile open. It is left out of a selection, so copying the output by hand skips it.
Source as text
Terminal shows a command, configuration file or log with Slant's monospace type and a small pixel in the header. code is plain text: indentation and line breaks are preserved, HTML is escaped, and nothing is executed. Output reads left to right under dir="rtl" too, while the header follows the page. Compose multiple snippets with Segmented or Tabs.
ANSI colour
Output from a test runner or a build tool keeps its colour. SGR sequences (ESC[…m) set the eight colours (30–37 and the bright 90–97), their backgrounds (40–47, 100–107), bold, dim, italic, underline and inverse, and their resets (0, 22, 23, 24, 27, 39, 49). Each colour is a theme token, so the output follows the theme: black and white are the muted and body text, red, green and yellow the danger, success and warning colours, blue the accent, and magenta and cyan chart colours mixed toward the text. The bright colours take the same tokens. 256-colour (38;5;n) and truecolor (38;2;r;g;b) take the nearest of the eight: a grey becomes black or white by its lightness, any other colour the nearest hue. A background is the colour's soft tint with body text on it, whatever the foreground; inverse draws the page background on the foreground colour; dim is the muted text on a run with no colour. Every other escape (cursor moves, line erases, OSC titles and links, charset switches) is removed. Source without escapes renders as it always has, and ansi={false} shows the escapes as raw text.
Exit code
exitCode adds a monospace badge to the header, “Exit code 0” in the success tone for a clean exit and in the danger tone for anything else (messages.terminalExitCode).
Folding
fold keeps long output short: with more than head + tail + 1 lines (20 and 20 by default; a final line break doesn't count), the first head and last tail lines show around a button that reads “Show 12 hidden lines”. Pressed, the lines appear below it and it reads “Fold the output”. The button stays where it is, so focus and the pointer stay on it, and it keeps to the start of the viewport while the output scrolls sideways. expanded, defaultExpanded and onExpandedChange control it.
Copying
The copy button writes the whole text, folded lines included and escapes removed (with ansi={false}, exactly the supplied string), using the browser clipboard API. Success is reported only after the write resolves. If permission is denied or the API is unavailable, an inline message asks the reader to select and copy the code manually. Set copy={false} to remove the action.
Scrolling and keyboard access
Long lines scroll horizontally instead of widening the page. The viewport becomes keyboard-focusable only when its content overflows. --sl-terminal-max-height limits long output to 20rem by default; the viewport uses Slant's ScrollArea.