---
title: "Code Block"
description: "Shows code with syntax colors, a Copy button, and line wrap. It can also number and mark lines, show a diff, hold file tabs, and show install commands."
url: https://gelatinui.com/components/code-block
markdown_url: https://gelatinui.com/components/code-block.md
access: free
install: "npx shadcn@latest add @gelatin-ui/code-block"
registry_item: https://gelatinui.com/r/code-block.json
version: "2026-11-08"
index: https://gelatinui.com/llms.txt
docs_index: https://gelatinui.com/docs/llms.txt
---

# Code Block

> Shows code with syntax colors, a Copy button, and line wrap. It can also number and mark lines, show a diff, hold file tabs, and show install commands.

Page: https://gelatinui.com/components/code-block

## When to use it

Best for: Code on a docs page or in an app, with a file name, the language, Copy, and Wrap.

- Use `CodeBlockTabs` for two or more files.
- Use `CodeBlockInstall` for one command in four package managers.
- Use `maxLines` for a long sample in an article.

## When not to use it

- For code in a sentence, use [Code](https://gelatinui.com/components/code.md).

## Notes for agents

- `palette` is `ink` by default, or `color`, or `theme` for the `--th-*` variables of a highlight theme.
- `variant` is `framed` or `plain`.
- Line marks use the shown numbers, so they follow `startLine`.
- `wrap` with `onWrapChange` lets the app keep the wrap state.

## Anatomy

```text
CodeBlock
├─ CodeBlockHeader (0..n)
├─ CodeBlockTitle (0..n)
├─ CodeBlockLanguage (0..n)
├─ CodeBlockCopyButton (0..n)
├─ CodeBlockWrapToggle (0..n)
├─ CodeBlockActions (0..n)
├─ CodeBlockFooter (0..n)
├─ CodeBlockContent (0..n)
├─ CodeBlockLine (0..n)
├─ CodeBlockStatus (0..n)
├─ CodeBlockTabs (0..n)
├─ CodeBlockTabsList (0..n)
├─ CodeBlockTab (0..n)
├─ CodeBlockTabsPanel (0..n)
└─ CodeBlockInstall (0..n)
```

## API reference

### CodeBlock

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `palette` | `"color" \| "ink" \| "theme"` |  |  |
| `variant` | `"framed" \| "plain"` |  |  |
| `wrap` | `boolean` |  | Wrap long lines (controlled). The Wrap toggle sets it. |
| `defaultWrap` | `boolean` | false | Wrap long lines at first (uncontrolled). Default `false`: code scrolls sideways. |
| `onWrapChange` | `function` |  |  |
| `streaming` | `boolean` | false | The code still arrives: an AI answer or a tool writes it now. The content shows plain text with fresh ink on the newest characters, Copy waits, and `CodeBlockStatus` says "Writing" with the line count. When it turns `false`, the syntax colors fade in once and the status says "Wrote N lines". Default `false`. |
| `interrupted` | `CodeBlockInterrupt` |  | The stream ended before the code was whole: `stopped` (a person stopped it) or `cut-off` (the reply ended inside the block, a destructive edge). A note under the code says so. |

### CodeBlockHeader

No props of its own: `className`, `children`, and every other prop go to the element it renders.

### CodeBlockTitle

No props of its own: `className`, `children`, and every other prop go to the element it renders.

### CodeBlockLanguage

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `lang` | `string` |  | The language: its short label comes from `languageLabel` (`tsx` → "TSX"). |

### CodeBlockCopyButton

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `aria-label` | `string` |  | Defines a string value that labels the current element. The name of the button. An icon-only button defaults to "Copy"; one with text uses its text. |
| `className` | `string` |  |  |
| `busy` | `boolean` |  | The button waits for the app (busy words): a spinner, `aria-busy`, and no clicks. Focus stays on the button. The style hook stays `data-loading`. |
| `spinnerPlacement` | `"center" \| "start"` |  | `center` (default): the spinner covers the label. `start`: it takes the leading icon slot. The icon sizes always use `center`, because their one icon is the label. |
| `timeout` | `number` |  | How long the result (check or X) shows, in ms. Default 2000. |
| `onCopy` | `function` |  | Runs after the clipboard has the text, with the text. |
| `onCopyError` | `function` |  | Runs when the copy fails: no Clipboard API, the browser refused, or `getValue` failed. |
| `glass` | `"clear" \| "dark" \| "light"` |  |  |
| `rounded` | `"default" \| "full"` |  |  |
| `size` | `"default" \| "icon" \| "icon-lg" \| "icon-sm" \| "icon-xs" \| "lg" \| "sm" \| "xl" \| "xs"` |  |  |
| `variant` | `"dashed" \| "default" \| "destructive" \| "destructive-outline" \| "ghost" \| "link" \| "media" \| "outline" \| "secondary"` |  |  |

Also takes every prop of Base UI `Button`: https://base-ui.com/react/components/button

### CodeBlockWrapToggle

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |

Also takes every prop of Base UI `Toggle`: https://base-ui.com/react/components/toggle

### CodeBlockActions

No props of its own: `className`, `children`, and every other prop go to the element it renders.

### CodeBlockFooter

No props of its own: `className`, `children`, and every other prop go to the element it renders.

### CodeBlockContent

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `aria-label` | `string` |  | Defines a string value that labels the current element. The name of the code region for screen readers. Default: "<Language> code". |
| `lang` | `string` |  | The language: a name or alias that `highlighter` knows (`ts`, `bash`, `md`). Default: plain text. |
| `code` | `string` |  | The source code. Copy copies it as is. While the block streams, the code so far. |
| `lineNumbers` | `boolean` | true | Show line numbers. Default `true`. |
| `startLine` | `number` | 1 | The number of the first line, for a part of a file. Default `1`. |
| `numbers` | `union` |  | Own line numbers, one entry per line, in place of `startLine`: one column, or two for a diff (`[old, new]`; `undefined` leaves a cell empty: an added line has no old number). Shown even with `lineNumbers={false}`. |
| `marks` | `Partial` |  | A mark for a line, by its shown number: `{ 4: "highlight", 7: "inserted" }`. |
| `focus` | `readonly number[]` |  | Lines in focus, by their shown number: a tint band and a 2px edge at the start. The other lines keep full contrast. |
| `words` | `readonly string[]` |  | Word marks: each match of each string. |
| `ranges` | `union` |  | Word marks by character range in `code`: `[start, end]`, end not included. |
| `prompt` | `string` |  | A prompt before each line (`$`), not selectable and not copied. |
| `maxLines` | `number` |  | Fold code longer than this many lines in a Spoiler: the first lines under a fade, and "Show all N lines" centered under the code. |
| `expanded` | `boolean` |  | Shows every line when `maxLines` applies (controlled). |
| `defaultExpanded` | `boolean` | false | Start expanded when `maxLines` applies (uncontrolled). |
| `onExpandedChange` | `function` |  | Runs when the fold trigger shows or hides the lines past `maxLines`. |
| `labels` | `CodeBlockContentLabels` |  | The words of the fold trigger (`expand`, `collapse`) and of the stream notes. |
| `reveal` | `StreamReveal` | "word" | The pace of streamed code (`#/lib/stream-pace`): `word` (default) shows bursts at an even rate on the frame clock, `off` shows the code as it comes. A part that paces its text already (Streaming Markdown) passes `off`. |
| `className` | `string` |  | Classes of the scroll box around the `<pre>`: set a height here (`max-h-96`). |

### CodeBlockLine

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `mark` | `"deleted" \| "error" \| "highlight" \| "inserted" \| "warning"` |  | The mark of the line: its tint, and for `inserted` and `deleted` an `<ins>` or `<del>` element, so a screen reader can say the change. |
| `number` | `number` |  | The shown line number (`data-line`). |
| `focused` | `boolean` | false | A line in focus: the `default` tint band and a 2px edge at the start. |
| `last` | `boolean` | false | The last line of a stream (`data-last`). |

### CodeBlockStatus

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` |  | A mark before "Writing" while the code arrives: the Agent Presence orb with the `writing` verb in the AI items (`<AiAgentPresence verb="writing" size={14} aria-hidden />`). |
| `labels` | `CodeBlockStatusLabels` |  |  |

### CodeBlockTabs

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `syncKey` | `string` |  | Share the open tab with every block that has the same key, and keep it for the next visit: `"package-manager"`, `"framework"`. Works with string tab values. A block that has no tab for the shared choice keeps its own tab. |

Also takes every prop of Base UI `Tabs.Root`: https://base-ui.com/react/components/tabs

### CodeBlockTabsList

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `rounded` | `"default" \| "full"` |  |  |
| `size` | `"default" \| "lg" \| "sm"` |  |  |

Also takes every prop of Base UI `Tabs.List`: https://base-ui.com/react/components/tabs

### CodeBlockTab

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |

Also takes every prop of Base UI `Tabs.Tab`: https://base-ui.com/react/components/tabs

### CodeBlockTabsPanel

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `gap` | `"default" \| "none"` |  | The space between the list and the panel: `default` (12px) or `none`, for a panel that sits flush under a list in a head with its own edge (the JSX Preview stage). |

Also takes every prop of Base UI `Tabs.Panel`: https://base-ui.com/react/components/tabs

### CodeBlockInstall

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `wrap` | `boolean` |  | Wrap long lines (controlled). The Wrap toggle sets it. |
| `defaultWrap` | `boolean` |  | Wrap long lines at first (uncontrolled). Default `false`: code scrolls sideways. |
| `onWrapChange` | `function` |  |  |
| `streaming` | `boolean` |  | The code still arrives: an AI answer or a tool writes it now. The content shows plain text with fresh ink on the newest characters, Copy waits, and `CodeBlockStatus` says "Writing" with the line count. When it turns `false`, the syntax colors fade in once and the status says "Wrote N lines". Default `false`. |
| `interrupted` | `CodeBlockInterrupt` |  | The stream ended before the code was whole: `stopped` (a person stopped it) or `cut-off` (the reply ended inside the block, a destructive edge). A note under the code says so. |
| `palette` | `"color" \| "ink" \| "theme"` |  |  |
| `variant` | `"framed" \| "plain"` |  |  |
| `command` | `string` |  | One npm command: `npm install @tanstack/highlight`, `npx shadcn add @gelatin-ui/code-block`. |
| `managers` | `ReadonlyArray<PackageManager>` | PACKAGE_MANAGERS | The tabs, in order. Default npm, pnpm, yarn, bun. |
| `syncKey` | `string` | "package-manager" | The shared choice (see `CodeBlockTabs`). Default `"package-manager"`. |
| `prompt` | `string` | "$" | The prompt before the command. Default `$`. |
| `label` | `string` | "Package manager" | The name of the tab list. Default "Package manager". |
| `marks` | `Partial` |  | A mark before the name of each tab, in place of the built-in one (the npm, pnpm, Yarn, or Bun logo at 14px). The name always shows with it. |
| `markTone` | `"color" \| "mono"` | "color" | The built-in marks: `color` (default, the brand colors) or `mono` (the tab text color). |
| `labels` | `Partial<{ copy: string; }>` |  | The words (`CODE_BLOCK_INSTALL_LABELS`). Keep the object stable. |
| `onCopyCommand` | `function` |  | Called after Copy wrote the command to the clipboard, with the text it copied: the command of the open tab (`pnpm dlx shadcn add …`). A failed copy does not call it. The root's own `onCopy` stays the DOM clipboard event. |

## Styling

| Part | Slot | States | CSS variables | Tokens |
| --- | --- | --- | --- | --- |
| CodeBlock | `code-block` | `data-float`, `data-focused`, `data-invalid` | `--code-block-copy-room`, `--spacing` |  |
| CodeBlockHeader | `code-block-header` | `data-part` | `--code-muted`, `--muted-foreground` |  |
| CodeBlockTitle | `code-block-title` |  |  |  |
| CodeBlockLanguage | `code-block-language` |  |  |  |
| CodeBlockCopyButton | `code-block-copy-button` |  |  |  |
| CodeBlockWrapToggle | `code-block-wrap-toggle` |  |  |  |
| CodeBlockActions | `code-block-actions` |  | `--code-background`, `--code-block-strip` |  |
| CodeBlockFooter | `code-block-footer` |  |  |  |
| CodeBlockContent | `code-block-content` | `data-token`, `data-wrap` | `--code-block-gutter`, `--code-token`, `--foreground` | `motion-timing` |
| CodeBlockLine | `code-block-line` | `data-wrap` | `--code-block-copy-room`, `--code-block-gutter`, `--spacing` | `status-colors` |
| CodeBlockStatus | `code-block-status` |  |  | `motion-timing`, `shimmer` |
| CodeBlockTabs | `code-block-tabs` |  |  |  |
| CodeBlockTabsList | `code-block-tabs-list` |  |  |  |
| CodeBlockTab | `code-block-tab` | `data-variant` |  |  |
| CodeBlockTabsPanel | `code-block-tabs-panel` |  |  |  |
| CodeBlockInstall | `code-block-install` |  |  |  |

## Usage

Put a header and the content in the root.

```tsx title="example-code.tsx"
import {
  CodeBlock,
  CodeBlockContent,
  CodeBlockCopyButton,
  CodeBlockHeader,
  CodeBlockLanguage,
  CodeBlockTitle,
} from "@/components/ui/code-block";

const CODE = `export function add(a: number, b: number) {
  return a + b;
}`;

export function ExampleCode() {
  return (
    <CodeBlock>
      <CodeBlockHeader>
        <CodeBlockTitle>add.ts</CodeBlockTitle>
        <CodeBlockLanguage lang="ts" />
        <CodeBlockCopyButton />
      </CodeBlockHeader>
      <CodeBlockContent code={CODE} lang="ts" />
    </CodeBlock>
  );
}
```

## Compose

Rules:

- Compose the parts (`compose-parts`): Build each item from its named parts as children, and pass your text and icons in as parts.
- Every state is designed (`every-state`): Give each view of data its loading, empty, and error states: a skeleton in the final layout, an `Empty` with an illustration and one next action, and an error that says what failed and how to retry.

## Add it

```bash
npx shadcn@latest add @gelatin-ui/code-block
```

It adds 78 registry items: 72 files, 12341 lines, and the npm packages `@base-ui/react`, `@mantine/hooks`, `@phosphor-icons/react`, `@tanstack/highlight`, `@xstate/react`, `clsx`, `motion`, `tailwind-merge`, `tailwind-variants`, `xstate`.

## Built on

- [Code](https://gelatinui.com/components/code.md)
- [Copy Button](https://gelatinui.com/components/copy-button.md)
- [Direction](https://gelatinui.com/components/direction.md)
- [Rolling Number](https://gelatinui.com/components/rolling-number.md)
- [Scroll Area](https://gelatinui.com/components/scroll-area.md)
- [Spoiler](https://gelatinui.com/components/spoiler.md)
- [Tabs](https://gelatinui.com/components/tabs.md)
- [Toggle](https://gelatinui.com/components/toggle.md)

## For AI agents

- Every item, one file: https://gelatinui.com/llms.txt
- MCP server (`search_items`, `get_item`, `get_install_plan`): https://gelatinui.com/mcp

## Related

- [Code](https://gelatinui.com/components/code.md)
- [Copy Button](https://gelatinui.com/components/copy-button.md)

## Keyboard

| Keys | Action |
| --- | --- |
| Tab | Moves focus to the code region when it scrolls, then to Copy and Wrap. |
| Left Arrow, Right Arrow | Scrolls the focused code sideways. |

## Accessibility

The root is a `figure`.

A code region that scrolls takes the focus, so the keys can scroll it.

The ` prompt of a terminal is not selected and not copied.
