Code block
Source with light highlighting, a language label and a copy button.
Demo
Usage
import { CodeBlock, Message } from '@tuttn/slant'
// The language labels the bar and picks the highlighting. Copy is on by default.
<CodeBlock code={source} language="tsx" />
// A file name in the bar instead, numbered lines, and a height to scroll from.
<CodeBlock code={view} language="swift" title="ContentView.swift" lineNumbers maxHeight={320} />
// A snippet numbered from its place in the file: 120, 121, …
<CodeBlock code={snippet} language="ts" title="server.ts" lineNumbers startLine={120} />
// Long lines wrap instead of scrolling sideways. With no language, title,
// header, copy button or wrap toggle there is no bar at all.
<CodeBlock code={log} wrap copyable={false} />
// A toggle in the bar lets the reader wrap the lines, uncontrolled
// (defaultWrap) or controlled (wrap and onWrapChange).
<CodeBlock code={log} wrapToggle />
<CodeBlock code={log} wrapToggle wrap={wrapped} onWrapChange={setWrapped} />
// Controls in the bar before the title, such as tabs between two versions.
// preProps reach the <pre>, so it can be their tab panel.
<CodeBlock code={source} language="tsx" header={tabs}
preProps={{ id: 'source-panel', role: 'tabpanel', 'aria-labelledby': 'source-react' }} />
// Inside an answer: a Message is a div, so it holds the block.
<Message author="assistant">
Add the package first:
<CodeBlock code="npm i @tuttn/slant" language="bash" onCopied={() => track('install copied')} />
</Message>Props
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | — | The source. Trailing whitespace is trimmed. |
language | string | — | The language, shown in the bar unless `title` replaces it, and used to pick the highlighting. ts, tsx, js, jsx, swift, css, json, html, bash and sh are highlighted; anything else is plain. |
title | ReactNode | — | Replaces the language in the bar: a file name, say. |
header | ReactNode | — | Controls in the bar before the title, such as tabs between two versions of the source. |
copyable | boolean | true | Shows the copy button. |
highlight | boolean | true | Lightly highlights comments, strings, keywords, numbers and attributes. |
lineNumbers | boolean | false | Numbers the lines. |
startLine | number | 1 | The first line's number, with `lineNumbers`: 120 for a snippet from line 120 of a file. The number column fits the widest number. |
wrap | boolean | — | Wraps long lines instead of scrolling sideways. Pass it to control wrapping; leave it out to let the block keep its own, from `defaultWrap`. |
defaultWrap | boolean | false | Whether an uncontrolled block starts wrapped. |
onWrapChange | (wrap: boolean) => void | — | Called with the next state when the wrap toggle is pressed. |
wrapToggle | boolean | false | Shows a toggle button in the bar that wraps and unwraps the lines, named `messages.codeWrap`. |
maxHeight | number | — | The height in pixels at which the code starts to scroll. |
copyLabel | string | messages.copyCode | The copy button's accessible name. It should contain the button's visible text, `messages.copy` ("Copy"). While the button says "Copied", its name is `messages.copied`, so the name always holds the text on show. |
onCopied | (code: string) => void | — | Called with the source after it is copied. |
preProps | HTMLAttributes<HTMLPreElement> | — | Attributes for the `<pre>`: an id, and a role and aria-labelledby when it is a tab panel. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, the ref and other attributes reach the root <div>; preProps reach the <pre>. |
Keyboard
| Tab | moves through the controls in the bar (the header’s, the wrap toggle, then Copy), then to the code |
| Enter / Space | on Copy, copies the source; on the wrap toggle, wraps or unwraps the lines |
| ← → ↑ ↓ | on the code, scrolls it when it overflows |
| PageUp / PageDown / Home / End | on the code, scrolls it a screen at a time, or to either end |
Accessibility
- The copy button reads “Copy” and is named by
copyLabel(messages.copyCode, “Copy code”); keep the visible word in any label you pass. After a copy it reads and is named “Copied” (messages.copied) for 1.6 seconds, keeping focus, and the root’s polite announcer says it too. If the clipboard refuses, nothing changes and nothing is announced. - Copy writes the source, trailing whitespace trimmed, never the line numbers. The numbers are
aria-hiddenand left out of a selection, so copying the code by hand leaves them out too. - The
<pre>is in the tab order (tabIndex0), so a keyboard can scroll code that overflows; its focus ring is drawn inside the frame. - Highlighting is colour only, on spans marked
data-token, and every token colour holds 4.5:1 against the block’s background in every theme. A screen reader reads the code as plain text. - The wrap toggle is an icon button named
messages.codeWrap(“Wrap lines”) whosearia-pressedsays whether the lines wrap; pressing it keeps focus on it.
Highlighting
A small tokenizer, with no dependencies, marks comments, strings, keywords, attributes and numbers, with keywords for each language: TypeScript and JavaScript (ts, tsx, js, jsx) and swift, then lighter rules for css, json, html and the shell (bash, sh: the command, its flags and variables). Any other language, or highlight={false}, shows plain text. It gives the same markup on the server and in the browser, so a prerendered page hydrates cleanly.
Line numbers
lineNumbers numbers the lines from 1, or from startLine for a snippet cut from a longer file, so the numbers match the file. The number column is as wide as its widest number, the numbers end-aligned, so a snippet crossing from 9 to 10 keeps its code in one straight column. Without lineNumbers, startLine does nothing.
Wrapping
wrap breaks long lines instead of scrolling sideways; numbered, a wrapped line keeps its number at the top. It works like any Slant state: pass wrap to control it, with onWrapChange to hear the toggle, or defaultWrap to let the block keep its own. wrapToggle puts the toggle in the bar, before Copy, for the reader to choose.
Direction
Code reads left to right in every language, so the <pre> is always dir="ltr", line numbers on the left. The bar follows the page: under dir="rtl" the title starts on the right and Copy sits on the left.