Code Block
A syntax-highlighted code block component with copy functionality and customizable layout
The code block renders syntax-highlighted code with optional headers, filenames, tabs, line numbers, diffs, and a copy button. Use it for documentation snippets, install commands, and any multi-line code sample.
To copy a single string from an inline icon button, use CopyButton instead.
Preview
Installation
Usage
Composition
Examples
Floating Copy Button
Set floatingCopy on CodeBlock to show a copy button that appears on hover, without a header.
With Filename
Pass filename to CodeBlockHeader to label the snippet.
With Tabs
Pass tabs to CodeBlockHeader to switch between code variants such as package managers or languages.
Switch between package managers using the tabs in the header.
With Line Numbers
Set lineNumbers on CodeBlockPre to number each line.
Multiple Languages
Set language on CodeBlock to highlight any Shiki-supported language.
TypeScript
Python
Bash
Custom Icon
Pass customIcon to CodeBlockHeader to replace the default language icon.
Custom Height
Set a max-h-* class on CodeBlockPre to cap the height and scroll overflow.
Custom max height with scrolling (max-h-[200px])
Server-Side Highlighting
Pre-highlight on the server with the highlight function and pass the result as initial for instant rendering.
Highlighted Lines
Pass highlightLines to CodeBlock to emphasize lines with a background and left border.
Highlight specific lines (lines 2, 5)
Highlight range using string syntax (lines 1-3, 7)
Diffs
Set showDiff to mark added, removed, and modified lines; the diff markers are stripped from the displayed code.
Diff view with added (+), removed (-), and modified (!) lines
Using comment-style markers for TypeScript/JSX compatibility
Focus Mode
Pass focusLines to CodeBlock to blur every line except the ones you name.
Focus on specific lines (lines 4-5) - other lines are dimmed
Focus on a range using string syntax (lines 1-2, 8)
API Reference
The CodeBlock component is a custom implementation built with Shiki for syntax highlighting. It does not extend Base UI.
Props
CodeBlock
The main component that manages state and provides context to sub-components automatically via React Context. Sub-components can access code, language, nodes, and lines without explicit prop passing.
CodeBlockHeader
Smart header component that auto-arranges language icons, filenames, tabs, and copy button based on props. Automatically consumes language and code from CodeBlock context.
CodeBlockPre
The pre-formatted text container that wraps the code content. Automatically consumes lines from CodeBlock context for line numbers. Includes group and relative classes for floating button support. Uses ScrollArea internally for styled scrollbars and fade edges.
CodeBlockCode
The actual syntax-highlighted code element. Automatically consumes code and nodes (highlighted JSX) from CodeBlock context.