Code Block

Code the agent wrote, with lines that arrive in turn and a copy button that always works.

brew.tsTypeScript
export function brewRatio(coffeeGrams: number, strength = 16) {  const water = coffeeGrams * strength;  const bloom = coffeeGrams * 2;  return { water, bloom, pours: [bloom, water * 0.6, water] };} brewRatio(15); // { water: 240, bloom: 30, ... }
'use client';

import { useState } from 'react';
import { Button, CodeBlock } from '@brand-studio/ui';

const code = `export function brewRatio(coffeeGrams: number, strength = 16) {
  const water = coffeeGrams * strength;
  const bloom = coffeeGrams * 2;
  return { water, bloom, pours: [bloom, water * 0.6, water] };
}

brewRatio(15); // { water: 240, bloom: 30, ... }`;

export default function CodeBlockDemo() {
  const [run, setRun] = useState(0);
  return (
    <div style={{ width: '100%', maxWidth: 600, display: 'grid', gap: 12, justifyItems: 'end' }}>
      <div style={{ width: '100%' }}><CodeBlock key={run} code={code} filename="brew.ts" language="TypeScript" reveal lineNumbers /></div>
      <Button tone="secondary" onClick={() => setRun(value => value + 1)}>Replay</Button>
    </div>
  );
}

Installation

pnpm add @brand-studio/ui

Usage

import { CodeBlock } from '@brand-studio/ui';
<CodeBlock code={code} filename="brew.ts" language="TypeScript" reveal lineNumbers />

reveal brings the lines in one after another at linesPerSecond. To stream real output, pass the growing code without reveal; new lines appear as they arrive. Copy always takes the whole of code, even mid-stream. The block shows plain text and adds no syntax colouring.

Accessibility

  • The code area takes focus, so arrow keys scroll long lines.
  • The copy button says "Copied" in words, not only with a tick.
  • With reduced motion every line shows at once.

API Reference

PropTypeDefault
codestring
language?string
filename?string
reveal?booleanfalse
linesPerSecond?number24
lineNumbers?booleanfalse
copyLabel?string'Copy code'
className?string''

Still, Deskhand and Hollis are fictional brands. The coffee photographs are original generated artwork. @brand-studio/ui is MIT licensed.