Skip to content
Ballpoint

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.

From shadcn/ui
Terminal
$ pnpm build> ballpoint@0.0.0 build✓ Compiled successfully✓ Generating static pages (72/72)Route (app)┌ ○ /├ ○ /docs└ ● /docs/[name]

Installation

pnpm dlx shadcn@latest add @ballpoint/code-block

Usage

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>

Examples

Lines

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

button.tsx
function Button({ variant }) {  const pen = "plain"  const pen = usePen({ roughness })  return <ButtonInk pen={pen} />}
A long line of output that would run off the edge goes round onto the next line instead, because this block wraps.

Highlighted

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

actions.tsx
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>    </>  )}

Failed output

The destructive variant, with a footer.

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

API reference

CodeBlock variant / size
"default" | "destructive" / "default" | "sm"

Failed output in red pen; a tighter block for tool output.

CodeBlockContent lineNumbers / startLine / wrap
default false / 1 / false
boolean / number / boolean

Number the lines from startLine; wrap long lines instead of scrolling them.

CodeBlockLine variant
default "default"
"default" | "added" | "removed" | "highlighted"

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

The drawing also takes draw, roughness, passes, radius, corners, weight and speed, as props or from the nearest InkProvider. What each one does.