Code Block
Composable, token-driven code display with headers, actions, line numbers, wrapping, and copy support.
CodeBlock owns presentation and actions, while syntax highlighting stays
bring-your-own. Pass highlighted React nodes or plain strings to each
CodeBlockLine.
1export async function POST(request: Request) {2 const { messages } = await request.json();3 4 return Response.json({ messages });5}Contract
Section titled “Contract”Overview
Machine-readable contract from Component Spec.
Anatomy
| Part | data-refineui |
|---|---|
| root | code-block |
| actions | code-block-actions |
| content | code-block-content |
| copy | code-block-copy |
| header | code-block-header |
| line | code-block-line |
| title | code-block-title |
Installation
Section titled “Installation”npm install @refineui/react @refineui/tokens yarn add @refineui/react @refineui/tokens pnpm add @refineui/react @refineui/tokens bun add @refineui/react @refineui/tokens import { CodeBlock, CodeBlockActions, CodeBlockContent, CodeBlockCopyButton, CodeBlockHeader, CodeBlockLine, CodeBlockTitle,} from "@refineui/react"; const code = "export const ready = true"; <CodeBlock> <CodeBlockHeader> <CodeBlockTitle>status.ts</CodeBlockTitle> <CodeBlockActions> <CodeBlockCopyButton value={code} /> </CodeBlockActions> </CodeBlockHeader> <CodeBlockContent lineNumbers wrap={false}> <CodeBlockLine>{code}</CodeBlockLine> </CodeBlockContent></CodeBlock> Use variant="highlighted" on a CodeBlockLine to draw attention to a range.
Use wrap and lineNumbers on CodeBlockContent to control rendering without
changing the source passed to CodeBlockCopyButton.
Actions and syntax highlighting
Section titled “Actions and syntax highlighting”CodeBlockActions accepts CodeBlockCopyButton and ordinary RefineUI Button
components. That keeps application-specific actions, such as downloads and line
wrapping, under your control.
CodeBlockLine accepts any ReactNode, so a syntax-highlighting library can
provide tokenized spans directly. Keep the unformatted source in the copy
button’s required value prop to make copying reliable.
The demo uses RefineUI semantic foreground tokens for its highlighted spans; you can use the same approach or pass tokens from Shiki, as shadcn/ui does.
API Reference
Section titled “API Reference”CodeBlock and layout parts
Section titled “CodeBlock and layout parts”CodeBlock, CodeBlockHeader, CodeBlockTitle, and CodeBlockActions accept
standard div attributes. Compose CodeBlockHeader and CodeBlockContent
inside the root; put the title and actions inside the header.
CodeBlockContent
Section titled “CodeBlockContent”| Prop | Default | Type | Description |
|---|---|---|---|
| wrap | false | boolean | Wrap long source lines at the available width. |
| lineNumbers | false | boolean | Show an automatically numbered, non-selectable gutter. |
CodeBlockLine
Section titled “CodeBlockLine”| Prop | Default | Type | Description |
|---|---|---|---|
| variant | "default" | "default" | "highlighted" | Visual treatment for the source line. |
| children | — | ReactNode | Plain text or syntax-highlighted content. |
CodeBlockCopyButton
Section titled “CodeBlockCopyButton”| Prop | Default | Type | Description |
|---|---|---|---|
| value | — | string | Required plain source copied to the clipboard. |
| copyLabel | "Copy code" | string | Accessible label before a successful copy. |
| copiedLabel | "Copied" | string | Accessible label while the copied state is visible. |
| onCopied | — | (value: string) => void | Called after source text has been copied. |