# Diff

Unified or before/after text diff with line numbers, word-level highlights, and +/- stats.

> For the complete documentation index, see [llms.txt](/llms.txt). Markdown variants are available by appending `.md` to any URL or sending an `Accept: text/markdown` header. An agent skill is available at [/.well-known/agent-skills/site-skill.md](/.well-known/agent-skills/site-skill.md).

<ComponentPreview name="diff">
  <DiffDemo />
</ComponentPreview>

## Installation [#installation]

```bash
npx shadcn@latest add https://motif.sweetretry.com/r/diff.json
```

## Usage [#usage]

```tsx
import {
  Diff,
  DiffContent,
  DiffFile,
  DiffHeader,
  DiffStat,
} from "@/components/ui/motif/diff";

export function FileChange({ patch }) {
  return (
    <Diff patch={patch}>
      <DiffHeader>
        <DiffFile />
        <DiffStat />
      </DiffHeader>
      <DiffContent />
    </Diff>
  );
}
```

| Part           | Role                                                                          |
| -------------- | ----------------------------------------------------------------------------- |
| `Diff`         | Root frame accepting either `patch` or `before`/`after`                       |
| `DiffHeader`   | Optional header row; renders nothing when empty                               |
| `DiffFile`     | Displays path from `patch` `+++` header or `file` prop                        |
| `DiffStat`     | Displays `+N −M` counts, omitting zero sides                                  |
| `DiffContent`  | Renders diff lines with gutter numbers, markers, and inline word highlights   |
| `useDiff`      | Returns parsed lines, counts, and path for custom subcomponents               |
| `parsePatch`   | Standalone patch parser returning lines, counts, and path                     |
| `compareTexts` | Standalone `before`/`after` diff helper using `jsdiff`                        |

## Inputs [#inputs]

Pass either `patch` or `before`/`after`, never both.

```tsx
<Diff patch={call.patch}>…</Diff>

<Diff before={call.old_string} after={call.new_string} firstLine={call.line}>
  …
</Diff>
```

- `firstLine` sets the starting line number for `before`/`after` diffs (pass `1` for full files; omit to hide the gutter). Unchanged stretches over 3 lines fold into a marker row.
- `patch` accepts full `git diff` headers (`diff --git`, `index`, `---`/`+++`, `@@`) or bare `+`/`-`/space lines; `\ No newline at end of file` is stripped.
- Gutter `+`/`-` markers are non-selectable so copied text contains only code.

## Word Highlights & Composition [#word-highlights--composition]

Paired removal/addition lines sharing at least 50% of their text highlight changed words at a stronger tint.

<ComponentPreview name="diff" align="start">
  <DiffProseDemo />
</ComponentPreview>

Inside a [Tool Call](/docs/agents/tool-call) row that already names the file, render `<DiffContent />` alone:

<ComponentPreview name="diff" align="start">
  <DiffToolCallDemo />
</ComponentPreview>

```tsx
<ToolCallContent>
  <Diff after={call.new_string} before={call.old_string}>
    <DiffContent />
  </Diff>
</ToolCallContent>
```

For a file summary list, render `<DiffHeader />` without `<DiffContent />`:

<ComponentPreview name="diff" align="start">
  <DiffFilesDemo />
</ComponentPreview>

## Styling & Accessibility [#styling--accessibility]

- Uses `success` and `destructive` tokens (`10%` line tint, `25%` word highlight); installing adds `success` CSS variables to `:root`, `.dark`, and `@theme`.
- Renders inside `<pre>` with `+`/`-` markers exposed to screen readers, line numbers hidden, and `DiffStat` labeled `"N added, M removed"`.

## Component source [#component-source]

<ComponentSource
  name="diff"
  src="registry/new-york/agents/diff/index.tsx"
  title="index.tsx"
/>

<ComponentSource
  name="diff"
  src="registry/new-york/agents/diff/read.ts"
  title="read.ts"
/>
