Code Block
Syntax-highlighted code surface for assistant replies with copy actions.
CodeBlock renders assistant code with sugar-high highlighting, an optional filename header, copy action, language switcher, and line numbers.
greet.ts
export function greet(name: string) {
return `Hello, ${name}!`
}
console.log(greet("Cubix"))Installation
pnpm dlx cubix@latest add code-block --base radixUsage
import {
CodeBlock,
CodeBlockActions,
CodeBlockCopyButton,
CodeBlockFileIcon,
CodeBlockFilename,
CodeBlockHeader,
CodeBlockTitle,
} from "@/components/cubix/code-block"<CodeBlock code={code} language="typescript">
<CodeBlockHeader>
<CodeBlockTitle>
<CodeBlockFileIcon />
<CodeBlockFilename>greet.ts</CodeBlockFilename>
</CodeBlockTitle>
<CodeBlockActions>
<CodeBlockCopyButton />
</CodeBlockActions>
</CodeBlockHeader>
</CodeBlock>Composition
CodeBlock
├── CodeBlockHeader
│ ├── CodeBlockTitle
│ │ ├── CodeBlockFileIcon
│ │ └── CodeBlockFilename
│ └── CodeBlockActions
│ ├── CodeBlockLanguageSelector
│ └── CodeBlockCopyButton
└── CodeBlockContent (rendered automatically)Features
- Filename header with copy button
- Lightweight sugar-high highlighting that stays fast while streaming
- Optional line numbers and language selector
Minimal
{
"name": "cubix",
"version": "0.1.0",
"private": true
}Line numbers
greet.ts
export function greet(name: string) {
return `Hello, ${name}!`
}
console.log(greet("Cubix"))Language selector
greet.ts
export function greet(name: string) {
return `Hello, ${name}!`
}
console.log(greet("Cubix"))Shell
Terminal
npx cubix@latest add code-block
npm run devLong content
next.config.ts
import type { NextConfig } from "next"
const nextConfig: NextConfig = {
reactStrictMode: true,
experimental: {
optimizePackageImports: ["lucide-react"],
},
images: {
remotePatterns: [
{
protocol: "https",
hostname: "www.google.com",
},
],
},
}
export default nextConfig
async function loadSources(query: string) {
const response = await fetch(`/api/search?q=${encodeURIComponent(query)}`)
if (!response.ok) {
throw new Error("Failed to load sources")
}
return response.json()
}Streaming
greet.ts
Empty
untitled.ts
In a message
API Reference
CodeBlock
| Prop | Type | Default | Description |
|---|---|---|---|
| code | string | - | Source text to highlight. Required. |
| language | string | "plaintext" | Language id or alias resolved by sugar-high (ts, js, python, json, shell, ...). |
| showLineNumbers | boolean | false | Show CSS counter line numbers beside each line. |
| children | React.ReactNode | - | Optional header and actions rendered above the code body. |
CodeBlockCopyButton
| Prop | Type | Default | Description |
|---|---|---|---|
| onCopy | () => void | - | Called after the code is copied successfully. |
| onError | (error: Error) => void | - | Called when the clipboard write fails. |
| timeout | number | 2000 | How long the check icon stays visible after a copy. |
CodeBlockLanguageSelector
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string | - | Controlled language value. Pair with onValueChange. |
| defaultValue | string | - | Initial language when uncontrolled. |
| onValueChange | (value: string | null) => void | - | Called when the selected language changes. |