Search Documentation

Search for components, guides, and more.

Code Block

Display syntax-highlighted code snippets using Shiki.

Shiki Reference Themes

Examples

File Title

Use CodeBlockTitle to display a filename header on the code block

Code Block Basics

Line Selection Highlight

Use highlight property for the line-selection highlight

Installation

pnpm dlx shadcn@latest add https://chatcn.me/c/codeblock

Show Line Numbers

To show line numbers in your code block, add the following styles to your global.css:

code {
counter-reset: step;
counter-increment: step 0;
margin: 0;
width: 100%;
}

code .line::before {
content: counter(step);
counter-increment: step;
width: 1rem;
margin-right: 1.5rem;
display: inline-block;
text-align: right;
color: gray;
}


Props

CodeBlock

PropTypeDefaultDescription
childrenstring or ReactNode-Code content (string) or compound components like CodeBlockTitle
codestring-The code content to display (use this when using compound children)
langBundledLanguage-Programming language for syntax highlighting (e.g., tsx, css, python)
heightstring"600"Maximum height of the code block in pixels
classNamestring-Additional CSS classes to apply
highlightobject-Line range to highlight (start and end properties)
themestring"github-dark-default"Shiki theme name for syntax highlighting

CodeBlockTitle

PropTypeDefaultDescription
childrenstring-The filename to display
langstring-Devicon language name for file icon (e.g., typescript, python, rust)
classNamestring-Additional CSS classes to apply