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
- 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-blockPaste this into your coding agent (Claude Code, Cursor, Codex). It checks your project, sets up the registry, runs the install and verifies it.
Install Code Block from Knowns UI into this project. Work step by step, show me each change, and ask before you edit files outside this project.
Context: Knowns UI (https://ui.knowns.dev) is a shadcn registry. Items land in the project as source files. Docs: https://ui.knowns.dev/docs/code-block
1. Check the project first. Stop and tell me if any of these is missing:
- React 19 and Tailwind CSS v4.
- A components.json at the project root. If there is none, ask me before running `npx knowns-ui@latest init --no-key` (it runs shadcn init when needed, then adds the registries).
2. Run:
```bash
npx knowns-ui@latest add code-block
```
It writes these files:
- lib/knowns/run.ts
- lib/knowns/json.tsx
- lib/knowns/status.ts
- lib/knowns/tone.ts
- lib/knowns/types.ts
- lib/knowns/use-abortable.ts
- lib/knowns/utils.ts
- lib/knowns/motion.tsx
- lib/knowns/motion-utils.ts
- components/ui/knowns/code-block/code-block.tsx
- components/ui/knowns/code-block/code-block-utils.ts
and installs these npm packages: clsx@^2.1.1, tailwind-merge@^3.7.0.
3. While it runs:
- It also adds a few @keyframes (all named kn-*) to the global CSS file (globals.css or the file components.json points at). Let it: they only add animations.
- If it asks to overwrite files under lib/knowns/: these are shared by every Knowns UI item. List them, say whether I changed them (check git), and ask me before you answer.
- If it asks to overwrite any other file, show me the diff and ask me.
4. Verify:
- Every file in the list above exists.
- The project type-checks (for example `npx tsc --noEmit`) and the dev build has no new errors.
5. Use it: follow the examples and props on https://ui.knowns.dev/docs/code-block, and ask me if anything about my data is unclear.
If the Knowns UI MCP server is connected (https://ui.knowns.dev/docs/ai-and-mcp), use get_component({ name: "code-block" }) or search_docs for details.
Finish with a short summary: the files you added or changed, the packages you installed, and anything I still need to do.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- Default
false - 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- Default
false - Soft-wrap long lines. The Wrap toggle in the header is a pressed button.
- wrapToggle
boolean- Default
true - 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- Default
false - Start unfolded.
- copy / onCopy
boolean / (code) => void- Default
true - 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.
| Prop | Type | Default | Description |
|---|---|---|---|
| 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 | false | 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 | false | Soft-wrap long lines. The Wrap toggle in the header is a pressed button. |
| wrapToggle | boolean | true | 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 | false | Start unfolded. |
| copy / onCopy | boolean / (code) => void | true | 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
- 01Colours the code with a built-in tokenizer that has no dependency, or with your own `highlight` function that returns tokens per line.
- 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`.
- 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.
- 04Wraps long lines with a toggle or the `wrap` prop, and the scrolling code region takes focus so long lines are reachable by keyboard.
- 05Folds output longer than `collapseAfter` behind a "Show N more lines" button, and `animateFold` opens it with a height transition.
- 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
| State | What moves | Timing |
|---|---|---|
| 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" }).