Skip to content
Thrum

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-block

This 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
type CodeToken = { text: string; kind: "keyword" | "string" | "number" | "comment" | "tag" | "property" | "punctuation" | "plain" };
// tokens: CodeToken[][], one array per line
Sample
[
  [{ text: "const", kind: "keyword" }, { text: " seats = ", kind: "plain" }, { text: "20", kind: "number" }],
  [{ text: "// per workspace", kind: "comment" }],
]

Props

PropTypeDefaultDescription
codestringDefault ""The code as plain text. It is coloured by the built-in tokenizer for language. One trailing line break is dropped.
tokensCodeToken[][]–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.
filenamestring–Shown in the header in place of the language, and used in the copy button's name.
lineNumbersbooleanDefault falseNumber the lines in a column that is not copied and not read aloud.
startLinenumberDefault 1The number of the first line, for an excerpt from the middle of a file. Marks use the same numbers.
highlightstring–Lines to mark with a tint and a 2 pixel bar at the start: "2,5-7".
addedstring–Lines marked as added: a plus sign and a green tint. Screen readers hear "Added" before the line.
removedstring–Lines marked as removed: a minus sign and a red tint. Screen readers hear "Removed" before the line.
defaultWrapbooleanDefault falseStart with long lines wrapped instead of scrolling sideways.
wrapTogglebooleanDefault trueShow the Wrap button in the header, a toggle button that switches wrapping.
copyablebooleanDefault trueShow the copy button. It copies the whole code, also when it is folded.
maxHeightnumber–The tallest the code area grows, in pixels, before it scrolls. Ignored while folded.
collapseAfternumber–Show only this many lines, faded at the bottom, with a button that shows the rest.
soundbooleanDefault truePlay the copy button's success or error sound, if the visitor has sound on.
aria-labelstring–The name of the code area. It defaults to the file name or the language.
...propsComponentProps<"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.
KeysAction
TabMove to the Wrap button, the copy button, the code area and the fold button.
Arrow keysIn the code area: scroll it, when it scrolls.
Enter / SpaceOn a button: switch wrapping, copy, or show and hide the rest of the lines.

Source

SourcePro

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. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor