Code Block
ProA bordered block of code with a language label and a copy button.
Playground
import { ChatLoader } from "@wunderui/react"
export function Waiting() {
return <ChatLoader />
}npm install @wunderui/react{
"quarter": "Q3",
"revenue": 1842000,
"customers": 1204,
"churn": 0.018
}npm install @wunderui/reactInstallation
Code Block comes with WunderUI Core and Pro, in the package from your plan. See the plans.
Then import what you need:
import { CodeBlock } from "@wunderui/react"Then use it:
import { CodeBlock } from "@wunderui/react"
export function Install() {
return <CodeBlock language="bash" code="npm install @wunderui/react" />
}Expected result: You see a bordered block with the label bash and a Copy button on top, and the command below. Press Copy and it reads Copied for a moment.
When to use
Use Code Block for
- A command or a snippet in a doc, a changelog or a setup guide
- Code in an assistant message, between two sentences of the reply
- Short config or JSON that people copy as it is
Use something else for
- Artifact Panel for a file the agent made, with versions and a status
- Diff View for a change between two versions of the code
- Markdown for prose around the code, with headings, lists and links
Content
The code is plain text, line for line. A wide line scrolls sideways and takes focus so the arrow keys scroll it, and the block grows with its lines.
npm install @wunderui/reactcurl -X POST https://wunderui.com/api/reports -H "Content-Type: application/json" -d '{"quarter":"Q3","revenue":1842000,"customers":1204,"churn":0.018}'type Quarter = {
revenue: number
customers: number
churn: number
}
const q2: Quarter = { revenue: 1644000, customers: 9980, churn: 0.024 }
const q3: Quarter = { revenue: 1842000, customers: 11184, churn: 0.018 }
export function growth(from: Quarter, to: Quarter) {
return (to.revenue - from.revenue) / from.revenue
}
console.log(growth(q2, q3)) // 0.12Newer props
tone="dark" for logs and terminals, title and actions in the header, maxLines with a built-in Show all button, frame={false} inside a framed container, and red and green lines for language diff.
Dark tone, title, actions, line limit and diff
POST /v1/publish → 429
x-ratelimit-remaining: 0@@ -1,2 +1,2 @@- border: 1px solid gray;+ border: 1px solid var(--border); padding: 8px;Keyboard interactions
The copy button is a native button, so the browser handles focus and activation. The code area is a focusable region, so its scrolling works from the keyboard.
- TaborShift+Tab
- Moves focus between the copy button and the code area of a block, and on to the next block.
- EnterorSpace
- On the copy button: copies the code to the clipboard.
- ←or→
- On the code area: scrolls a long line sideways.
Focus shows as an indigo border with a 2 px ring on both stops. There is no copy shortcut, but Ctrl+C or Cmd+C copies a text selection as on any page.
Behavior and motion
What the block does with its code, the clipboard, assistive technology and motion.
- Plain text
- The code is rendered as text, so whitespace and line breaks are kept and markup shows as characters. There is no syntax highlighting, line numbers or wrapping option.
- Language
- The language only fills the label and names the code area. Without it the header reads code and the region is named Code code.
- Wide and tall code
- A line wider than the block does not wrap, and the code area scrolls sideways. The block has no maximum height, so cap long code in the container you put it in.
- Copy
- The button writes the code to the clipboard and waits for the result. On success it shows the check and Copied for 1.2 seconds, plays the ring pulse, shows the tooltip and calls the callback.
- When the copy fails
- If the browser refuses the write (no permission, an insecure page, a sandboxed frame), the block shows nothing and skips the callback. Offer your own fallback, such as selecting the text.
- Live region
- The block has no live region. The confirmation is a tooltip and a change of the button's name, and neither is announced reliably, so what a screen reader says depends on the reader.
- Focus
- The code area is a named region with a tab stop, so it takes focus and arrow keys scroll it. Each block adds two tab stops.
- Motion
- The copy icon fades out while it scales down and turns, and the check fades in with a short overshoot. The ring pulse is one slow animation of the shadow.
- Reduced motion
- With reduced motion the scale and rotation are dropped, so the two icons crossfade, and the ring pulse does not play. The label, check and tooltip still show.
Anatomy
The figure marks one block. The header and code area have no slots of their own.
import { ChatLoader } from "@wunderui/react"
export function Waiting() {
return <ChatLoader />
}- data-slot="code-block"
The bordered box, clipped to its rounded corners. It takes the code, the language and a copy callback, and keeps its own copy state.
The row above the code, with a bottom border: the language label on the left and the copy button on the right.
- 1
The language in small tertiary type. It is a label only and loads no grammar, and without it the label reads code.
- 2
A native button with an icon and the word Copy. After a successful copy it shows a check and Copied for about a second, with a green ring pulse and a tooltip.
- 3
A scrollable region named after the language, with a tab stop. It scrolls sideways when a line is wider than the block, and arrow keys scroll it once it has focus.
- 4
The code, shown verbatim in the foreground colour. Whitespace is kept, nothing is highlighted and lines are not numbered.
Best practices
- Do
Always pass the language, such as tsx or bash. It labels the header and names the code area for screen readers, which otherwise hear Code code.
- Do
Use the copy callback for side effects such as analytics. It runs only after the code reached the clipboard, and the block already confirms the copy itself, so add no toast.
- Do
Keep lines short where you can. A wider line scrolls sideways, and the code area is keyboard reachable, so scrolling works without a mouse.
- Don’t
Don't expect syntax highlighting, line numbers, line wrapping or a maximum height. The block shows plain monospace text, scrolls sideways and grows with its lines.
- Don’t
Don't treat a click on Copy as a copy. If the browser refuses the clipboard, the block shows no confirmation or error and skips the callback, so report a copy as done only from the callback.
API reference
One table. CodeBlock is a plain component and wraps no primitive, so the table lists every prop.
The bordered box, clipped to its rounded corners. It takes the code, the language and a copy callback, and keeps its own copy state.
| Prop | Type | Default | Description |
|---|---|---|---|
| code required | string | — | The code to show and to copy, as one string. It is rendered as text, line for line, and copied as it is. |
| language | string | — | The label in the header and the name of the code area (). It loads no grammar. Without it the label reads . |
| onCopy | () => void | — | Called after the code landed on the clipboard. It is not called when the write is refused. The block already confirms the copy, so use it for side effects only. |
| className | string | — | Extra classes for the outer box, for example . The box clips its content to its rounded corners. |
| actions | ReactNode | — | Extra controls rendered in the header, left of the copy button. |
| frame | boolean | true | When false, the block's outer border and rounding are removed for embedding in an already-framed container. |
| maxLines | number | — | When the code has more lines, only the first maxLines are shown; a toggle button reveals the rest. |
| title | ReactNode | — | Content shown in the header instead of the language label. The language still names the code region via aria-label. |
| tone | "default" | "dark" | "default" | Visual tone of the block. "dark" darkens the container, header, copy button and code text. |
Notes for AI
Give your coding assistant the Markdown reference instead of screenshots.
get_component("code-block")The full library index for assistants is at /llms.txt.
Related components
Three things stand out in the Q3 report:
- Revenue: $1,842,000, up 12%
- New customers: 1,204
- Churn: 1.8%, down from 2.4%
Next steps:
- Share the report with Priya Raman
- Review churn with Daniel Okafor
- Plan the Q4 launch
AI & Agents2 stylesProMarkdownProse styling wrapper for rendered markdown/HTML assistant output.usage-summary.tsxCode · edited 2 min ago 1export function UsageSummary() {2return (3<div className="rounded-xl border border-border bg-card p-4">4<p className="text-sm text-text-secondary">Requests this month</p>5<p className="text-2xl font-semibold">1,842</p>6<p className="text-sm text-text-secondary">Cost $69.98</p>7<p className="text-xs text-text-tertiary">Up 12% on last month</p>8</div>9)10}AI & Agents6 stylesProArtifact PanelThe side panel for what the agent made — code or a document — with versions, copy and download.@@ -12,7 +12,8 @@ export async function sendReminders()
1212const overdue = invoices.filter(isOverdue)1313for (const invoice of overdue) {14−Removed:if (daysSince(invoice.dueDate) < 7) continue14+Added:if (daysSince(invoice.dueDate) < 3) continue15+Added:if (invoice.reminderSentAt) continue1516await send({1617to: invoice.customer.email,17−Removed:subject: "Payment reminder",18+Added:subject: `Invoice ${invoice.number} is overdue`,1819})AI & Agents4 stylesProDiff ViewThe result of an edit tool call: file, added and removed lines, and the changed code.