# Diff

An agent's edit to one file, waiting in its answer: the row reads Edited policy.ts +5 −2 with Undo and Keep, over the path and the line diff. Keep and Undo turn the diff into the file in place, and each change can be kept or undone on its own.

Tier: pro. Type: component. Category: ai. Tags: agent, approval, undo, keyboard, motion.

## Install

```bash
npx shadcn@latest add @pixilui-pro/diff
```

One-time setup: add the registry to `components.json`:

```json
{
  "registries": {
    "@pixilui": "https://pixilui.com/r/{name}.json",
    "@pixilui-pro": {
      "url": "https://pixilui.com/r/pro/{name}.json",
      "headers": { "Authorization": "Bearer ${PIXILUI_TOKEN}" }
    }
  }
}
```

Pro items need a license. Create a token on https://pixilui.com/account and set `PIXILUI_TOKEN` in `.env.local`; the CLI sends it as a bearer header.

Files: `components/pixilui/diff.tsx`.

## Preview

Live preview: https://pixilui.com/components/diff

Knobs:

- `state`: `review`, `kept`, `undone`, `editing`
- `context`: `3 lines`, `1 line`

Signature: Undo one of two changes: its new words erase from the right, the rows fold, the line numbers below roll back, and the row reads 1 kept · 1 undone.

Styles: `lively` (default), `quiet`, set with `data-style` on <html>. The item reads the style through the foundation; it needs no prop.

## Code

Pro source is available to license holders.

## Usage

When to use:

- An edit the agent made to a file that the person keeps or undoes, inside the chat.
- A file with several changes, where some are right and some are not: each change has its own Undo and Keep.
- A replay of the agent writing the edit (a ref's write()).

When not to use:

- An edit across several files under one decision: use File edits.
- Code with nothing to decide: use Code block.
- A side by side review of a whole pull request: a code host does that.

```tsx
import { useRef } from "react";
import { Button } from "@/components/pixilui/button";
import { Diff, type DiffHandle } from "@/components/pixilui/diff";
import { policyAfter, policyBefore } from "@/components/pixilui/diff.examples";

// The first option of each knob is the demo's default; the docs preview starts there.
export const knobs = {
  state: ["review", "kept", "undone", "editing"],
  context: ["3 lines", "1 line"],
} as const;

const ANSWER = { fontSize: "var(--pixilui-text-answer)", lineHeight: "var(--pixilui-text-answer-leading)", letterSpacing: "var(--pixilui-text-answer-tracking)" };

/** The agent's edit to one file, waiting: Keep or Undo it whole, or hover a change for its own. Replay shows the agent writing it. */
export default function DiffDemo({ state = "review", context = "3 lines" }: KnobValues<typeof knobs>) {
  const ref = useRef<DiffHandle>(null);
  return (
    <div className="grid w-full max-w-xl gap-3 rounded-panel bg-surface p-5 elevation-raised">
      <p className="m-0 text-foreground" style={ANSWER}>
        Raised the auto-approve limit to $500. Anything above it now goes to finance leads instead of being denied.
      </p>
      <Diff key={`${state}${context}`} ref={ref} path="src/refunds/policy.ts" before={policyBefore} after={policyAfter} state={state} context={context === "3 lines" ? 3 : 1} />
      <div className="flex gap-1">
        <Button variant="secondary-2" size="sm" onClick={() => ref.current?.write()}>
          Replay the edit
        </Button>
        <Button variant="secondary-2" size="sm" onClick={() => ref.current?.reset()}>
          Reset
        </Button>
      </div>
    </div>
  );
}
```

## Examples

None.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `path` (required) | `string` |  | The file: "src/refunds/policy.ts". The folders go quiet, the file name leads. |
| `before` (required) | `string` |  | The file as it was (an empty string: a new file, all added). |
| `after` (required) | `string` |  | The file as the agent left it. |
| `context` | `number` | `3` | Unchanged lines kept around each change (3, as git); longer runs fold into a band you can open. |
| `state` | `"review" \| "kept" \| "undone" \| "editing"` | `"review"` | How it starts: review (waiting, the default), kept, undone or editing. |
| `defaultOpen` | `boolean` | `true` | Open (default) or folded, uncontrolled. |
| `open` | `boolean` |  | Open or folded, controlled. |
| `onOpenChange` | `(open: boolean) => void` |  | The row was pressed. |
| `onStateChange` | `(state: DiffFileState, changes: DiffState[]) => void` |  | The file's state changed (each change's state, in order, too). |
| `changeActions` | `"float" \| "icons" \| "under"` | `"float"` | Where a change's own Undo and Keep sit: float (the lock's pick), icons or under. |
| `words` | `Partial<DiffWords>` |  | Every word it says. |
| `icons` | `DiffIcons` | `{}` | The icons it draws (the locked disclosure, undo, redo and done). |

Also accepts every native `<section>` attribute except `onToggle`, and a ref to it.

## Keyboard

- `Tab`: Reaches the row, Undo and Keep, each change's Undo and Keep (shown at once on focus), the unchanged-lines bands, then the code when it scrolls sideways.
- `Enter` + `Space`: Decides, folds the file from its row, or opens a band.

## Accessibility

- After Undo focus moves to Redo, after Redo to Undo, after a change's Keep to the next waiting change; each decision is said in the row's status line.
- Signs carry the change as well as color, and each changed line says Added or Removed to a screen reader.
- A change's Undo and Keep are a group named Change 1 of 2; on touch they take a 44 row of their own inside the green block; Keep reaches 44 on touch.

## Motion

- Keep: the removed rows fold shut and the tints drain left to right. Undo: the new words erase from the right, then their rows fold, and the numbers below roll back.
- Redo and the replay: rows open and the words write in across the line (560ms, even), a row apart; the counts roll up from nothing.
- A change's buttons wait 80ms for a hover, then grow from their right edge out of a 2px blur; they leave faster. The header's words swap with the quiet blur swap.
- Reduced motion: every fold, wipe, write and swap at once.

## Responsive

- Fills its container. Long lines wrap under their own column; under 330 wide Undo keeps only its icon; nothing scrolls sideways at 390.

## Performance

- The diff is worked out once per file (a line LCS); the motion is CSS transitions on data-state, plus a few Web Animations for the swaps and the roll.

## Notes for AI

- <Diff path="src/refunds/policy.ts" before={old} after={next} onStateChange={(state, changes) => save(state, changes)} />: plain strings in, the decision out.
- state starts it (review, kept, undone or editing); a ref has keep(), undo(), redo(), hunk(n, "keep" | "undo"), write() and reset().
- context is the unchanged lines kept around each change (3); longer runs fold into a band.
- changeActions picks where a change's own buttons sit (float, the lock's pick; icons; under). Every word is in words.
- Per-style rule: quiet is compact with its hairline, and its bands follow the well's 8 corners in.

## Related

- [File edits](https://pixilui.com/components/file-edits/markdown): An agent's edit across files as one row: Edited 3 files +22 −5 with Undo all and Keep all, over each file's diff hung from a hairline. Decide all at once or file by file and change by change; the row keeps count.
- [Code block](https://pixilui.com/components/code-block/markdown): Code the agent shows you in its answer, in a grey well: the file with its folders quiet, Copy, the syntax in six picked inks and a write-in replay. A well of JSON masks any key as it renders, and Copy copies the mask.
- [Command](https://pixilui.com/components/command/markdown): A command the agent ran: the row reads Ran pnpm test · 4.2 s and folds a well with the command, Copy and what it printed. Running, its output prints in, the clock ticks and Stop ends it; a failure reads red with its exit code.
- [Tool call](https://pixilui.com/components/tool-call/markdown): One tool call in the agent's answer: its row names the call (the tool's real logo, the function as a code tag, the time) and folds its Arguments and Result. A secret in the arguments renders masked, and Copy copies the mask.
