This page as markdown: /code-block.md · Index for agents: /llms.txt · Everything in one file: /llms-full.txt
CodeBlock
Status: stable
Multi-line code surface. Optional header bar carries a filename and a copy affordance; the body is a horizontally scrollable monospace pre. Syntax highlighting is opt-in: pass a highlighter per block or once via CodeHighlightProvider (the package ships none).
Keywords: code block, snippet, pre, example
Usage
Use CodeBlock for multiline code samples that users may read, inspect, or copy.
Best practices
- Do: Give the snippet a filename, or a sentence of surrounding copy, when it helps readers and agents place it.
- Do: Keep examples complete enough to teach the intended pattern.
- Do: Wire highlighting once with CodeHighlightProvider and map token colors to semantic tone tokens (keywords → tone-1, strings → success, comments → muted-foreground), so code follows the theme instead of a fixed palette.
- Don't: Do not put short inline identifiers in a CodeBlock; use Code for those.
- Don't: Do not paste code with the indentation the surrounding file had. A block indented to sit inside your JSX shows the reader leading whitespace that is not part of the example.
Anatomy
| Part | Required | Description |
|---|---|---|
| CodeBlock | yes | The surface: optional filename header, copy affordance, scrollable pre. |
| CodeHighlightProvider | no | Optional app-level wrapper that supplies one highlighter to every CodeBlock inside it. |
Subcomponents
| Name | Type | Description |
|---|---|---|
| CodeHighlightProvider | { highlight: (code: string) => ReactNode; children: ReactNode } | Supplies one highlighter to every CodeBlock beneath it, so an app wires highlighting once. Map the highlighter's token colors to Sable tone tokens so code follows the theme. |
Props
| Name | Type | Default | Description |
|---|---|---|---|
| code | string | — | The code to display. Always what the copy button copies, whatever the highlighter renders. |
| filename | string | — | Optional filename shown in the header bar. |
| showCopy | boolean | true | Show the copy affordance — in the header bar with a filename, floating in the corner without one. |
| highlight | ((code: string) => ReactNode) | false | — | Highlighter for this block; overrides CodeHighlightProvider. false renders plain text inside a provider. |
| prompt | string | — | A shell prompt ("quot;, ">"). Makes it a one-line command: the prompt is dimmed and unselectable, the line truncates, and copy takes only code. |
| wrap | boolean | false | Wrap long lines instead of scrolling them. For prose in a code face (a prompt to paste into an agent, a log line); code keeps scrolling, since wrapping breaks its indentation. |
| copyLabel | string | "Copy code" ("Copy command" with prompt) | Accessible name of the copy button. Its 32px box keeps a 44px touch target under a coarse pointer. |
| copiedLabel | string | "Copied" | Announced through a polite live region once the code is copied. |
Tokens used
border, muted, muted-foreground, foreground
Motion
- arrival · Root · no motion — Code is reading content and arrives with the page or message it belongs to. An entrance of its own would delay the text someone came to copy.
- response · Copy button ·
motion-interactive— The copy button is a quiet ghost icon in the corner of a block. Its hover fill is what tells the pointer it is a control and not decoration. - transition · Copy glyph ·
motion-icon-swap— The copy mark turning into a check is the only proof the clipboard took the text. Snapped between frames it was easy to miss at the edge of a block; the swap reads as one mark being replaced. - activity · Root · no motion — Copying is instant. There is no in-between worth showing, and a spinner for a clipboard write would read as a slow one.
Accessibility
- Code renders inside a
for correct semantics and screen-reader handling. - Copy is the ghost icon Button with
useCopy, which guards clipboard access for SSR safety. - A successful copy is announced as "Copied" through a polite role=status region that is always mounted; the button keeps its label, which names what it does. Both glyphs are aria-hidden.
Related
- Components: Code
Examples
With filename + copy
import { CodeBlock } from "@sableui/react"
<CodeBlock filename="cn.ts" code={"import { clsx } from \"clsx\"\nexport const cn = clsx"} />
With a highlighter for the whole app
import { CodeBlock, CodeHighlightProvider } from "@sableui/react"
<CodeHighlightProvider highlight={(code) => <span dangerouslySetInnerHTML={{ __html: highlightToHtml(code) }} />}><CodeBlock code={"const a = 1"} /></CodeHighlightProvider>
No header
import { CodeBlock } from "@sableui/react"
<CodeBlock showCopy={false} code={"type Size = \"sm\" | \"md\"\nconst size: Size = \"md\""} />