Skip to content

Content

CodeBlock

Highlighted code with a copy button. Syntax colours come from code-* tokens only.

  • Experimental
  • Keyboard not tested yet
  • Screen reader not tested yet

Updated 2026-11-12

Playground

Change the props. The controls come from the documented props.
tsx
<Button variant="primary">Request a project</Button>

Usage

When to use

Use a code block for any code people should read or copy: imports, examples, terminal commands, configuration.

When not to use

  • A single token or class name in running text: use inline code.
  • A screenshot of code: people can't copy it and screen readers can't read it.

How it works

Code is highlighted at build time; every syntax colour comes from a code-* token and is checked against the card surface. The header shows the language or a file name and a Copy code button. With collapsible, long code starts folded behind “Show full code”. Wide lines scroll inside the block, and the block is focusable so it scrolls with the keyboard.

Content guidelines

Show code that works when pasted as it is: complete imports, real prop names, no ellipses in the middle. Use the title for the file name when the file matters.

Do and don’t

Do
  • Show code that works when pasted as is.
Don’t
  • Don't put screenshots of code in place of a CodeBlock.

Anatomy

The parts of the component and what each one does, as in the playground above.
  1. titleFile name or language in mono.
  2. copy-buttonCopies the full code.
  3. codeScrollable, focusable region; highlighting at build time.

Examples

Type-checked, and the same code goes into the AI manifest. Import: import { CodeBlock } from "@/ds";

TSX

tsx
<Button variant="primary">Request a project</Button>

Props

Checked against the component's TypeScript type on every build.
CodeBlock props
NameTypeDefaultDescription
code (required)string–Source text.
lang"tsx" | "ts" | "bash" | "json" | "css" | "md""tsx"Language for highlighting.
titlestring–Header text, e.g. a file name. Defaults to the language.
collapsiblebooleanfalseCollapses long code behind “Show full code”.

Tokens used

Derived from the utility classes in the component's source. Copy a class with one click.
Tokens used by CodeBlock
TokenTierValueUtility
code.bgcomponent#FFFFFF
code.bordercomponentrgba(10,10,11,0.08)
code.collapsedHeightcomponent320px
code.fgcomponent#0A0A0B
code.paddingcomponent16px
code.radiuscomponent12px
focussemantic#C6492E
spacing.1semantic4px
spacing.2semantic8px
spacing.4semantic16px
surface.pagesemantic#FAFAF7
text.secondarysemantic#555555
type.small.sizesemantic14px

Accessibility

What is checked automatically on every build, and what a person has tested. Nothing is marked as tested unless it was.
Testing status by check
CheckHowStatus
axe-core rulesAutomated on every npm run check, at desktop and mobile widthRuns on every build
KeyboardAutomated Playwright test (scripts/keyboard.mjs)Not tested yet
Screen readerManualNot tested yet

Keyboard

CodeBlock keyboard interactions
KeyAction
TabMoves to the copy button, then into the code region.
Arrow keysScroll the focused code region.

Semantics and focus

  • The code region is focusable and named, so it scrolls with the keyboard.
  • Every syntax colour on surface.card is in the contrast gate.

Contrast

CodeBlock contrast pairings
PairingRatio (x:1)MinimumStatus
text.primary on surface.page18.924.5Pass
text.primary on surface.card19.794.5Pass
text.secondary on surface.page7.134.5Pass
text.secondary on surface.card7.464.5Pass
focus on surface.page4.573.0Pass
focus on surface.card4.783.0Pass