File Diff

A collapsible file diff that streams rows in with live change counts.

Preview

"use client";

import { RotateCcw } from "lucide-react";
import { useState } from "react";
import {
  FileDiff,
  type FileDiffLine,
} from "@/components/agents/file-diff";
import { useToolResultDemo } from "./use-tool-result-demo";

const DIFF_LINES: FileDiffLine[] = [
  { id: "1", oldLine: 18, newLine: 18, content: "export async function runTask() {" },
  {
    id: "2",
    type: "removed",
    oldLine: 19,
    content: "  return execute(task);",
  },
  {
    id: "3",
    type: "added",
    newLine: 19,
    content: "  const result = await execute(task);",
  },
  {
    id: "4",
    type: "added",
    newLine: 20,
    content: "  return normalize(result);",
  },
  { id: "5", oldLine: 20, newLine: 21, content: "}" },
];

function FileRun() {
  const { visible, status } = useToolResultDemo(DIFF_LINES.length, 360);

  return (
    <FileDiff
      file="src/runner.ts"
      lines={DIFF_LINES.slice(0, visible)}
      status={status === "success" ? "complete" : "streaming"}
      copyText={DIFF_LINES.map((line) => line.content).join("\n")}
      maxHeight={150}
    />
  );
}

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

  return (
    <div className="relative h-[300px] w-full max-w-lg">
      <FileRun 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.5 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/file-diff

API Reference

file
ReactNode

File name or path shown in the header.

lines
FileDiffLine[]

Diff lines, each added, removed or context, with optional line numbers.

status?
"complete" | "streaming"

"streaming" while lines arrive, "complete" once the edit lands. Default: "streaming"

Defaultstreaming
open?
boolean

Whether the diff is expanded, for controlled use.

defaultOpen?
boolean

Whether the diff starts expanded when uncontrolled. Default: true

Defaulttrue
onOpenChange?
((open: boolean) => void)

Called with the new state when the diff expands or collapses.

collapseOnComplete?
boolean

Fold the diff when status moves from streaming to complete. Default: true

Defaulttrue
maxHeight?
number

Tallest the line viewport grows before it scrolls (px). Default: 220

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

Language used for syntax highlighting. Default: "typescript"

Defaulttypescript
copyText?
string

Text the copy button writes to the clipboard. Shows the button when set.

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

Runs instead of the clipboard write when the copy button is pressed.

labels?
Partial<{ applying: string; applied: string; changes: string; copy: string; copied: string; }>

Override any built-in string.

className?
string

Classes merged onto the root element.

Composition

Place the patch inside the tool output that produced the file change.

ToolResult
└── ToolResultOutput
    └── FileDiff

Note: Code Block provides the shared syntax presentation for source lines. Tool Result summarizes the command or edit that produced the patch. Agent Activity records the file edit among surrounding agent events.

How it works

A file diff explains a proposed or completed edit. It should preserve the relationship between old and new lines while making the scale and status of the change visible at a glance.

Also on React Native. Changes with live counts and folded context.

Updated