Skip to content

Code Block

Code for chat answers, docs and agent output: syntax colours without a highlighter dependency, a copy button, filename and language, line numbers, highlighted lines, wrapping and a fold for long output.

On this page
Used for
  • Tool call output
  • Install commands
  • Streaming logs
  • Config snippets
  • Diff review

Examples

Commands

Long output, folded

Diff

Bring your own highlighter

Installation

npx knowns-ui@latest add code-block
For Claude Code, Cursor or Codex: installs it and wires the props to your own data.

Props

code
string
The code. Leading blank lines and trailing whitespace are trimmed (trim={false} keeps them).
language
string
tsx, ts, js, json, bash, css, html, python, yaml or diff are coloured. Any other name is shown in the header and left plain.
filename
string
Shown in the header, in mono.
lineNumbers
boolean
Defaultfalse
A gutter that is not selectable, not copied and not read out.
highlightLines
number | number[] | string
Lines to mark: 3, [1, 4] or "1,3-5" ("8-" runs to the end). A left bar and tint, and "Highlighted:" for screen readers.
wrap / defaultWrap / onWrapChange
boolean
Defaultfalse
Soft-wrap long lines. The Wrap toggle in the header is a pressed button.
wrapToggle
boolean
Defaulttrue
Show the Wrap toggle.
collapseAfter
number
Fold longer code behind "Show N more lines" (aria-expanded). A block one or two lines over the limit stays open.
defaultExpanded
boolean
Defaultfalse
Start unfolded.
copy / onCopy
boolean / (code) => void
Defaulttrue
The Copy button copies the whole code, even folded, without line numbers. It announces "Code copied".
highlight
(code, language) => CodeLines
Your own highlighter: tokens per line. See the Shiki example above.
icon
ReactNode
Before the filename.
aria-label
string
Name of the scrollable code region when there is no filename.
Keyboard
-
The code region is a tab stop, so a long line scrolls with the arrow keys. Wrap, Copy and Show more are buttons.

The built-in colours come from a small tokenizer (3.0 kB gzipped, 7.7 kB minified, no dependency; the whole block is 4.9 kB gzipped without React). It runs synchronously, so the block renders the same in a server component, in the browser and on Cloudflare Workers. For grammar-exact colours in other languages, pass a highlighter through highlight.

What it handles

  1. 01Colours the code with a built-in tokenizer that has no dependency, or with your own `highlight` function that returns tokens per line.
  2. 02Shows a header with `filename`, a language label or your own `label`, a copy button that reads "Copied to clipboard" after a press, and `onCopy`.
  3. 03Marks lines with `highlightLines` as a number, an array or a spec such as "1,3-5", and adds a gutter with `lineNumbers` that is never copied.
  4. 04Wraps long lines with a toggle or the `wrap` prop, and the scrolling code region takes focus so long lines are reachable by keyboard.
  5. 05Folds output longer than `collapseAfter` behind a "Show N more lines" button, and `animateFold` opens it with a height transition.
  6. 06Caps the height with `maxHeight` and with `follow` keeps the last line in view as streaming output grows, until the reader scrolls up.

States

First render
Nothing. It paints as it is
none
Copy pressed
The icon becomes a check for a moment
none
Toolbar button hover
Background and text colour change
120ms, ease out
Fold with animateFold
The extra lines open and close with a height change
180ms, ease out
Fold without animateFold
The lines swap at once
none
Reduced motion
The fold and the chevron land at once
0ms

Works with

When to use

When to use

  • Code or a command in a chat answer, a docs page or an agent's output, where it should be copied.
  • Long tool output that folds to a few lines, with line numbers or a highlighted line to point at.

When not to use

  • A word or path inside a sentence: use Kbd.
  • A tool call with its arguments and result: use Tool Call Card.

Using with AI

Use Copy page for AI above to give your assistant this page, Copy prompt under the install command to have it installed and wired to your data, or connect the Knowns UI MCP server and just ask:

  • Add the Knowns UI Code Block to my app and wire it to my own data.
  • I need code or a command in a chat answer, a docs page or an agent's output, where it should be copied. Set that up with the Knowns UI Code Block.
  • Which Code Block props do I need for a minimal setup? Show me a short example.

With the server connected, your assistant calls get_component({ name: "code-block" }) and search_docs({ query: "code block" }).

Need help?

Stuck on Code Block, or found something off? We read every message.