---
agent:
  instructions: "Search goop.so for Goop's components, templates, design skill, docs, pricing and terms. Ask in keywords or a plain question, such as \"date picker props\" or \"is the design skill free\"; the answer is the matching page sections in Markdown, each with its address."
  endpoint: "https://goop.so/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Code block

A block of code or a command with a copy button, for React and Tailwind CSS. Part of Goop Pro.

Pro. Its export: `CodeBlock`.

## Try it

Pick a command, a file, a tall file or a long line; turn the title bar, line numbers, wrapping, highlighted lines and a height limit on and off; copy the code and paste it anywhere.

The code is our own sample, its colours a stand-in for your own highlighter's: nothing runs, and only the plain code reaches your clipboard.

## When to use

- A block of code or a command with a copy button.
- Also called: code snippet or syntax highlighting.

## When not to use

- For a word of code use InlineCode.

## Installation

Paste the prompt below into your coding agent. It reads Goop's install steps, installs the part and its tokens once, then reuses this same Code block on every screen instead of drawing a new one: the same height, the same focus ring, the same spacing. As a Pro part, its command comes from Goop's server once you sign in, with a one-time link that lasts ten minutes.

```sh
npx shadcn@4.23.1 add https://registry.goop.so/g/<link>/trail-code-block.json --yes
```

```text
Add the Goop Code block to this project. The install steps are at https://goop.so/docs/install.md
```

## Usage

```tsx
import { CodeBlock } from '@/components/ui/goop/code-block';

function Snippets() {
  const code = "export const ledger = {\n  currency: 'EUR',\n};\n";
  // your highlighter's tokens for `code`, one list per line (made on your server; written out here)
  const tokens = [
    [{ content: 'export const', color: 'var(--code-keyword)' }, { content: ' ledger = {' }],
    [{ content: '  currency: ' }, { content: "'EUR'", color: 'var(--code-string)' }, { content: ',' }],
    [{ content: '};' }],
  ];
  return (
    <div className="flex flex-col gap-4">
      <CodeBlock code="ledger serve --port 4400" language="bash" />
      <CodeBlock
        title="ledger.config.ts"
        language="ts"
        lineNumbers
        code={code}
        highlightedLines={tokens.map((line) =>
          line.map((token, index) => (
            <span key={index} style={{ color: token.color }}>
              {token.content}
            </span>
          )),
        )}
      />
    </div>
  );
}
```

- Fits: chat, settings (their recipes).
- Also known as: CodeSnippet, Snippet, Pre, CodeSample, CodePreview.
- Use a CodeBlock for any code or command a person reads or copies whole — an install line, a config file, an API call; a word of code inside a sentence is an InlineCode, and a single value to paste (a key, a link) is a read-only TextInput with a CopyButton at its end.
- height, width, padding, gap, corner, border, background, text: add no class for these.

## API reference

### Options

- `code?: string` — The code, exactly as it is copied: its spaces, tabs and line breaks kept.
- `children?: string` — The code as text, when `code` is not given.
- `language?: string` — The code's language (`ts`, `bash`): kept as the block's `data-language` and the `language-<name>` class on its `code` element, the seam a highlighter reads.
- `highlightedLines?: readonly ReactNode[]` — The code's lines already coloured by your own highlighter, one node per line of `code`, in order (its tokens as spans with their colours): each is drawn in place of its plain line, with the numbers, the wrap, the fades and the copy button working as before.
- `title?: string` — A name for the block, shown in a bar on top (a filename): it also names the body for a screen reader.
- `lineNumbers?: boolean = false` — A gutter of line numbers, never selected or copied with the code.
- `wrap?: boolean = false` — Long lines wrap inside the block; left out, they scroll sideways inside it (never the page).
- `maxHeight?: 'sm' | 'md' | 'lg'` — How tall the block may grow before it scrolls inside: `sm` 200 px, `md` 300 px, `lg` 500 px.
- `copyable?: boolean = true` — The copy button: it copies exactly `code`.
- `onCopy?: (code: string) => void` — Called with the code once it is on the clipboard (never when the copy failed).
- `copyLabel?: string = 'Copy code'` — The copy button's spoken name.
- `copiedLabel?: string` — The word the copy button shows once the code is copied; `Copied` by default.
- `failedLabel?: string` — The word the copy button shows when the copy failed; `Couldn’t copy` by default.

## Keyboard

- Tab: reach the copy button and, while it scrolls, the body, in the block's order: under a title bar the button first; with no bar the body first, the button beside its first line after it
- Enter: on the copy button: copy the code; the focus stays on the button
- Space: on the copy button: copy the code; the focus stays on the button
- ArrowLeft ArrowRight ArrowUp ArrowDown: on the scrolling body: scroll the code

## Accessibility

- The part itself answers the keys above.
- focus-visible: the browser's own (:focus-visible): write nothing

## Notes for AI

Give your coding agent this page as text instead of screenshots: the parts, the options, the states and the keys. An agent that reads the real part builds the real part, not a lookalike.

Shortened to fit: the data-slot values.

## Related

- [Inline code](/themes/trail/components/inline-code): A word or short phrase of code inside running text, such as a key or a path.
- [Prose](/themes/trail/components/prose): A block of running text set for reading, one paragraph or several, for a description, an article or a help page.
- [Shortcut key](/themes/trail/components/shortcut-key): One keyboard key drawn as a small bordered cap inline in hint text, naming a chord with its neighbours.
- [Feature grid](/themes/trail/components/feature-grid): A grid of feature cells, each an icon, a title and a line, for a marketing or a pricing page that lists what a product does.
