Git Diff
A patch or a list of file diffs for review: per-file headers (rename arrow, status chip, +/- counts and bar, mode change, collapse, viewed), a tree of changed files to jump between them, unified/split and wrap controls, one virtualized document for 500-file patches, j/k and n/p, line selection for comments, and agent actions openFile, nextHunk, previousHunk, markViewed and getHunk.
Example
Installation
lucide-reactUsage
Use GitDiff to review a patch or a set of changed files: pass git diff output as patch, or FileDiffs with both texts so unchanged regions expand. The tree beside it jumps to a file; marking a file viewed folds it. onLineSelect gives the line or Shift range a comment would anchor to.
API reference
GitDiffTree
| Prop | Type | Default |
|---|---|---|
filesrequired | readonly DiffViewFile[] | – |
groupsWithout it, one tree of every file. | readonly GitDiffTreeGroup[] | – |
valueThe `path` of the file shown, highlighted in the tree. | string | null | – |
onOpenrequired | (path: string) => void | – |
labelsrequired | DiffLabels | – |
className | string | – |
GitDiff
| Prop | Type | Default |
|---|---|---|
patchA unified diff as git prints it (`git diff`, `git show`, a `.patch`), with any number of files. | string | – |
filesUnchanged regions expand only for a file that has `oldText` and `newText`. | readonly FileDiff[] | – |
fileTree`--diff-tree-width` wide (13rem). `"collapsible"` adds a toolbar toggle that hides and shows it. @defaultValue true | boolean | "collapsible" | true |
fileTreeOpen | boolean | – |
defaultFileTreeOpen | boolean | true |
onFileTreeOpenChange | (open: boolean) => void | – |
groupsThe tree shows one section per group; the diff lists the groups one after another, in order. | readonly GitDiffGroup[] | – |
toolbarThe layout and wrap controls and the totals above the diff. @defaultValue true | boolean | true |
height`fill` (default) fills the height GitDiff is given. `auto` grows to the whole diff, for a scrolling dialog body: the body is the one scroller and the tree stays in view up to 40rem tall. A `--diff-max-height` caps both, and the diff scrolls inside past it. | DiffViewProps["height"] | "fill" |
currentFileSetting it scrolls the diff to that file; `onCurrentFileChange` reports the file at the top as the diff scrolls. | string | null | – |
defaultCurrentFile | string | null | – |
Also takes DiffViewProps except files, headers.
Other exports
useOpenCurrentFilecreateDiffViewControllertype GitDiffTreeGrouptype GitDiffTreePropstype DiffViewControllertype GitDiffGrouptype GitDiffPropsAccessibility
- The lines of each file are a grid with one tab stop: arrows move by line (and side, in split), Page Up and Page Down by a screen, j/k to the next or previous hunk, n/p to the next or previous file; Enter or Space selects a line, Shift extends, Escape clears.
- Every line carries its +/- sign and a screen-reader label (Added, line 12), so a change never rests on colour; diff fills and changed words keep every syntax colour at 4.5:1 in both modes and every palette.
- Code stays left to right in a right-to-left page; headers follow the page. Find in page reaches lines that aren't rendered and opens a folded region or file it matches.
Agent actions
The diff is one component named by its aria-label, with openFile(path), currentFile(), nextHunk(), previousHunk(), nextFile(), markViewed(path, viewed), getHunk(path, n) (read-only), expandGap, setLayout and setWrap; its items are the file paths. Viewed checkboxes, the layout control and the wrap toggle are UI SDK parts too.
Runs agent.snapshot() from @ui-sdk/react/agent on the live preview above.