Data Display

CodeBlock

Block container for multi-line source code with optional syntax highlighting and a copy-to-clipboard control.

import { CodeBlock } from '@lostgradient/cinder';
codesnippet
01

Overview

Reference usage

Renders preformatted source code with optional client-side syntax highlighting and copy controls. The first paint may be the escaped plain-code fallback; when highlighting resolves, the block keeps the same layout metrics.

CodeBlock imports no highlighter of its own, so a consumer bundle carries only the grammars it chooses. A block highlights with its highlighter prop, else with the highlighter an ancestor publishes through setCodeHighlighterContext, and renders plain code with neither. Build a highlighter with shikiHighlighter over the grammars your application renders, or use fullShikiHighlighter for every grammar Shiki ships.

Usage

svelte
<script lang="ts">
  import { CodeBlock } from '@lostgradient/cinder';
  const code = `function greet(name: string): string {
  return \`Hello, \${name}!\`;
}

const message = greet('world');`;
</script>

<CodeBlock {code} />

Line numbers and notes

lineNumbers numbers each line in a gutter on the left. The numbers are generated content: a selection copies the code without them, and they are kept out of the accessibility tree. A wrapped line (wrap) is numbered once, and the gutter is as wide as the last number.

annotations draws a note under a line, for a file problem or a warning: the line's row takes the tone's tint, its number turns to the tone's color, and the note sits directly beneath with a glyph.

svelte
<CodeBlock
  {code}
  language="json"
  lineNumbers
  annotations={[{ line: 11, message: 'Missing comma at the end of this line.' }]}
/>

Plain code is split into lines by the block itself. Highlighted code is decorated after it renders, which needs a highlighter that wraps each line in an element with the class line, as Shiki does; a highlighter that returns a single <code> element cannot be numbered, so the block shows the plain, numbered rendering instead.

Scrolling and wrapping

A code block scrolls inside its own box, with a scrollbar that is always painted from the shared scrollbar tokens. wrap breaks a long line instead, and hangs the rows after the first by two characters so each line of code reads as one unit. A wrapping block breaks a line at whitespace, and a long URL or path at a slash, dot, colon, hyphen or closing quote (a <wbr> after each, which adds no character), never inside a word unless one part is wider than the line. A flag such as --notify never breaks after its dashes. The copied text is the code as written.

Copy

copyable adds an icon-only copy button. With a visible language label, the label and the button share a row above the code. With no visible label, the button floats over the code's top inline-end corner and the block has no header row at all; copyFloating chooses either layout outright. The button is named "Copy code", or for the language ("Copy SQL code", an acronym in its canonical case); copyLabel names it for the page ("Copy the Claude Code command"). A floating button reserves a rail at the inline end: a block that scrolls stops its scrollport before the rail on every row, and a block that wraps reserves the rail beside its first row only, so every further row keeps the whole width.

Live preview

Basic code block

function greet(name: string): string {
  return `Hello, ${name}!`;
}

const message = greet('world');

02

When to use

Use when
  • Displaying a multi-line code sample or terminal transcript inside documentation or chat.
  • Letting the reader copy a snippet to the clipboard via the copyable prop.
Avoid when
  • Annotating a single inline keystroke or shortcut — use kbd instead.
  • Rendering rich prose that happens to include code — embed it in markdown instead.
03

Examples

Basic code block

Multi-line preformatted code.

Delayed highlighting

Colorization can resolve after first paint while the code block keeps its dimensions.

JSON payload

A highlighted JSON payload draws keys in the accent blue, strings in green and numbers in amber, so a value reads as its own class of token.

Line numbers and notes

Read-only code with line numbers and a note under the line a file problem points at. The numbers and the note are not part of the copied text.

With language label and copy

Header shows the language and a copy-to-clipboard button. The block highlights with the highlighter an ancestor publishes through setCodeHighlighterContext — here, the playground publishes one — and renders plain code when none is published.

Scrolling and wrapping

By default a long line scrolls inside the block with a visible scrollbar. wrap breaks it instead, and a wrapped line keeps one number.

04

Props

Props for code-block
Name
Type
Default
Description
code required text The code to render.
language text Optional language label rendered above the code (there is no header bar); also selects the grammar for highlighting.
highlight boolean Whether to highlight. Defaults to true whenever language is set and a highlighter resolves from the highlighter prop or from context. highlight={false} is an absolute off switch: it disables ALL highlighting — including an explicit highlighter prop and a context highlighter. The block renders the escaped plain <pre><code> fallback while keeping the language label. When highlighting is enabled, client-side colorization may appear after first paint, but the block keeps stable layout metrics.
highlighter Highlighter Highlighter for this instance. Receives (code, language) and returns an HTML string (sync or async). Takes precedence over a highlighter an ancestor published through setCodeHighlighterContext; with neither, the block renders unhighlighted.
copyable boolean false When true, render an icon-only copy button above the code. The block has no header bar or frame.
copyFloating boolean Float the copy button (and the language label, when shown) over the top inline-end corner of the code instead of giving them a row above it. Unset, the copy button floats whenever no language label is visible, so a block with only Copy has no header row; with a visible language label it shares a row with it. Pass true or false to choose either way.
copyLabel text The accessible name of the copy button, also its tooltip. Defaults to "Copy code", or "Copy SQL code" when language is set, an acronym such as JSON or SQL in its canonical spelling. Say what is copied when the page has more than one block ("Copy the Claude Code command").
languageLabelVisible boolean true Whether to render the language label above the code. Default true. Pass false to keep language-driven highlighting without the visible label; with copyable also unset, nothing renders above the code.
streaming boolean false The code is still arriving, as in a streamed reply with an open fence. A caret follows the final character, streamingLabel shows where the copy button will be, the copy button is withheld, and the bottom edge is dashed. Highlighting waits until it is false. Defaults to false.
streamingLabel text 'Writing…' The status shown while streaming. Defaults to 'Writing…'.
scrollFadeVisible boolean false Adds the shared scroll-driven fade classes to the horizontal (inline) edges of the code viewport. Every block that overflows already ends in a cue at its end edge, whatever this is: the block reports (data-cinder-overflows-inline-end) that code runs past that edge, so no line is cut mid-glyph without a sign that it continues. Presentation-only. Defaults to false.
lineNumbers boolean false Number each line of the code. The numbers are drawn in the left gutter, kept out of the copied text and out of the accessibility tree, and a wrapped line is numbered once. Defaults to false.
annotations CodeBlockAnnotation[] Notes drawn under specific lines, such as the line a file problem points at. Works with highlighted and plain code alike. Pass a new array to change them.
wrap boolean false Wrap a line longer than the block instead of scrolling it sideways. Defaults to false, which keeps the block a fixed-width box that scrolls inside itself with a visible scrollbar.