Code Block
CodeBlock and CodeTabs: syntax highlighting for ts, tsx, js, jsx, json, css, html, bash, md and diff with Lezer parsers loaded on first use, rendered as React elements in syntax tokens that hold 4.5:1; a filename header, copy (an agent action too), line numbers, highlighted lines, wrap and a max height. Code for inline code.
Example
Inline, npx shadcn add @eskais/code-block reads as code inside a sentence.
Installation
@lezer/common@1.5.5@lezer/highlight@1.2.5@lezer/javascript@1.5.6@lezer/json@1.0.3@lezer/css@1.3.9@lezer/html@1.3.13@lezer/markdown@1.8.1lucide-reactUsage
Use CodeBlock for any code you show: a filename title, copy, line numbers, highlighted lines, wrap and maxHeight. CodeTabs holds several files; Code is inline code.
API reference
CodeBlock
| Prop | Type | Default |
|---|---|---|
coderequired | string | – |
languagets, tsx, js, jsx, json, css, html, bash/sh, md, diff or plaintext; aliases like "typescript" work too. | CodeLanguage | (string & {}) | – |
titleA filename or caption in a header above the code. | ReactNode | – |
actionsContent at the end of the header, before the copy button, like a language badge. | ReactNode | – |
copyableShows a copy button. Agents can always call the block's `copy` action. | boolean | true |
onCopy | CodeBlockOptions["onCopy"] | – |
lineNumbers | boolean | false |
highlightLinesOne-based line numbers or inclusive ranges, marked with a fill and a start edge. | readonly LineRange[] | – |
wrap | boolean | false |
maxHeightA CSS length, like "24rem"; the code scrolls inside past it. | string | – |
labels | Partial<CodeBlockLabels> | – |
Also takes <div> attributes except children, title, onCopy.
Code
Takes <code> attributes.
CodeTabs
| Prop | Type | Default |
|---|---|---|
filesrequired | readonly CodeFile[] | – |
aria-labelrequiredNames the tab list, like "Installation files". | string | – |
value | string | – |
defaultValue | string | – |
onValueChange | (value: string) => void | – |
Also takes Pick< CodeBlockProps, "lineNumbers" | "wrap" | "maxHeight" | "copyable" | "onCopy" | "labels" | "className" >.
Other exports
highlightsyntaxHighlighterloadParserneedsParsernormalizeLanguageparserForsyntaxToneOfwatchParsercodeBlocktype CodeLanguagetype CodeLinetype CodeTokentype DiffKindtype SyntaxTonetype CodeBlockCopyDetailstype CodeBlockOptionstype CodeBlockLabelstype CodeBlockPropstype CodePropstype LineRangetype CodeFiletype CodeTabsPropsAccessibility
- Highlighting is React elements in syntax tokens, each 4.5:1 on the code surface and on a highlighted line, in both modes and every palette.
- A block that scrolls takes focus so the keyboard can scroll it; line numbers are hidden from screen readers and from selection.
- Copying announces Copied in a status region; highlighted and diff lines also carry a start edge or their +/- sign, not colour alone.
Agent actions
Each block is a group named by its title (or aria-label) whose value is the code, with a copy action. The copy button is hidden from agents so every block's copy has one name.
Runs agent.snapshot() from @ui-sdk/react/agent on the live preview above.