CodeBlock

Highlighted code with a file name, a copy button, and language tabs.

Source

A file

The header shows the file name, language, and copy action.

analytics.tsTypeScript
import posthog from 'posthog-js'posthog.capture('signup_completed', {    plan: 'team',    source: 'pricing',})

Multiple files

One tab per file. Set defaultValue to choose the first selected file.

import posthog from 'posthog-js'export function captureExport(reportId: string) {    posthog.capture('report_exported', {        report_id: reportId,        format: 'csv',    })}

Languages

Use arrow keys to change tabs. Tabs scroll when the container is narrow.

posthog.capture('report_exported', {    format: 'csv',})

Highlights and changes

Use highlights to point at the lines that the text talks about, and added or removed marks to show a change. Line numbers can start at any value.

feature-flags.tsTypeScript
const flag = 'new-reports'Removed: const enabled = falseAdded: const enabled = await isFeatureOn(Added:     flagAdded: )renderReports({ enabled })

Long blocks and long lines

Show more expands tall code. Focus the code region to scroll with the keyboard.

WITH signups AS (    SELECT        person_id,        min(timestamp) AS signup_time    FROM events    WHERE event = 'signup_completed'    GROUP BY person_id)SELECT    toStartOfWeek(signup_time) AS week,    count(DISTINCT signups.person_id) AS activated_usersFROM signupsJOIN events ON events.person_id = signups.person_idWHERE events.event = 'report_exported'    AND events.timestamp BETWEEN signup_time AND signup_time + INTERVAL 7 DAYGROUP BY weekORDER BY week DESC

Usage

These are defaults, not laws. Break one when you have a good reason, or when it makes the design more cohesive or more interesting.

  • Use CodeBlock for multi-line code and file tabs. For a command to copy, use Snippet. For code in a sentence, use Snippet.Inline.
  • Give each snippet a stable, unique id.
  • Set maxHeight for long files. The Show more control appears only when the code is taller.

API

CodeBlock is a site component. It accepts div attributes and a div ref. Import CodeSnippet and CodeBlockProps as types.

CodeBlock

PropTypeDefaultDescription
snippetsreadonly CodeSnippet[]–The code files or language options.
value / defaultValuestringFirst snippet idThe controlled or initial active snippet id.
onValueChange(id: string) => void–Runs when the user selects a snippet. Returns its id, not the old LanguageOption object.
lineNumbersbooleanfalseShows the line-number gutter.
lineNumberStartnumber1Sets the first displayed number. It does not change line metadata positions.
maxHeightnumber–Limits the collapsed code height in pixels. Show more reveals the complete snippet. Omit it to show all code.
showCopybooleantrueShows the library CopyButton.
onCopy() => void–Runs after the clipboard write succeeds.
classNamestring–Merges classes with the solid editor surface.
refRef<HTMLDivElement>–References the outer div.

CodeSnippet

PropTypeDefaultDescription
idstring–A unique, stable id for selection and expansion state.
codestring–The complete source text. Whitespace remains unchanged.
languagestringPlain textA Prism language or an alias from the existing language map.
fileName / labelstring–Sets the header or tab text. The file name takes priority.
highlightedLinesreadonly number[]–One-based positions of highlighted lines. Each line has a tinted fill and a one-pixel marker.
addedLines / removedLinesreadonly number[]–One-based positions of diff lines. These fields do not alter the code.