Code block
A block of code with a header for the file name or language, a copy button and a wrap switch. Lines can be numbered, marked, or shown as added and removed, and long code scrolls or folds. For a word of code inside a sentence, use inline code from the typography set.
Install
npx shadcn@latest add @thrum-pro/code-blockThis is a Pro component. The command works once your project has a licence key: set up Pro or see pricing.
Automatically installs icon chevron-down from the icon set.
Data shape
type CodeToken = { text: string; kind: "keyword" | "string" | "number" | "comment" | "tag" | "property" | "punctuation" | "plain" };
// tokens: CodeToken[][], one array per line[
[{ text: "const", kind: "keyword" }, { text: " seats = ", kind: "plain" }, { text: "20", kind: "number" }],
[{ text: "// per workspace", kind: "comment" }],
]Props
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | Default "" | The code as plain text. It is coloured by the built-in tokenizer for language. One trailing line break is dropped. |
tokens | CodeToken[][] | – | Code already split into lines of coloured pieces, from your own highlighter. Wins over code. |
language | "tsx" | "ts" | "jsx" | "js" | "json" | "bash" | "css" | "text" | Default "text" | The language of code, for the colours and for the label in the header when there is no file name. |
filename | string | – | Shown in the header in place of the language, and used in the copy button's name. |
lineNumbers | boolean | Default false | Number the lines in a column that is not copied and not read aloud. |
startLine | number | Default 1 | The number of the first line, for an excerpt from the middle of a file. Marks use the same numbers. |
highlight | string | – | Lines to mark with a tint and a 2 pixel bar at the start: "2,5-7". |
added | string | – | Lines marked as added: a plus sign and a green tint. Screen readers hear "Added" before the line. |
removed | string | – | Lines marked as removed: a minus sign and a red tint. Screen readers hear "Removed" before the line. |
defaultWrap | boolean | Default false | Start with long lines wrapped instead of scrolling sideways. |
wrapToggle | boolean | Default true | Show the Wrap button in the header, a toggle button that switches wrapping. |
copyable | boolean | Default true | Show the copy button. It copies the whole code, also when it is folded. |
maxHeight | number | – | The tallest the code area grows, in pixels, before it scrolls. Ignored while folded. |
collapseAfter | number | – | Show only this many lines, faded at the bottom, with a button that shows the rest. |
sound | boolean | Default true | Play the copy button's success or error sound, if the visitor has sound on. |
aria-label | string | – | The name of the code area. It defaults to the file name or the language. |
...props | ComponentProps<"div"> | – | Every other div attribute is passed through. className is merged last. |
tokenizeCode() | (code: string, language?: CodeLanguage) => CodeToken[][] | – | A second export: the built-in tokenizer, for code you colour elsewhere. |
parseLineRanges() | (ranges?: string) => Set<number> | – | A third export: reads "2,5-7" into a set of line numbers. |
Motion and sound
Motion
- The fold button's chevron turns over 150 ms. Nothing else moves.
- Reduced motion: the chevron turns at once.
Sound
- The copy button plays the success chime after a copy, and the error tone if the browser refuses the clipboard.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- The code area is a pre element named by the file name or language. It is focusable, so a keyboard can scroll it.
- Line numbers, plus and minus signs are hidden from screen readers and from selection. Added and removed lines carry the words "Added" and "Removed" for screen readers.
- The Wrap button is a toggle button with aria-pressed. The fold button says how many lines there are and has aria-expanded.
- The copy button announces Copied in a polite live region.
- Token colours are theme series colours pulled towards the text colour, so they keep 4.5 to 1 on the card in both themes. Comments are also italic.
- The code block frame is a panel surface (data-surface="panel"): under a glass material it becomes translucent while keeping token colours and line marks readable.
| Keys | Action |
|---|---|
| Tab | Move to the Wrap button, the copy button, the code area and the fold button. |
| Arrow keys | In the code area: scroll it, when it scrolls. |
| Enter / Space | On a button: switch wrapping, copy, or show and hide the rest of the lines. |
Source
The source comes with Pro
TypeScript source · Copy-paste ready · MIT-compatible for client projects · Includes all dependencies
Files and dependencies
- Adds
components/ui/code-block.tsx- Packages
- None
- Brings along
- Copy button
- Icons
chevron-down
Changelog
- 1.0.0 · 2026-12-03First release.