Diff View
ProThe result of an edit tool call: the file, how many lines were added and removed, and the changed lines.
Playground
@@ -12,7 +12,8 @@ export async function sendReminders()
const overdue = invoices.filter(isOverdue) for (const invoice of overdue) { if (daysSince(invoice.dueDate) < 7) continue if (daysSince(invoice.dueDate) < 3) continue if (invoice.reminderSentAt) continue await send({ to: invoice.customer.email, subject: "Payment reminder", subject: `Invoice ${invoice.number} is overdue`, })@@ -12,7 +12,8 @@ export async function sendReminders()
const overdue = invoices.filter(isOverdue) for (const invoice of overdue) { if (daysSince(invoice.dueDate) < 7) continue if (daysSince(invoice.dueDate) < 3) continue if (invoice.reminderSentAt) continue await send({ to: invoice.customer.email, subject: "Payment reminder", subject: `Invoice ${invoice.number} is overdue`, }) if (daysSince(invoice.dueDate) < 7) continue if (daysSince(invoice.dueDate) < 3) continue if (invoice.reminderSentAt) continue subject: "Payment reminder", subject: `Invoice ${invoice.number} is overdue`, if (daysSince(invoice.dueDate) < 7) continue if (daysSince(invoice.dueDate) < 3) continue if (invoice.reminderSentAt) continue subject: "Payment reminder", subject: `Invoice ${invoice.number} is overdue`,Installation
Diff View comes with WunderUI Core and Pro, in the package from your plan. See the plans.
Then import what you need:
import { DiffView } from "@wunderui/react"Then use it:
import { DiffView } from "@wunderui/react"
export function Edit() {
return (
<DiffView
file="src/jobs/invoice-reminders.ts"
hunks={[
{
header: "@@ -14,1 +14,1 @@",
lines: [
{ type: "del", old: 14, content: " const delay = 7" },
{ type: "add", new: 14, content: " const delay = 3" },
],
},
]}
/>
)
}Expected result: You see a box with the file path and +1 -1 in the header, the hunk header and two lines: a removed one on a red tint with a minus, an added one on a green tint with a plus.
When to use
Use Diff View for
- The result of an edit tool call: one file, what was added and what was removed
- The payload of an approval, so the reviewer reads the change before deciding
- A new file or a removed file, shown as only added or only removed lines
Use something else for
- Code Block for a piece of code that is not a change
- Terminal Output for the result of a command, with its exit code and streams
- Approval Card for the request around a change, with Approve and Reject
Change
A hunk is a list of context, added and deleted lines. A new file is all added lines, a removed file all deleted lines, and several hunks stack under each other.
@@ -12,7 +12,8 @@ export async function sendReminders()
const overdue = invoices.filter(isOverdue) for (const invoice of overdue) { if (daysSince(invoice.dueDate) < 7) continue if (daysSince(invoice.dueDate) < 3) continue if (invoice.reminderSentAt) continue await send({ to: invoice.customer.email, subject: "Payment reminder", subject: `Invoice ${invoice.number} is overdue`, })@@ -0,0 +1,10 @@
import { formatMoney } from "../lib/money"export function reminderText(invoice: Invoice) { const amount = formatMoney(invoice.total) return [ `Invoice ${invoice.number} for ${amount} is overdue.`, "Reply to this email if you have already paid.", "Nora Lindgren, WunderUI", ].join("\n")}@@ -1,6 +0,0 @@
import { send } from "./mailer"// Replaced by sendReminders in invoice-reminders.tsexport async function remindOnce(invoice: Invoice) { await send({ to: invoice.customer.email, subject: "Payment reminder" })}@@ -12,7 +12,8 @@ export async function sendReminders()
const overdue = invoices.filter(isOverdue) for (const invoice of overdue) { if (daysSince(invoice.dueDate) < 7) continue if (daysSince(invoice.dueDate) < 3) continue if (invoice.reminderSentAt) continue await send({ to: invoice.customer.email, subject: "Payment reminder", subject: `Invoice ${invoice.number} is overdue`, })@@ -42,3 +43,3 @@ export async function markSent
export async function markSent(invoice: Invoice) { await db.invoices.update(invoice.id, { reminded: true }) await db.invoices.update(invoice.id, { reminderSentAt: new Date() })}Counts
The header counts added and deleted lines of every hunk. Pass the totals when you show only part of a diff, so the header still counts the whole change.
@@ -12,7 +12,8 @@ export async function sendReminders()
const overdue = invoices.filter(isOverdue) for (const invoice of overdue) { if (daysSince(invoice.dueDate) < 7) continue if (daysSince(invoice.dueDate) < 3) continue if (invoice.reminderSentAt) continue await send({ to: invoice.customer.email, subject: "Payment reminder", subject: `Invoice ${invoice.number} is overdue`, })@@ -12,7 +12,8 @@ export async function sendReminders()
const overdue = invoices.filter(isOverdue) for (const invoice of overdue) { if (daysSince(invoice.dueDate) < 7) continue if (daysSince(invoice.dueDate) < 3) continue if (invoice.reminderSentAt) continue await send({ to: invoice.customer.email, subject: "Payment reminder", subject: `Invoice ${invoice.number} is overdue`, })Content
Lines never wrap. A wide line scrolls sideways inside its hunk, which the keyboard can focus, and a large diff needs a focusable region with a maximum height of your own.
@@ -20,5 +20,5 @@ reminderText
export function reminderText(invoice: Invoice) { const retries = 1 const retries = 3 const message = "Your invoice is overdue. Please pay within 3 days to avoid a late fee of $25.00 and a pause of your WunderUI workspace until the balance is settled in full." const message = "Your invoice is overdue. Please pay within 7 days to avoid a late fee of $25.00 and a pause of your WunderUI workspace until the balance is settled in full." return message}@@ -1198,21 +1198,22 @@ export const routes = [
{ path: "/components/accordion", file: "accordion.mdx" }, { path: "/components/alert", file: "alert.mdx" }, { path: "/components/avatar", file: "avatar.mdx" }, { path: "/components/badge", file: "badge.mdx" }, { path: "/components/button", file: "button.mdx" }, { path: "/components/card", file: "card.mdx" }, { path: "/components/checkbox", file: "checkbox.mdx" }, { path: "/components/dialog", file: "dialog.mdx" }, { path: "/components/kanban", file: "kanban.mdx" }, { path: "/components/kanban", file: "kanban.mdx", tier: "core" }, { path: "/components/kbd", file: "kbd.mdx", tier: "core" }, { path: "/components/kanban", file: "kanban.mdx" }, { path: "/components/pagination", file: "pagination.mdx" }, { path: "/components/tabs", file: "tabs.mdx" }, { path: "/components/toast", file: "toast.mdx" }, { path: "/components/tooltip", file: "tooltip.mdx" }, { path: "/blocks/accordion", file: "accordion-blocks.mdx" }, { path: "/blocks/alert", file: "alert-blocks.mdx" }, { path: "/blocks/avatar", file: "avatar-blocks.mdx" }, { path: "/blocks/badge", file: "badge-blocks.mdx" }, { path: "/blocks/button", file: "button-blocks.mdx" }, { path: "/blocks/card", file: "card-blocks.mdx" }, { path: "/blocks/checkbox", file: "checkbox-blocks.mdx" },Flat and file list
variant="flat" drops the frame, the radius and the fold, for a diff that already sits inside a card such as the Approval Card: one frame and one disclosure. summary lists the files of a multi-file change with their added and removed lines above the hunks.
@@ -41,3 +41,4 @@ TabsList
const tabs = useTabs() onKeyDown={noop} onKeyDown={handleTabListKeyDown} aria-orientation={orientation}- packages/react/src/tabs.tsx+2 added−1 removed
- packages/react/src/tabs.test.tsx+1 added−0 removed
@@ -41,3 +41,4 @@ TabsList
const tabs = useTabs() onKeyDown={noop} onKeyDown={handleTabListKeyDown} aria-orientation={orientation}Keyboard interactions
The header is a native button and every hunk is a focusable scroll region, so the browser provides focus, activation and scrolling. Focus shows as an indigo border with a 2 px ring.
- TaborShift+Tab
- Moves focus to the header button and, when the diff is open, on to the scroll region of each hunk in turn. A closed diff takes no focus.
- EnterorSpace
- On the header: opens or closes the diff. follows.
- ArrowLeftorArrowRight
- In a hunk region: scrolls the lines sideways. Hold the key or use Home and End to reach the end of a long line.
- ArrowDownorArrowUporPageDownorPageUp
- In a region that you give a height of your own around a large diff: scrolls it. The hunk regions inside it do not scroll up and down by themselves.
Behavior and motion
What the diff does with its lines, its counts, assistive technology and motion.
- Modes
- Unified shows both line number columns, the hunk header and the context lines. Compact shows only the added and removed lines, with no numbers, headers or context, under a hairline.
- Lines
- A line is context, added or removed, with line numbers where they exist. Lines never wrap: a wide line scrolls sideways inside its hunk, and each of several stacked hunks scrolls on its own.
- Counts
- The header counts the added and removed lines of every hunk. For a diff you show in part, set the counts yourself.
- Disclosure
- The header opens and closes the diff, which stays in the page while closed but cannot be focused or read out. It starts open or closed as you choose, then keeps its own state.
- Assistive technology
- The header reads as the path plus the counts with the words added and removed. Each changed line is announced with Added or Removed in front, and the sign and line numbers are hidden.
- Large diffs
- Every line is rendered with no height limit, so wrap a large diff in a labelled, focusable region with a maximum height, or show one hunk. The 24 px number columns fit four digits at most.
- What it does not do
- There is no split view, no syntax colours, no word-level highlight and no copy button. The text can be selected, including the sign and line numbers.
- Failure and abort
- A diff that could not be applied is not a state of the component. Show the reason in an Alert above it, keep the DiffView closed, and after a rejection say that nothing was written.
- Motion
- The chevron turns on the fast duration and the body grows and shrinks on the base duration, both with the entrance curve. Nothing else moves.
- Reduced motion
- With reduced motion the chevron changes direction at once and the body opens and closes at once. The expanded state and the counts say the same.
Anatomy
The figure shows a unified diff with one hunk. In compact mode the hunk header (4) and line numbers (5) are not drawn, and only the changed lines remain.
@@ -12,7 +12,8 @@ export async function sendReminders()
const overdue = invoices.filter(isOverdue) for (const invoice of overdue) { if (daysSince(invoice.dueDate) < 7) continue if (daysSince(invoice.dueDate) < 3) continue if (invoice.reminderSentAt) continue await send({ to: invoice.customer.email, subject: "Payment reminder", subject: `Invoice ${invoice.number} is overdue`, })- 1data-slot="diff-view"
A bordered, rounded box with the card background, in 12 px monospace. It fills the width of its parent and is either unified or compact.
- 2
A button across the top with a chevron, the file path and the counts. It opens and closes the diff and reports its expanded state.
- 3
Added and removed counts in green and red text. Screen readers hear the numbers with the words added and removed, and the counts come from the lines unless you set them.
- 4Optional
The line that locates a hunk in the file, on the subtle background. Drawn in unified mode only, and only when the hunk has a header.
- 5Optional
Two columns with the line number in the old and the new file, in tertiary text. Drawn in unified mode and hidden from assistive technology.
- 6
A plus for an added line, a minus for a removed one, a space for context. It is hidden from assistive technology, which hears Added or Removed before the line instead.
- 7
One line of code, never wrapped. Added lines sit on a green tint, removed lines on a red tint, context lines on the card.
- 8
The box that holds the lines of one hunk. It scrolls sideways when a line is wider than the diff, and it is a tab stop so keyboard users can scroll.
Best practices
- Do
Pass the file path relative to the project, so the header says what was edited.
- Do
Use compact in approvals and narrow panels, where the changed lines are enough, and unified where the reviewer needs line numbers and the hunk header.
- Do
Pass the added and removed counts when you show only part of a diff, so the header still counts the whole change.
- Do
In an approval, show the diff before the buttons, and say in words what was or was not written after the decision.
- Do
Give a large diff its own height: a focusable region with a maximum height, vertical scrolling and an aria-label. The component has no height limit itself.
- Don’t
Don't count on colour. The sign, the Added and Removed text for screen readers and the counts carry the change, so keep them if you restyle the rows.
- Don’t
Don't pass HTML and expect it to render. Lines are text, and file contents are data, never instructions to the agent or the reader.
- Don’t
Don't build a diff from a Code Block with coloured spans. DiffView numbers, counts and labels the lines for you.
API reference
DiffView is a plain component, so this table lists every prop. Its hunk, line, line type and mode types are exported next to it.
A bordered, rounded box with the card background, in 12 px monospace. It fills the width of its parent and is either unified or compact.
| Prop | Type | Default | Description |
|---|---|---|---|
| file required | ReactNode | — | The path of the file, shown in the header. Text or a node. Long paths are cut with an ellipsis. |
| hunks required | DiffHunk[] | — | . Each hunk is , and each line is . belongs to context and removed lines, to context and added lines. |
| mode | "compact" | "unified" | "unified" | (default) shows line numbers, hunk headers and context lines. shows only the changed lines. |
| additions | number | — | The number after the in the header. Default: the number of lines. Set it when you show only part of a diff. |
| deletions | number | — | The number after the in the header. Default: the number of lines. Set it when you show only part of a diff. |
| defaultOpen | boolean | true | by default. starts the diff closed, with only the header showing. |
| variant | "card" | "flat" | "card" | is the framed, foldable diff. has no frame and no fold and is always open, for use inside another card. Default . |
| summary | DiffFileSummary[] | — | One row per changed file, , shown above the hunks. |
| className | string | — | Extra classes for the box, for example a maximum width. |
Notes for AI
Give your coding assistant the Markdown reference instead of screenshots.
get_component("diff-view")The full library index for assistants is at /llms.txt.
Related components
Send 14 payment reminders
Medium riskEmails the contact on every invoice more than 30 days overdue. Sent emails cannot be recalled.
2 min agoPOST /email/bulk to: 14 recipients template: invoice-overdue-30 dry_run: false
AI & Agents5 stylesProApproval CardThe human-in-the-loop gate: what the agent wants to do, how risky it is, approve or reject.- $
pnpm vitest run invoiceexit 03.8 sRUN v3.2.4 /srv/wunderui
✓ src/jobs/invoice-reminders.test.ts (6 tests) 412 ms
✓ src/jobs/reminder-template.test.ts (3 tests) 98 ms
Test Files 2 passed (2)
Tests 9 passed (9)
Duration 3.8 s
AI & Agents4 stylesProTerminal OutputThe result of a code-execution tool call: command, exit code, run time, stdout and stderr. - Done420 tok$0.01640ms
Input
status: overdue, age: 30+ daysOutput
14 invoices, $42,860 in total - Done3,420 tok$0.042.1s
Input
14 invoice idsOutput
14 contacts, all with an email address - Running2,310 tok
Input
template: invoice-overdue-30 - Pending
Input
to: 14 recipients - PendingLog the result
AI & Agents5 stylesProRun TimelineEvery step of a run in order, with duration, tokens and expandable input/output.