Skip to content
WunderUI

Code Block

Pro

A bordered block of code with a language label and a copy button.

Playground

tsx
import { ChatLoader } from "@wunderui/react"

export function Waiting() {
  return <ChatLoader />
}

Installation

Code Block comes with WunderUI Core and Pro, in the package from your plan. See the plans.

Then import what you need:

tsx
import { CodeBlock } from "@wunderui/react"

Then use it:

tsx
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.

bash
npm install @wunderui/react

Newer 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

Response
attempt 3/3
POST /v1/publish → 429
x-ratelimit-remaining: 0
diff
@@ -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.

TaborShiftTab
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.

tsx
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.

  • The language in small tertiary type. It is a label only and loads no grammar, and without it the label reads code.

  • 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.

  • 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.

  • 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.

PropTypeDefaultDescription
code requiredstring—The code to show and to copy, as one string. It is rendered as text, line for line, and copied as it is.
languagestring—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.
classNamestring—Extra classes for the outer box, for example . The box clips its content to its rounded corners.
actionsReactNode—Extra controls rendered in the header, left of the copy button.
framebooleantrueWhen false, the block's outer border and rounding are removed for embedding in an already-framed container.
maxLinesnumber—When the code has more lines, only the first maxLines are shown; a toggle button reveals the rest.
titleReactNode—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.

MCPget_component("code-block")
InstallIncluded in Core and Pro

The full library index for assistants is at /llms.txt.