Code Block
A high-performance syntax-highlighted code surface powered by Shiki with zero-DOM CSS line numbers, dual themes, line highlighting, collapsible blocks, diff mode, code folding, diagnostic levels, and copy actions.
$ pnpm dlx shadcn@latest add @byphongphan/code-block@byphongphan in components.jsonInstallation
$ pnpm dlx shadcn@latest add @byphongphan/code-block@byphongphan in components.jsonAnatomy
Usage
Examples
Git Diff & Patch Review
Display additions and removals with dedicated line gutters and background tints for reviewing pull requests and patch differences.
Diagnostic Severity Levels
Surface inline compiler and linter diagnostics with error, warning, and info visual indicators.
Collapsible with Max Lines
Clamp lengthy code snippets to a clean maximum height with a smooth bottom gradient fade and floating expand button.
Action Toolbar with Wrap & Download
A complete header bar equipped with soft line-wrapping toggles, one-click file download, and clipboard copy affordances.
API Reference
CodeBlock
The root container managing syntax highlighting, Shiki streaming, CSS counter state, and context providers.
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | "" | Raw source code string to highlight. |
language? | string | undefined | Programming language identifier (e.g. "typescript", "json", "bash"). |
lines? | CodeBlockLine[] | undefined | Pre-tokenized server-side lines to skip client-side Shiki highlighter. |
showLineNumbers? | boolean | false | Whether to display numeric gutter indicators. |
startLine? | number | 1 | 1-based initial counter seed for line numbering. |
highlightedLines? | CodeBlockLineSpec | undefined | Comma-separated lines or inclusive ranges to highlight (e.g. "2,4-6"). |
highlightedWords? | CodeBlockWordSpec[] | undefined | Exact token substrings to highlight with visual pills. |
focusedLines? | CodeBlockLineSpec | undefined | Lines to keep focused while dimming other lines. |
diff? | CodeBlockDiffSpec | undefined | Addition and removal line specs (e.g. { added: "2-4", removed: "6" }). |
lineLevels? | CodeBlockLevelSpec | undefined | Diagnostic levels per line (e.g. { error: "3", warning: "7" }). |
maxLines? | number | undefined | Maximum visible lines before clamping with a collapsible fade. |
expanded? | boolean | undefined | Controlled collapsed/expanded state when maxLines is set. |
defaultExpanded? | boolean | false | Initial uncontrolled expanded state. |
onExpandedChange? | (expanded: boolean) => void | undefined | Callback fired when the expanded state changes. |
foldable? | boolean | false | Enables indentation-based code folding with chevron toggles. |
folded? | number[] | undefined | Controlled array of folded region start lines. |
defaultFolded? | number[] | undefined | Initial uncontrolled folded region start lines. |
onFoldedChange? | (folded: number[]) => void | undefined | Callback fired when folded regions change. |
wrap? | boolean | undefined | Controlled soft text wrapping toggle. |
defaultWrap? | boolean | false | Initial soft text wrapping state. |
onWrapChange? | (wrap: boolean) => void | undefined | Callback fired when wrap state changes. |
selectable? | boolean | false | Allows clicking and Shift-clicking lines for selection. |
selectedLines? | number[] | undefined | Controlled array of selected line numbers. |
defaultSelectedLines? | number[] | [] | Initial uncontrolled selected line numbers. |
onSelectedLinesChange? | (lines: number[]) => void | undefined | Callback fired when line selections change. |
streaming? | boolean | false | Enables AI token streaming optimizations with animated caret. |
variant? | "default" | "ghost" | "default" | Visual surface variant. "ghost" renders a transparent backdrop. |
className? | string | undefined | Additional classes merged onto the root element. |
CodeBlockContent
Composable surface that renders the code block inside an external scroll wrapper (e.g. ScrollArea).
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | undefined | Additional classes for the surface element. |
CodeBlockHeader
Top toolbar header housing filename title, language tag, and action buttons.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | undefined | Additional classes for the header container. |
children | React.ReactNode | undefined | Header content elements. |
CodeBlockTitle
Displays the file name or snippet label with standard typography.
| Prop | Type | Default | Description |
|---|---|---|---|
children? | React.ReactNode | undefined | Title label text. Defaults to active file name. |
className? | string | undefined | Additional classes for the title element. |
CodeBlockLanguage
Displays the detected or specified syntax language badge.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | undefined | Additional classes for the language label. |
CodeBlockCopyButton
Interactive clipboard button with animated copied state feedback.
| Prop | Type | Default | Description |
|---|---|---|---|
value? | string | undefined | Explicit text to copy. Defaults to the current code snippet. |
position? | "default" | "pinned" | "default" | Placement strategy. "pinned" places the button in the top-right corner. |
alwaysVisible? | boolean | false | When pinned, keeps the button visible instead of fading in on hover. |
className? | string | undefined | Additional classes for the copy button. |
CodeBlockDownloadButton
Downloads the current code block content as a local file.
| Prop | Type | Default | Description |
|---|---|---|---|
filename? | string | undefined | Target download filename (e.g. "snippet.tsx"). |
label? | string | "Download code" | Accessible ARIA label. |
className? | string | undefined | Additional classes for the download button. |
CodeBlockWrapToggle
Toggles soft line-wrapping on and off.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | undefined | Additional classes for the toggle button. |
CodeBlockExpandButton
Floating expand/collapse control rendered when maxLines clamps code block height.
| Prop | Type | Default | Description |
|---|---|---|---|
expandText? | string | "Expand" | Label displayed when collapsed. |
collapseText? | string | "Collapse" | Label displayed when expanded. |
className? | string | undefined | Additional classes for the button. |
CodeBlockLineActions
Render prop registering custom action controls on hovered/active code lines.
| Prop | Type | Default | Description |
|---|---|---|---|
side? | "end" | "gutter" | "end" | Placement of the action control on the line row. |
children | (data: { line: number, text: string }) => ReactNode | — | Render prop receiving active line metadata. |