Code Diff
What a change did, read from a unified diff: added and removed lines tinted and marked, both sides numbered, and a +N −M count for the header.
@@ -1,8 +1,9 @@export function formatDuration(seconds: number) {Removed: const whole = Math.round(seconds)Added: const whole = Math.max(1, Math.round(seconds)) if (whole < 60) return `${whole}s` const minutes = Math.floor(whole / 60)Removed: return `${minutes}m ${whole % 60}s`Added: const rest = whole % 60Added: return rest > 0 ? `${minutes}m ${rest}s` : `${minutes}m`}Installation
npx shadcn@latest add "@aiellie/code-diff"shadcn needs the @aiellie registry in components.json first. npx aiellie add code-diff adds it on its first run, then installs the same way.
Code Diff shows what a change did, read straight from the unified diff that git diff or an agent's edit tool writes. Added and removed lines are tinted and marked, both sides of the change are numbered, and each hunk keeps its header. CodeDiffStat counts the lines for the header.
It goes inside a CodeBlock, so a diff has the same frame, title and actions as any other code.
Getting started
Install the code block too
The diff installs
code-blockwith it, for its frame and its colouring.Show a diff
components/change.tsximport { CodeBlock, CodeBlockHeader, CodeBlockTitle, } from "@/components/aiellie/code-block" import { CodeDiffContent, CodeDiffStat } from "@/components/aiellie/code-diff" export function Change({ file, diff }: { file: string; diff: string }) { return ( <CodeBlock> <CodeBlockHeader> <CodeBlockTitle>{file}</CodeBlockTitle> <CodeDiffStat diff={diff} className="ms-auto" /> </CodeBlockHeader> <CodeDiffContent diff={diff} language="ts" /> </CodeBlock> ) }Lines before the first hunk, like
diff --gitand the---and+++names, are left out, since the header already names the file.
Anatomy
<pre data-slot="code-diff-content" dir="ltr">
<span data-slot="code-diff-hunk" /> {/* @@ -1,8 +1,9 @@ */}
<span data-slot="code-diff-line" data-kind="context | added | removed" />
</pre>
<span data-slot="code-diff-stat" /> {/* +3 −2 */}Each line's gutter holds its old number, its new number and a + or −, and stays put while the code scrolls. A screen reader hears "Added" or "Removed" before a changed line, so the change doesn't rest on colour alone.
The tints are mixed into the ground rather than laid over it, so the pinned gutter can share its line's colour. Added lines use emerald, since a shadcn theme has no success colour.
Examples
A new file
A diff against /dev/null is all additions. Leave out lineNumbers when the numbers say nothing, and leave out language to keep the lines plain.
@@ -0,0 +1,3 @@Added: DATABASE_URL=Added: OPENAI_API_KEY=Added: NEXT_PUBLIC_APP_URL=http://localhost:3000Count what changed
diffStat gives the same counts as numbers, for a summary of several files.
import { diffStat } from "@/components/aiellie/code-diff"
const { added, removed } = diffStat(diff)API reference
CodeDiffContent
| Prop | Type | Default | Description |
|---|---|---|---|
diff | string | required | A unified diff. |
language | string | Any language Shiki knows. Left out, the lines stay plain. | |
lineNumbers | boolean | true | Numbers both sides of each line. |
All other pre props are forwarded.
CodeDiffStat
| Prop | Type | Default | Description |
|---|---|---|---|
diff | string | required | The diff to count. |
All other span props are forwarded.
parseDiff
| Argument | Type | Default | Description |
|---|---|---|---|
diff | string | required | A unified diff. |
Returns its rows as DiffLines: hunk headers, and context, added and removed lines with their numbers.