Code Block

Shows code highlighted at build time, alone or in tabs.

lib/greet.tsts

Installation

pnpm dlx shadcn@latest add @paneui/code-block

Usage

Composition

Examples

Code Group

Use a group when the same step has several files or variants.

Remember the Tab

Highlighted Lines

Without Title

Use a bare block for short commands; the copy button shows on hover.

Markup as Children

Input

The block never highlights in the browser. Give it markup highlighted at build time, as an html string or as children. Dual-theme output that puts --shiki-light and --shiki-dark on the <pre> and its spans follows the .dark class; output with inline light colours plus --shiki-dark works too. Only pass trusted build output to html.

Lines

Lines are the .line spans of the output. lineNumbers numbers them with CSS counters, so copying skips the numbers. Transformer classes are styled: .line.highlighted, .line.diff.add, .line.diff.remove, .has-focused .line:not(.focused) and .highlighted-word.

In a content pipeline

Map your pre to a CodeBlock: pre: ({ "data-title": title, "data-language": language, ...props }) => <CodeBlock title={title} language={language}><pre {...props} /></CodeBlock>. A CodeBlock inside a typeset container keeps its own styles.

Copy

The copy button copies the text of the <pre>. In a group it copies the open tab.

API Reference

CodeBlock

Frame with an optional title bar. Inside a CodeGroup it renders only its code; the group owns the frame. Also accepts every prop of div.

html
string
Highlighted markup, usually a whole <pre>.
children
ReactNode
Highlighted elements, used when html is not set.
title
ReactNode
Title bar text, usually a file name. Also the tab label (and, as a string, the tab value) in a group.
language
string
Label in the title bar; picks the icon when title is not a file name.
icon
ReactNode | false
Title bar icon. Defaults to the file-type icon from getFileKind; false hides it.
lineNumbers= false
boolean
Number the lines.
wrap= false
boolean
Wrap long lines instead of scrolling sideways.
copyable= true
boolean
Show the copy button.
maxHeight
number | string
Height after which the code scrolls.
PropTypeDefaultDescription
htmlstring—Highlighted markup, usually a whole <pre>.
childrenReactNode—Highlighted elements, used when html is not set.
titleReactNode—Title bar text, usually a file name. Also the tab label (and, as a string, the tab value) in a group.
languagestring—Label in the title bar; picks the icon when title is not a file name.
iconReactNode | false—Title bar icon. Defaults to the file-type icon from getFileKind; false hides it.
lineNumbersbooleanfalseNumber the lines.
wrapbooleanfalseWrap long lines instead of scrolling sideways.
copyablebooleantrueShow the copy button.
maxHeightnumber | string—Height after which the code scrolls.
data-line-numbers
On code-block-content while lineNumbers is set.
Data attributeDescription
data-line-numbersOn code-block-content while lineNumbers is set.

CodeGroup

Tabs of CodeBlocks in one frame. Also accepts every prop of div.

value
string
Controlled tab (a block's string title, else its language).
defaultValue
string
First tab shown. Defaults to the first block.
onValueChange
(value: string) => void
Called when the tab changes.
storageKey
string
Remember the tab in localStorage and switch every group with the same key together.
copyable= true
boolean
Show the copy button.
PropTypeDefaultDescription
valuestring—Controlled tab (a block's string title, else its language).
defaultValuestring—First tab shown. Defaults to the first block.
onValueChange(value: string) => void—Called when the tab changes.
storageKeystring—Remember the tab in localStorage and switch every group with the same key together.
copyablebooleantrueShow the copy button.

CodeCopyButton

The copy button on its own. value is a string or a function that returns one.

value*
string | (() => string)
Text to copy.
label= "Copy code"
string
Accessible label.
PropTypeDefaultDescription
value*string | (() => string)—Text to copy.
labelstring"Copy code"Accessible label.