Web · Components
CodeBlock
Syntax-highlighted code block.
Live example
Preview
Code
Events
- Events appear here when you interact with the preview.
Comparing two codes: side by side (split), changed words highlighted
Without a title: language name in the corner, copy button on hover (always on touch)
Added / removed lines (automatic in the diff language)
Long code: collapsing with maxLines
Import
Syntax-highlighted code block. Colors come from the theme (--ui-code-* variables; separate for light and dark), so they fit custom themes too. The highlighter is the library's own, with no external dependency; each language is a separate chunk downloaded only when used on the page. Server-rendered pages get colored HTML ready.
When a title (title) or icon is given, a strip with the file name, language name and copy button appears on top; otherwise the copy button sits at the top right corner (appears on hover, always visible on touch). Long lines scroll horizontally with a scrollbar that takes no width; with wrap they wrap.
With original it shows the diff of two pieces of code (one column or side by side with diffView; long unchanged regions collapse).
Line numbers, highlighted / inserted / deleted lines, a maximum height and collapsing with "Show all" are given with props. Line numbers don't enter the selection or the copied text.
To change colors, define --ui-code-keyword, --ui-code-string, --ui-code-function… variables in the theme (full list in theme.css).
Examples
loadLanguage
functionLoads a language's rules. The same language always gets the same Promise, marked the way React expects once resolved, so use() reads a loaded language right away without suspending. Resolves with null for an unknown language.
registerLanguage
functionAdds your own language or replaces a built-in one. Rules are ordered; the first rule matching at a position wins. Call it once at app start.
Examples
Line selection: line numbers and ranges. An array ([1, [4, 6]]) or a string ("1,4-6"). Numbers are the line numbers shown on screen (relative to startLine).
CodeBlockClassNames
interfaceClasses that can be given to the inner parts of CodeBlock.
| Name | Type | Description |
|---|---|---|
header? | string | Top strip holding the title, language name and copy button. |
title? | string | Title text. |
viewport? | string | Area where the code scrolls (e.g. padding). |
pre? | string | The <pre> element (e.g. font size: text-sm). |
line? | string | Each line. |
copyButton? | string | Copy button. |
CodeBlockProps
interfaceProps of the CodeBlock component.
Extends: Omit<ComponentProps<"div">, "title" | "children" | "onCopy">
| Name | Type | Default | Description |
|---|---|---|---|
code | string | Code to show. A single leading and trailing blank line is trimmed. | |
language? | CodeLanguage | Highlighting language: tsx, ts, js, json, html, css, bash, python, sql, md, yaml, diff, go, rust, java, kotlin, c, cpp, csharp, php, ruby, swift, graphql, dockerfile, toml… Each language loads only when used. An unknown name or "text" shows as plain text. For your own language see registerLanguage. | |
title? | ReactNode | Title shown in the top strip, usually a file name ("app/page.tsx"). | |
icon? | IconProp | Icon left of the title: a name or an element. When given, the top strip is shown. | |
lineNumbers? | boolean | false`; `true` in diff mode (when `original` is given) | Show line numbers. Numbers aren't selected or copied. |
startLine? | number | 1 | Number of the first line. |
highlightLines? | CodeLineSelection | Lines to highlight: [3, [5, 7]] or "3,5-7". Ignored in diff mode. | |
addedLines? | CodeLineSelection | Lines shown as inserted (green). With language="diff" + lines are marked automatically. | |
removedLines? | CodeLineSelection | Lines shown as deleted (red). With language="diff" - lines are marked automatically. | |
wrap? | boolean | false | Wrap long lines instead of scrolling. |
maxHeight? | number | string | Code taller than this scrolls vertically (320, "50vh"). | |
maxLines? | number | If the code is longer than this many lines only the first lines are shown, with a "Show all" button below. So long examples don't take over the page (especially on mobile). | |
original? | string | Old code to compare with. When given the block switches to diff mode: code is the new state, deleted lines are red, inserted ones green, and changed words within a line are also highlighted. The diff is found with the library's own algorithm (Myers, the one git uses); the code is still highlighted with language. The header shows +inserted −deleted counts. | |
diffView? | CodeDiffView | "unified" | Diff view: "unified" shows old and new lines stacked in one column, "split" shows old on the left and new on the right. "split" becomes "unified" automatically on narrow screens (below 640 px). |
contextLines? | number | false | 3 | Number of unchanged lines shown around changes in diff mode; longer unchanged regions collapse as "12 unchanged lines" and expand on click. false shows everything. |
copyable? | boolean | true | Show the copy button. |
onCopy? | (code: string) => void | Called with the copied text when the code is copied. | |
classNames? | CodeBlockClassNames | Classes added to the inner parts. |
CodeDiffView
typeDiff view: one column (unified) or side by side (split).
CodeLanguageName
typeCanonical names of the languages built into the library.
CodeLanguage
typeThe language prop of CodeBlock: a built-in language name or alias (ts, py, sh, yml…), a language added with registerLanguage, or plain text that is not colored ("text").
CodeTokenType
typeHighlighted token kinds. Each kind is colored by a --ui-code-* theme variable (e.g. keyword → --ui-code-keyword).
CodeRule
interfaceA language rule.
| Name | Type | Description |
|---|---|---|
type? | CodeTokenType | Kind of the matched text; plain text when undefined. With inside, text the inner language cannot match gets this kind. |
pattern | RegExp | Pattern to match. All rules combine into one expression with the gm flags (^ and $ are line start/end); the i flag is supported, others are ignored. Do not use back references (\1): numbers shift when combined; capture groups are fine. |
inside? | CodeGrammar | (() => CodeGrammar) | Also highlight the matched text with this language (e.g. ${…} in a template string, <style> in HTML). A function is called on first use; it lets a language refer to itself. |
CodeGrammar
typeOrdered rules of a language.