---
title: "File Diff"
description: "A syntax-highlighted file change disclosure with progressive rows, line numbers, live change counts, smooth following, and completion collapse."
category: "AI Agents"
publishedAt: "2026-08-01"
updatedAt: "2026-11-25"
documentation: "https://beui.dev/components/agents/file-diff"
markdown: "https://beui.dev/components/agents/file-diff.md"
license: "MIT"
---

# File Diff

> A syntax-highlighted file change disclosure with progressive rows, line numbers, live change counts, smooth following, and completion collapse.

## Install

```bash
npx shadcn@latest add @beui/file-diff
```

## Dependencies

- `@floating-ui/dom`
- `clsx`
- `lucide-react`
- `motion`
- `react`
- `react-dom`
- `shiki`
- `tailwind-merge`

## Usage

```tsx
"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>
  );
}
```

## API Reference

### FileDiff

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `file` | `ReactNode` |  | Yes | File name or path shown in the header. |
| `lines` | `FileDiffLine[]` |  | Yes | Diff lines, each added, removed or context, with optional line numbers. |
| `status` | `"streaming" \| "complete"` | `streaming` | No | "streaming" while lines arrive, "complete" once the edit lands. Default: "streaming" |
| `open` | `boolean` |  | No | Whether the diff is expanded, for controlled use. |
| `defaultOpen` | `boolean` | `true` | No | Whether the diff starts expanded when uncontrolled. Default: true |
| `onOpenChange` | `((open: boolean) => void)` |  | No | Called with the new state when the diff expands or collapses. |
| `collapseOnComplete` | `boolean` | `true` | No | Fold the diff when status moves from streaming to complete. Default: true |
| `maxHeight` | `number` | `220` | No | Tallest the line viewport grows before it scrolls (px). Default: 220 |
| `language` | `"text" \| "bash" \| "diff" \| "json" \| "tsx" \| "typescript"` | `typescript` | No | Language used for syntax highlighting. Default: "typescript" |
| `copyText` | `string` |  | No | Text the copy button writes to the clipboard. Shows the button when set. |
| `onCopy` | `(() => void \| Promise<void>)` |  | No | Runs instead of the clipboard write when the copy button is pressed. |
| `labels` | `Partial<{ applying: string; applied: string; changes: string; copy: string; copied: string; }>` |  | No | Override any built-in string. |
| `className` | `string` |  | No | Classes merged onto the root element. |

## Types

### FileDiffLine

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | Yes | Stable key, so streamed lines keep their place. |
| `type` | `FileDiffLineType` | No | Marks the line added (green, +), removed (red, −) or unchanged. Default: "context" |
| `oldLine` | `number` | No | Line number in the old file, shown in the first gutter. Blank when not set. |
| `newLine` | `number` | No | Line number in the new file, shown in the second gutter. Blank when not set. |
| `content` | `string` | Yes | The line's code, without the + or − marker. Syntax highlighted. |

## Composition

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

```text
ToolResult
└── ToolResultOutput
    └── FileDiff
```

- `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.

- **Open the change.** Filename, language, and live counts establish the scope of the edit.
- **Append rows.** Context, additions, and removals arrive without reanimating earlier lines.
- **Reduce to summary.** Completion collapses the patch while keeping the full change one action away.

### What it owns and what it leaves to you

- **Owns:** Diff rows, old and new line numbers, syntax presentation, change counts, and completion disclosure.
- **Leaves to the app:** Diff generation, patch application, conflict handling, and repository state to the application.

### Principles

- **Keep both line spaces.** Old and new line numbers remain distinct so additions, removals, and unchanged context can be discussed precisely.
- **Stream by row.** New diff rows arrive as stable units. Existing syntax and backgrounds do not reanimate when another row is appended.
- **Collapse to the result.** After completion, the disclosure can reduce to the filename and change counts while preserving access to the full patch.

### Use it when

A person needs to inspect how an agent changed a file, including old and new line relationships.

### Avoid it when

The content is a standalone snippet, a complete file, or raw terminal output with no before-and-after structure.

## About File Diff

File Diff is a React component for showing the file edits an AI agent makes, with syntax-highlighted rows, separate old and new line numbers, and live addition and deletion counts in its header. Rows stream in as you append them to the lines array, the viewport follows new rows inside a capped height, and the panel collapses to a one-line summary when the status turns complete. It is built with Framer Motion and Tailwind CSS.

### When to use it

- A coding agent proposes an edit and the user needs to see exactly which lines change before accepting it.
- A chat transcript keeps completed file changes as compact summaries that can be reopened later.
- A code review or changelog view shows small patches inline next to the discussion about them.

### Accessibility

The header is a real button with aria-expanded and aria-controls, and the patch is a labelled region that is hidden from assistive technology and made inert while collapsed, so its rows cannot be tabbed into. The diff viewport is a polite live region, the root sets aria-busy while streaming, and the status icon is labelled "Applying changes" or "Changes applied". The optional copy button is named "Copy diff" and switches to "Copied". With reduced motion enabled, the spinner stops, following new rows jumps instead of smooth scrolling, and the panel opens and closes instantly.

### FAQ

**How do I show a code diff from an AI agent in React?**

Render FileDiff with a file name and a lines array. Each line has an id and content, plus an optional type of added, removed or context and optional oldLine and newLine numbers. Set status to "streaming" while rows arrive and "complete" when the edit is done.

**Can I keep the diff open after it finishes?**

Yes. Set collapseOnComplete to false, or control the panel with open and onOpenChange. defaultOpen sets the starting state when it is uncontrolled, and the panel reopens if status goes back to "streaming".

**How do I add a copy button to the diff?**

Pass copyText to copy that string to the clipboard, or onCopy to handle copying yourself. Either one shows the copy button. Use maxHeight to change how tall the viewport grows before it scrolls.

## React Native

A React Native version ships in beUI Native. The detail JSON has every file to copy.

```bash
npx expo install prism-react-renderer react-native-reanimated react-native-svg react-native-worklets
npm install uniwind tailwindcss
```

- Native detail: https://beui.dev/native/r/file-diff
- GitHub: https://github.com/starc007/beui-native/tree/main/src/components/file-diff

## Source

- Registry detail: https://beui.dev/r/file-diff
- Raw source: https://beui.dev/r/file-diff/raw
- GitHub: https://github.com/starc007/ui-components
