Basic usage
With header and copy button
Add a header with a label and copy button.Language selection
Allow users to switch between different code examples.Collapsible code
Collapse long code blocks to save space.Without line numbers
Hide line numbers for cleaner display.Floating copy button
Add a floating copy button that appears on hover.Controlled value
Control the selected language/tab externally.Custom styling
Apply custom styles and classes.Supported languages
CodeBlock supports all languages provided by Prism React Renderer, including:- JavaScript / TypeScript
- Python
- Java
- Go
- Rust
- Ruby
- PHP
- C / C++
- C#
- Swift
- Kotlin
- HTML / CSS
- SQL
- Bash / Shell
- And many more
API reference
CodeBlock (root)
boolean
default:"false"
Whether to hide line numbers.
string
Currently selected language/tab (controlled).
string
Default selected language/tab (uncontrolled).
(value: string) => void
Callback when the selected language/tab changes.
number
Maximum number of lines to show before collapsing.
string | number
Maximum height of the code block container.
boolean
Whether the code block is collapsed (controlled).
boolean
default:"true"
Default collapsed state (uncontrolled).
(value: boolean) => void
Callback when the collapsed state changes.
string
Additional CSS classes to apply.
CodeBlock.Header
ReactNode
required
Header content, typically label and copy button.
string
Additional CSS classes to apply.
CodeBlock.Content
ReactNode
required
Content, typically
CodeBlock.Code elements.string
Additional CSS classes to apply.
CodeBlock.Code
string
required
Code content to syntax highlight.
Language
required
Programming language for syntax highlighting.
string
Value to match against the CodeBlock’s value prop for conditional rendering.
string
Additional CSS classes to apply.
CodeBlock.Label
ReactNode
required
Label content.
string
Additional CSS classes to apply.
CodeBlock.CopyButton
'default' | 'floating'
default:"'default'"
Visual variant of the copy button.
string
Additional CSS classes to apply.
CodeBlock.CollapseTrigger
ReactNode | ((collapsed: boolean) => ReactNode)
Button content. Can be a function that receives the collapsed state.
string
Additional CSS classes to apply.