# Unified diff block

Pass unified diff text directly to `UnifiedDiffBlock` from `@ancrel/workspace/diff`. File and hunk headers stay visible. Each hunk starts its own old and new line numbers. The left gutter advances on removals, the right gutter advances on additions, and context advances both. Literal plus, minus and space prefixes identify the operation when colors are unavailable.

```tsx
'use client'

import { useState } from 'react'
import { UnifiedDiffBlock, computeClientFileDiffs } from '@ancrel/workspace/diff'

const serverDiff = '--- a/notes.txt\n+++ b/notes.txt\n@@ -1,2 +1,2 @@\n Title\n-First edition\n+Second edition'
const files = computeClientFileDiffs({ 'notes.txt': 'Title\nFirst edition\n' },
  { 'notes.txt': 'Title\nWorking draft\n' })

export default function App() {
  const [local, setLocal] = useState(false)
  return <>
    <label><input type="checkbox" checked={local} onChange={event => setLocal(event.target.checked)} />
      Show local preview</label>
    <UnifiedDiffBlock label={local ? 'Local preview' : 'Server diff'} diff={local ? files[0].diff : serverDiff} />
  </>
}
```

Expected:

The server diff shows the change from First edition to Second edition. Select Show local preview to see Working draft as the added line. Both views retain literal change signs and line numbers.

## Local previews

Use `computeUnifiedDiffText(before, after, path?)` to preview edits to a single text file. It looks ahead at most five lines, includes two lines of context around each change and merges overlapping context. Identical strings produce an empty preview.

Use `computeClientFileDiffs(before, after)` for two maps of path to text. It returns changed paths in sorted order, each with a change kind and a preview produced by the same bounded algorithm. Both helpers are preview-only. Repeated lines or moves beyond the lookahead can produce a different alignment from a version-control tool. Generate authoritative patches on the server and pass that text to the component for review.

## Presentation

Supply a descriptive `label` when a page contains several diffs. Each region supports horizontal scrolling and keyboard focus. Load the token and workspace stylesheets to apply the mono font, tabular line numbers and change colors.

`--ac-sys-diff-add-bg`

| `Context` | `--ac-sys-diff-add-bg` |
| - | - |
| `dark/compact` | `#17372b` |
| `dark/comfortable` | `#17372b` |
| `light/compact` | `#e4f4ec` |
| `light/comfortable` | `#e4f4ec` |

`--ac-sys-diff-remove-bg`

| `Context` | `--ac-sys-diff-remove-bg` |
| - | - |
| `dark/compact` | `#392628` |
| `dark/comfortable` | `#392628` |
| `light/compact` | `#fbeaec` |
| `light/comfortable` | `#fbeaec` |

| `Prop` | `Type` | `Required` | `Default` |
| - | - | - | - |
| `className` | `string` | `false` | `—` |
| `diff` | `string` | `true` | `—` |
| `key` | `React.Key \| null \| undefined` | `false` | `—` |
| `label` | `string` | `false` | `—` |
| `ref` | `React.Ref<HTMLPreElement> \| undefined` | `false` | `—` |

```ts
import * as React from 'react';
import type { UnifiedDiffBlockProps } from './types.js';
export type * from './types.js';
export { computeClientFileDiffs, computeUnifiedDiffText } from './preview.js';
export declare const UnifiedDiffBlock: React.ForwardRefExoticComponent<UnifiedDiffBlockProps & React.RefAttributes<HTMLPreElement>>;
```
