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
Playground
Change the props. The controls come from the documented props.
<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.
- titleFile name or language in mono.
- copy-buttonCopies the full code.
- 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
<Button variant="primary">Request a project</Button>Props
Checked against the component's TypeScript type on every build.
| Name | Type | Default | Description |
|---|---|---|---|
| code (required) | string | – | Source text. |
| lang | "tsx" | "ts" | "bash" | "json" | "css" | "md" | "tsx" | Language for highlighting. |
| title | string | – | Header text, e.g. a file name. Defaults to the language. |
| collapsible | boolean | false | Collapses long code behind “Show full code”. |
Tokens used
Derived from the utility classes in the component's source. Copy a class with one click.
| Token | Tier | Value | Utility |
|---|---|---|---|
| code.bg | component | #FFFFFF | |
| code.border | component | rgba(10,10,11,0.08) | |
| code.collapsedHeight | component | 320px | |
| code.fg | component | #0A0A0B | |
| code.padding | component | 16px | |
| code.radius | component | 12px | |
| focus | semantic | #C6492E | |
| spacing.1 | semantic | 4px | |
| spacing.2 | semantic | 8px | |
| spacing.4 | semantic | 16px | |
| surface.page | semantic | #FAFAF7 | |
| text.secondary | semantic | #555555 | |
| type.small.size | semantic | 14px |
Accessibility
What is checked automatically on every build, and what a person has tested. Nothing is marked as tested unless it was.
| Check | How | Status |
|---|---|---|
| axe-core rules | Automated on every npm run check, at desktop and mobile width | Runs on every build |
| Keyboard | Automated Playwright test (scripts/keyboard.mjs) | Not tested yet |
| Screen reader | Manual | Not tested yet |
Keyboard
| Key | Action |
|---|---|
| Tab | Moves to the copy button, then into the code region. |
| Arrow keys | Scroll 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
| Pairing | Ratio (x:1) | Minimum | Status |
|---|---|---|---|
| text.primary on surface.page | 18.92 | 4.5 | Pass |
| text.primary on surface.card | 19.79 | 4.5 | Pass |
| text.secondary on surface.page | 7.13 | 4.5 | Pass |
| text.secondary on surface.card | 7.46 | 4.5 | Pass |
| focus on surface.page | 4.57 | 3.0 | Pass |
| focus on surface.card | 4.78 | 3.0 | Pass |