# Diff review

> Accept or reject what the AI changed, word by word for prose or line by line for code.

When an AI edits your work, you should see exactly what it touched and keep the final say. Give it each change’s before and after: it works out the difference itself, striking removed words in red and marking added ones in green. Accepting doesn’t swap the diff for the result, the paragraph resolves in place: removed words shrink out of the sentence and the added ones lose their green, while Accept morphs into "Accepted" with a check that draws itself. Rejecting does the reverse. Accept or reject one at a time or all at once, undo either, watch the count roll, and get a single callback when nothing is left to review.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/diff-review.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `number-roll`, `text-morph`
- Docs and live preview: https://hairlineui.com/ui/diff-review
- Registry item (source): https://hairlineui.com/r/diff-review.json

## Usage

```tsx
import { DiffReview } from "@/components/diff-review";

<DiffReview
  changes={[{ id: "intro", label: "Paragraph 1", before: draft, after: edited }]}
  onComplete={(decisions) => apply(decisions)}
/>
```

## With the AI SDK

```tsx
// Server: a tool with no execute; the user's decisions become its output
import { tool } from "ai";
import { z } from "zod";

const suggestEdits = tool({
  description: "Propose edits to the user's document for them to review",
  inputSchema: z.object({
    changes: z.array(z.object({ id: z.string(), label: z.string(), before: z.string(), after: z.string() })),
  }),
});

// Client: in your message list. Set sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls
// on useChat so the model continues once the decisions are sent.
const { addToolOutput } = useChat();

if (part.type === "tool-suggestEdits" && (part.state === "input-available" || part.state === "output-available")) {
  return (
    <DiffReview
      changes={part.input.changes}
      decisions={part.state === "output-available" ? part.output : undefined}
      readOnly={part.state === "output-available"}
      onComplete={(decisions) => addToolOutput({ tool: "suggestEdits", toolCallId: part.toolCallId, output: decisions })}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `changes` | `{ id; before; after; label? }[]` | – | Each change; the label says where it is, like a paragraph or file name. |
| `mode` | `"words" \| "lines"` | `"words"` | Compare word by word (prose) or line by line (code). |
| `decisions / defaultDecisions` | `Record<id, "pending" \| "accepted" \| "rejected">` | – | Controlled or initial decisions; missing ids are pending. |
| `onDecisionsChange` | `(decisions) => void` | – | Every decision after a change, in one call even for Accept all. |
| `onDecisionChange` | `(id, decision) => void` | – | Each individual decision, including Undo back to pending. |
| `onComplete` | `(decisions) => void` | – | Called when the last pending change gets a decision. |
| `readOnly` | `boolean` | `false` | Shows decisions without Undo or buttons, e.g. once they’ve been sent. |
| `title` | `string` | `"Suggested changes"` | The header. |

## States

- **pending**: The change inline with Reject and Accept; the header counts what’s left to review, rolling as you go.
- **accepted**: Removed words shrink out of the line (460ms) and added ones lose their tint; code lines that go fold shut. "Accept" morphs to "Accepted" as a green check draws itself, Reject folds away and Undo opens in.
- **rejected**: The reverse: added words shrink away and the struck ones return as plain, muted text; "Reject" morphs to "Rejected".
- **done**: "3 to review" morphs to "2 of 3 accepted"; onComplete fires with every decision.

## What it handles

When an AI edits someone’s work, they should see exactly what it touched and keep the final say. This works out the difference itself and lets them decide, one change at a time or all at once.

- **It works out the difference**: Give each change as before and after text. Removed words are struck in red and added words marked in green.
- **Words for prose, lines for code**: Choose word-by-word or line-by-line comparison to suit what is being edited.
- **Accept or reject, one or all**: Decide one change at a time or all at once, and undo any decision.
- **It resolves in place**: Accepting shrinks the removed words out of the sentence and the added ones lose their tint. Nothing is swapped for a new block.
- **It tells you when you’re done**: The count rolls from “3 to review” to “2 of 3 accepted”, and onComplete fires with every decision.
- **Not colour alone**: Removed text is struck through, code lines have + and − gutters, and each button names its change.

## Replaces

a diff algorithm, tracked-changes styling, Accept and Reject buttons, decision state, an undo for decisions

## Where it fits

- **AI writing tools**: Let people accept or reject the AI’s edits to each paragraph.
- **Coding assistants**: Review the changes to a file line by line before applying them.
- **Content and CMS editors**: Review a rewrite of a page in the style of tracked changes.
- **Drafting replies**: Show what the AI changed in a customer reply before it is sent.

## Notes

- Removed and added words are real <del> and <ins>, and code lines carry "(added)" or "(removed)" for screen readers.
- Colour is never the only signal: removed text is struck through, and code lines have + and − gutters.
- Each button names its change ("Accept Paragraph 2"), and the count is announced as decisions are made. Focus moves to Undo after a decision and back to the button after Undo, so keyboard users never lose their place.
- Words that have folded away are hidden from screen readers. Installs Number roll and Text morph; with reduced motion, decisions change the text in place.
- The diff is a longest-common-subsequence over words or lines; very large inputs fall back to a full replace.

License: MIT (https://hairlineui.com/license#components)
