# Code Block

Code, terminal output, logs or a diff on a pencilled box, with a header, a copy button, line numbers and marked lines, and optional syntax highlighting in the ballpoint's pens.

Docs: https://ballpoint.st9wd.com/docs/code-block

```bash
npx shadcn@latest add @ballpoint/code-block
```

```tsx
import { CodeBlock, CodeBlockActions, CodeBlockContent, CodeBlockCopyButton, CodeBlockHeader, CodeBlockTitle } from "@/components/ui/code-block"

<CodeBlock>
  <CodeBlockHeader>
    <CodeBlockTitle>Terminal</CodeBlockTitle>
    <CodeBlockActions>
      <CodeBlockCopyButton value={output} />
    </CodeBlockActions>
  </CodeBlockHeader>
  <CodeBlockContent>{output}</CodeBlockContent>
</CodeBlock>
```

Props:

- `CodeBlock variant / size`: `"default" | "destructive" / "default" | "sm"`. Failed output in red pen; a tighter block for tool output.
- `CodeBlockContent lineNumbers / startLine / wrap`: `boolean / number / boolean` (default `false / 1 / false`). Number the lines from startLine; wrap long lines instead of scrolling them.
- `CodeBlockLine variant`: `"default" | "added" | "removed" | "highlighted"` (default `"default"`). Marks a diff with + and −, or hatches the line.
- `CodeBlockCopyButton value`: `string | () => string`. The text to copy.
- `useHighlightedCode(code, language)`: `ReactNode[]`. One node a line, for CodeBlockLine, inked in the ballpoint's pens. Needs the shiki package.
- `highlightCode(code, language)`: `Promise<ReactNode[]>`. The same, awaited: it works in a Server Component.
- `seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `passes`, `radius`, `corners`, `weight`, `speed` (see Pen settings).

## Example

```tsx
import { CodeBlock, CodeBlockActions, CodeBlockContent, CodeBlockCopyButton, CodeBlockHeader, CodeBlockTitle } from "@/components/ui/code-block";
import { InkIcon } from "@/components/ui/ink-icons";

const output = `$ pnpm build
> ballpoint@0.0.0 build
✓ Compiled successfully
✓ Generating static pages (72/72)

Route (app)
┌ ○ /
├ ○ /docs
└ ● /docs/[name]`;

export default function CodeBlockDemo() {
  return (
    <CodeBlock seed="cb-demo" className="max-w-lg">
      <CodeBlockHeader>
        <CodeBlockTitle>
          <InkIcon name="terminal" /> Terminal
        </CodeBlockTitle>
        <CodeBlockActions>
          <CodeBlockCopyButton value={output} />
        </CodeBlockActions>
      </CodeBlockHeader>
      <CodeBlockContent>{output}</CodeBlockContent>
    </CodeBlock>
  );
}
```

## Lines

Numbers, a diff with + and −, a highlighted line, and a block that wraps.

```tsx
import { CodeBlock, CodeBlockContent, CodeBlockHeader, CodeBlockLine, CodeBlockTitle } from "@/components/ui/code-block";

export default function CodeBlockLines() {
  return (
    <div className="flex w-full max-w-lg flex-col gap-6">
      <CodeBlock seed="cbl-a">
        <CodeBlockHeader>
          <CodeBlockTitle>button.tsx</CodeBlockTitle>
        </CodeBlockHeader>
        <CodeBlockContent lineNumbers startLine={12}>
          <CodeBlockLine>{"function Button({ variant }) {"}</CodeBlockLine>
          <CodeBlockLine variant="removed">{'  const pen = "plain"'}</CodeBlockLine>
          <CodeBlockLine variant="added">{"  const pen = usePen({ roughness })"}</CodeBlockLine>
          <CodeBlockLine variant="highlighted">{"  return <ButtonInk pen={pen} />"}</CodeBlockLine>
          <CodeBlockLine>{"}"}</CodeBlockLine>
        </CodeBlockContent>
      </CodeBlock>
      <CodeBlock seed="cbl-b" size="sm">
        <CodeBlockContent wrap>{"A long line of output that would run off the edge goes round onto the next line instead, because this block wraps."}</CodeBlockContent>
      </CodeBlock>
    </div>
  );
}
```

## Highlighted

useHighlightedCode inks the lines; they appear plain until the highlighter has loaded.

```tsx
"use client";

import { CodeBlock, CodeBlockActions, CodeBlockContent, CodeBlockCopyButton, CodeBlockHeader, CodeBlockLine, CodeBlockTitle } from "@/components/ui/code-block";
import { useHighlightedCode } from "@/hooks/use-highlighted-code";

const code = `import { Button } from "@/components/ui/button"

export function Actions() {
  // Two buttons, one drawn lighter.
  return (
    <>
      <Button>Book a call</Button>
      <Button variant="outline">Copy email</Button>
    </>
  )
}`;

export default function CodeBlockHighlight() {
  const lines = useHighlightedCode(code, "tsx");
  return (
    <CodeBlock seed="cbh" className="max-w-lg">
      <CodeBlockHeader>
        <CodeBlockTitle>actions.tsx</CodeBlockTitle>
        <CodeBlockActions>
          <CodeBlockCopyButton value={code} />
        </CodeBlockActions>
      </CodeBlockHeader>
      <CodeBlockContent lineNumbers>
        {lines.map((line, i) => (
          <CodeBlockLine key={i}>{line}</CodeBlockLine>
        ))}
      </CodeBlockContent>
    </CodeBlock>
  );
}
```

## Failed output

The destructive variant, with a footer.

```tsx
import { CodeBlock, CodeBlockContent, CodeBlockFooter, CodeBlockHeader, CodeBlockTitle } from "@/components/ui/code-block";
import { Button } from "@/components/ui/button";

const log = `Error: ENOENT: no such file or directory, open 'registry.json'
    at readFileSync (node:fs:441:20)
    at build (scripts/build-registry.ts:48:12)`;

export default function CodeBlockError() {
  return (
    <CodeBlock variant="destructive" seed="cbe" className="max-w-lg">
      <CodeBlockHeader>
        <CodeBlockTitle>Build failed</CodeBlockTitle>
      </CodeBlockHeader>
      <CodeBlockContent>{log}</CodeBlockContent>
      <CodeBlockFooter>
        <Button variant="outline" size="xs" seed="cbe-more">
          Show the full log
        </Button>
      </CodeBlockFooter>
    </CodeBlock>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `corners`: `"crossed" | "joined"` (default `"crossed"`). Square corners: sides pulled separately past each other, or the box drawn in one motion.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
