Code Block

Highlighted code that streams in stably, with line focus and copy.

Preview

"use client";

import { RotateCcw } from "lucide-react";
import { useEffect, useRef, useState } from "react";
import { CodeBlock } from "@/components/agents/code-block";

const LINES = [
  'import { generateText } from "ai";',
  "",
  "export async function summarize(input: string) {",
  "  const { text } = await generateText({",
  '    model: "openai/gpt-5",',
  `    prompt: \`Summarize this clearly: \${input}\`,`,
  "  });",
  "",
  "  return {",
  "    text,",
  "    generatedAt: new Date().toISOString(),",
  "  };",
  "}",
];

function StreamingCodeBlock() {
  const [visibleLines, setVisibleLines] = useState(1);
  const timer = useRef<number | undefined>(undefined);
  const complete = visibleLines === LINES.length;

  useEffect(() => {
    if (visibleLines >= LINES.length) return;
    timer.current = window.setTimeout(
      () => setVisibleLines((value) => value + 1),
      260,
    );
    return () => {
      if (timer.current) window.clearTimeout(timer.current);
    };
  }, [visibleLines]);

  return (
    <CodeBlock
      filename="summarize.ts"
      language="typescript"
      code={LINES.slice(0, visibleLines).join("\n")}
      status={complete ? "complete" : "streaming"}
      highlightLines={[4, 5, 6, 7]}
      maxHeight={224}
    />
  );
}

export function CodeBlockPreview() {
  const [run, setRun] = useState(0);

  return (
    <div className="relative h-[340px] w-full max-w-xl">
      <StreamingCodeBlock key={run} />
      <button
        type="button"
        onClick={() => setRun((value) => value + 1)}
        className="absolute bottom-0 left-0 inline-flex items-center gap-1.5 rounded-full px-2 py-1 text-xs font-medium text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring"
      >
        <RotateCcw className="size-3" />
        Replay
      </button>
    </div>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/code-block

API Reference

code
string

The source code to show. May grow while `status` is "streaming".

language?
"text" | "bash" | "diff" | "json" | "tsx" | "typescript"

Language for syntax highlighting, also shown in the header. Default: "typescript"

Defaulttypescript
filename?
ReactNode

File name shown in the header.

status?
"complete" | "streaming"

"streaming" shows Writing and follows new lines, "complete" shows Ready. Default: "complete"

Defaultcomplete
showLineNumbers?
boolean

Shows a line number gutter. Default: true

Defaulttrue
highlightLines?
number[]

1-based line numbers to tint.

Default[]
maxHeight?
number

Height in px before the code scrolls. Default: 280

Default280
wrap?
boolean

Wraps long lines instead of scrolling sideways. Default: false

Defaultfalse
copyable?
boolean

Shows the copy button. Default: true

Defaulttrue
onCopy?
(() => void | Promise<void>)

Runs in place of the clipboard write when copy is pressed. Shows the copy button.

className?
string

Classes merged onto the root element.

labels?
Partial<{ writing: string; ready: string; copy: string; copied: string; }>

Override any built-in string.

Composition

Nest generated source inside the response that owns its streaming lifecycle.

StreamingResponse
└── CodeBlock

Note: File Diff explains additions and removals around highlighted code. Tool Result wraps generated code in an execution outcome. Streaming Response embeds highlighted code inside a rich answer.

How it works

Generated code changes faster than a conventional static snippet. The surface must remain readable while lines arrive, preserve syntax structure, and make copying the final result predictable.

Also on React Native. Streaming code with fixed line numbers and wrap.

Updated