Code Block
A syntax-highlighted code block component with copy functionality and customizable layout
Preview
Installation
Usage
Examples
Floating Copy Button
Display code without a header using a floating copy button that appears on hover.
With Filename
Display a filename in the header to provide context for the code snippet.
With Tabs
Show multiple code variants using tabs, commonly used for different package managers or language implementations.
Switch between package managers using the tabs in the header.
With Line Numbers
Add line numbers to help users reference specific lines of code.
Multiple Languages
The component automatically detects and highlights syntax for different programming languages.
TypeScript
Python
Bash
Custom Icon
Override the default language icon with a custom icon of your choice.
Custom Height
Control the maximum height and scrolling behavior with custom classes.
Custom max height with scrolling (max-h-[200px])
Server-Side Highlighting
Pre-highlight code on the server using the highlight function for instant rendering with zero client-side delay.
Highlighted Lines
Emphasize specific lines with background highlighting and a left border accent.
Highlight specific lines (lines 2, 5)
Highlight range using string syntax (lines 1-3, 7)
Diffs
Show code changes with added, removed, and modified line indicators. Diff markers are automatically stripped from the displayed code.
Diff view with added (+), removed (-), and modified (!) lines
Using comment-style markers for TypeScript/JSX compatibility
Focus Mode
Blur non-focused lines to draw attention to specific parts of the code.
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.