Skip to content
RefineUI

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.

app/api/chat/route.ts
export async function POST(request: Request) {  const { messages } = await request.json();   return Response.json({ messages });}

Overview

Machine-readable contract from Component Spec.

component: code-blockexport: CodeBlockversion: 1attributes: data-refineui=code-block

Anatomy

Partdata-refineui
rootcode-block
actionscode-block-actions
contentcode-block-content
copycode-block-copy
headercode-block-header
linecode-block-line
titlecode-block-title
npm install @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.

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.

CodeBlock, CodeBlockHeader, CodeBlockTitle, and CodeBlockActions accept standard div attributes. Compose CodeBlockHeader and CodeBlockContent inside the root; put the title and actions inside the header.

PropDefaultTypeDescription
wrapfalsebooleanWrap long source lines at the available width.
lineNumbersfalsebooleanShow an automatically numbered, non-selectable gutter.
PropDefaultTypeDescription
variant"default""default" | "highlighted"Visual treatment for the source line.
children—ReactNodePlain text or syntax-highlighted content.
PropDefaultTypeDescription
value—stringRequired plain source copied to the clipboard.
copyLabel"Copy code"stringAccessible label before a successful copy.
copiedLabel"Copied"stringAccessible label while the copied state is visible.
onCopied—(value: string) => voidCalled after source text has been copied.