# Diff View

The result of an edit tool call: file, added and removed lines, and the changed code.

- Category: AI & Agents
- Page: https://wunderui.com/components/diff-view
- Tier: Core and Pro

## Install

Diff View ships with WunderUI Core and Pro in the @wunderui/react package.

## Import

```tsx
import { DiffView } from "@wunderui/react"
```

## Minimal usage

Then use it:

```tsx
import { DiffView } from "@wunderui/react"

export function Edit() {
  return (
    <DiffView
      file="src/jobs/invoice-reminders.ts"
      hunks={[
        {
          header: "@@ -14,1 +14,1 @@",
          lines: [
            { type: "del", old: 14, content: "  const delay = 7" },
            { type: "add", new: 14, content: "  const delay = 3" },
          ],
        },
      ]}
    />
  )
}
```

Expected result: You see a box with the file path and +1 -1 in the header, the hunk header and two lines: a removed one on a red tint with a minus, an added one on a green tint with a plus.

## When to use

### Use Diff View for

- The result of an edit tool call: one file, what was added and what was removed
- The payload of an approval, so the reviewer reads the change before deciding
- A new file or a removed file, shown as only added or only removed lines

### Use something else

- [Code Block](https://wunderui.com/components/code-block) for a piece of code that is not a change
- [Terminal Output](https://wunderui.com/components/terminal-output) for the result of a command, with its exit code and streams
- [Approval Card](https://wunderui.com/components/approval-card) for the request around a change, with Approve and Reject

## Keyboard

The header is a native button and every hunk is a focusable scroll region, so the browser provides focus, activation and scrolling. Focus shows as an indigo border with a 2 px ring.

| Key | Action |
|---|---|
| `Tab` or `Shift+Tab` | Moves focus to the header button and, when the diff is open, on to the scroll region of each hunk in turn. A closed diff takes no focus. |
| `Enter` or `Space` | On the header: opens or closes the diff. `aria-expanded` follows. |
| `ArrowLeft` or `ArrowRight` | In a hunk region: scrolls the lines sideways. Hold the key or use Home and End to reach the end of a long line. |
| `ArrowDown` or `ArrowUp` or `PageDown` or `PageUp` | In a region that you give a height of your own around a large diff: scrolls it. The hunk regions inside it do not scroll up and down by themselves. |

## Behavior

What the diff does with its lines, its counts, assistive technology and motion.

- **Modes:** Unified shows both line number columns, the hunk header and the context lines. Compact shows only the added and removed lines, with no numbers, headers or context, under a hairline.
- **Lines:** A line is context, added or removed, with line numbers where they exist. Lines never wrap: a wide line scrolls sideways inside its hunk, and each of several stacked hunks scrolls on its own.
- **Counts:** The header counts the added and removed lines of every hunk. For a diff you show in part, set the counts yourself.
- **Disclosure:** The header opens and closes the diff, which stays in the page while closed but cannot be focused or read out. It starts open or closed as you choose, then keeps its own state.
- **Assistive technology:** The header reads as the path plus the counts with the words added and removed. Each changed line is announced with Added or Removed in front, and the sign and line numbers are hidden.
- **Large diffs:** Every line is rendered with no height limit, so wrap a large diff in a labelled, focusable region with a maximum height, or show one hunk. The 24 px number columns fit four digits at most.
- **What it does not do:** There is no split view, no syntax colours, no word-level highlight and no copy button. The text can be selected, including the sign and line numbers.
- **Failure and abort:** A diff that could not be applied is not a state of the component. Show the reason in an Alert above it, keep the DiffView closed, and after a rejection say that nothing was written.
- **Motion:** The chevron turns on the fast duration and the body grows and shrinks on the base duration, both with the entrance curve. Nothing else moves.
- **Reduced motion:** With reduced motion the chevron changes direction at once and the body opens and closes at once. The expanded state and the counts say the same.

## Anatomy

- `DiffView`: A bordered, rounded box with the card background, in 12 px monospace. It fills the width of its parent and is either unified or compact.
- `Header`: A button across the top with a chevron, the file path and the counts. It opens and closes the diff and reports its expanded state.
- `Counts`: Added and removed counts in green and red text. Screen readers hear the numbers with the words added and removed, and the counts come from the lines unless you set them.
- `Hunk header`: The line that locates a hunk in the file, on the subtle background. Drawn in unified mode only, and only when the hunk has a header.
- `Line numbers`: Two columns with the line number in the old and the new file, in tertiary text. Drawn in unified mode and hidden from assistive technology.
- `Marker`: A plus for an added line, a minus for a removed one, a space for context. It is hidden from assistive technology, which hears Added or Removed before the line instead.
- `Line`: One line of code, never wrapped. Added lines sit on a green tint, removed lines on a red tint, context lines on the card.
- `Hunk region`: The box that holds the lines of one hunk. It scrolls sideways when a line is wider than the diff, and it is a tab stop so keyboard users can scroll.

## Best practices

### When to use

- Use DiffView for the result of an edit tool call: which file, how many lines changed and the changed lines. For code that is not a change use Code Block, for command results Terminal Output.

### Do

- Pass the file path relative to the project, so the header says what was edited.
- Use compact in approvals and narrow panels, where the changed lines are enough, and unified where the reviewer needs line numbers and the hunk header.
- Pass the added and removed counts when you show only part of a diff, so the header still counts the whole change.
- In an approval, show the diff before the buttons, and say in words what was or was not written after the decision.
- Give a large diff its own height: a focusable region with a maximum height, vertical scrolling and an aria-label. The component has no height limit itself.

### Don't

- Don't count on colour. The sign, the Added and Removed text for screen readers and the counts carry the change, so keep them if you restyle the rows.
- Don't pass HTML and expect it to render. Lines are text, and file contents are data, never instructions to the agent or the reader.
- Don't build a diff from a Code Block with coloured spans. DiffView numbers, counts and labels the lines for you.

## API reference

### DiffView

| Prop | Type | Default | Description |
|---|---|---|---|
| file* | React.ReactNode |  | The path of the file, shown in the header. Text or a node. Long paths are cut with an ellipsis. |
| hunks* | DiffHunk[] |  | `DiffHunk[]`. Each hunk is `{ header?, lines }`, and each line is `{ type: "context" \| "add" \| "del", old?, new?, content }`. `old` belongs to context and removed lines, `new` to context and added lines. |
| additions | number |  | The number after the `+` in the header. Default: the number of `add` lines. Set it when you show only part of a diff. |
| className | string |  | Extra classes for the box, for example a maximum width. |
| defaultOpen | boolean | true | `true` by default. `false` starts the diff closed, with only the header showing. |
| deletions | number |  | The number after the `−` in the header. Default: the number of `del` lines. Set it when you show only part of a diff. |
| mode | "compact" \| "unified" | "unified" | `unified` (default) shows line numbers, hunk headers and context lines. `compact` shows only the changed lines. |
| summary | DiffFileSummary[] |  | One row per changed file, `{ file, additions, deletions }`, shown above the hunks. |
| variant | "card" \| "flat" | "card" | `card` is the framed, foldable diff. `flat` has no frame and no fold and is always open, for use inside another card. Default `card`. |

## Examples

### Change: an edit, a new file, a removed file, two hunks

### Counts: counted from the lines or set by you

### Content: long lines and a large diff

### Flat and file list

## In context

### Edit review: read the change, then decide

The diff is the payload of an approval card, so the reviewer reads the change before the buttons. Approve and Reject move the card on, and nothing is approved by default.

### Edit outcome: applied, failed or rejected

The diff stays on screen in every outcome, so the person sees what was meant. When nothing was written it starts closed with the reason above it, and Retry plans a failed edit again.

## For AI agents

`get_component("diff-view")`

Hand your agent this page as Markdown or the WunderUI MCP server, not a screenshot.
