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

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

Accessibility

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\""} />