Skip to content
Browse docs and components

Code Block

code-block

Code an agent wrote or is writing: highlighted by a small built-in tokenizer in the theme's syntax colours, with line numbers, marked and diff lines, wrapping, a header with a title and actions, a small size for tool output, copy, and a view that follows streamed code until you scroll away.

Responsive

Install

$npxshadcnadd@forme/code-block

Adds the component, its registry dependencies, and the token stylesheet you import once.

Usage

import { CodeBlock } from "@/components/forme/code-block/code-block";

<CodeBlock filename="server.ts" language="ts" code={source} lineNumbers />

When to use

Reach for it

  • Code an assistant wrote or is writing, and code samples in docs.
  • Streaming output with streaming, which keeps the newest line in view.

Use something else

  • Changes to an existing file — show those as a FileDiff.
  • Inline code inside a sentence; use a <code> element.

Examples

4 more ways to put it to work. Install one by name and it lands beside the component, or copy it from the Code tab into your app.

Wrapped lines

wrap: a long command with flags reads whole instead of scrolling sideways.

$npxshadcnadd@forme/code-block-wrapped
Responsive

Explained line by line

Notes beside the code; choosing one marks the lines it explains with highlightLines.

$npxshadcnadd@forme/code-block-explained
Responsive

Diff lines

diff reads the + and - markers into the gutter and tints added and removed lines.

$npxshadcnadd@forme/code-block-diff
Responsive

Terminal output

A title and icon instead of a file name, and a Run again action beside copy.

$npxshadcnadd@forme/code-block-terminal
Responsive

API reference

Read from the source, so it is the API of the file you install. A prop marked * is required.

CodeTokens

A line's tokens, coloured from the --syntax-* tokens.

PropTypeDefaultDescription
tokens*readonly Token[]—

CodeBlock

Code an agent wrote, or is writing.

PropTypeDefaultDescription
code*string—
language'html' | 'text' | 'ts' | 'tsx' | 'js' | 'jsx' | 'json' | 'css' | 'bash'"text"
filenameReactNode—Shown in the header: a path, a file name.
titleReactNode—The header's title when it is not a file: "Output", "Terminal". Overrides filename.
iconReactNode—Replaces the file icon in the header: a terminal, a log.
actionsReactNode—Extra buttons in the header, before copy.
headerbooleantruefalse drops the header, for output that is already labelled, like a tool's in a Trace.
size"default" | "small""default"small is for code inside something else: a Trace step, a tool's output.
diffbooleanfalseReads each line's first character as a diff marker: + added, - removed, anything else context. The marker leaves the code and takes the gutter.
streamingbooleanfalseWhile true the code is still arriving: the view follows it and a caret marks the end.
lineNumbersbooleantrue
highlightLinesreadonly number[][]One-based line numbers to mark.
maxHeightnumber320The tallest the code gets before it scrolls, in pixels.
wrapbooleanfalseWrap long lines instead of scrolling sideways.
copyablebooleantrue
classNamestring—

Accessibility

  • Line numbers sit in their own column and are never selected or copied with the code.

Motion

  • While streaming, the view follows the newest line until the reader scrolls up, and a caret blinks at the end.