Diff viewer
Unified and side-by-side code diff viewer with addition/deletion counts and monospace line numbers.
pnpm dlx shadcn@latest add @haze/diff-viewer---
import { FileDiff } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface DiffLine {
type: "context" | "add" | "del";
oldLine?: number;
newLine?: number;
content: string;
}
interface Props {
id: string;
filename?: string;
lines?: DiffLine[];
view?: "unified" | "split";
class?: string;
}
const {
id,
filename = "haze.config.ts",
view = "unified",
lines = [
{ type: "context", oldLine: 12, newLine: 12, content: 'radius: "0.4rem",' },
{ type: "del", oldLine: 13, content: 'borders: "translucent",' },
{ type: "add", newLine: 13, content: 'borders: "opaque",' },
{ type: "add", newLine: 14, content: 'lightTheme: true,' },
{ type: "context", oldLine: 14, newLine: 15, content: 'icons: "@lucide/astro",' },
],
class: className,
} = Astro.props;
const additions = lines.filter((l) => l.type === "add").length;
const deletions = lines.filter((l) => l.type === "del").length;
const leftLines = lines.filter((l) => l.type !== "add");
const rightLines = lines.filter((l) => l.type !== "del");
---
<section
id={id}
data-haze-diff-viewer
aria-label={`Code diff for ${filename}`}
class={cn("min-w-0 overflow-hidden rounded-xl border border-border bg-card text-card-foreground", className)}
>
<header class="flex min-h-11 flex-wrap items-center justify-between gap-2 border-b border-border bg-muted/40 px-4 py-1.5">
<div class="flex min-w-0 items-center gap-2">
<FileDiff class="size-4 shrink-0 text-muted-foreground" aria-hidden="true" />
<span class="min-w-0 truncate font-mono text-[0.8125rem]/5 font-medium text-foreground">{filename}</span>
<span class="rounded-sm bg-success/14 px-1.5 py-0.5 font-mono text-xs font-medium text-success tabular-nums">
+{additions}
</span>
<span class="rounded-sm bg-destructive/14 px-1.5 py-0.5 font-mono text-xs font-medium text-destructive tabular-nums">
-{deletions}
</span>
</div>
<div role="group" aria-label="Diff view mode" class="inline-flex shrink-0 rounded-lg bg-muted p-1">
<button
type="button"
data-diff-mode="unified"
aria-pressed={view === "unified" ? "true" : "false"}
class="inline-flex h-6 cursor-pointer items-center rounded-md px-2.5 text-xs font-medium text-muted-foreground transition-[color,background-color,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-100 aria-pressed:bg-card aria-pressed:text-foreground aria-pressed:shadow-key focus-visible:outline-2 focus-visible:outline-ring"
>
Unified
</button>
<button
type="button"
data-diff-mode="split"
aria-pressed={view === "split" ? "true" : "false"}
class="inline-flex h-6 cursor-pointer items-center rounded-md px-2.5 text-xs font-medium text-muted-foreground transition-[color,background-color,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-100 aria-pressed:bg-card aria-pressed:text-foreground aria-pressed:shadow-key focus-visible:outline-2 focus-visible:outline-ring"
>
Split
</button>
</div>
</header>
<div data-diff-pane="unified" hidden={view !== "unified"} class="min-w-0 overflow-x-auto font-mono text-[0.8125rem]/6">
<table class="w-full min-w-[26rem] border-collapse">
<tbody>
{
lines.map((line) => (
<tr
class={cn(
line.type === "add" && "bg-success/8",
line.type === "del" && "bg-destructive/8",
)}
>
<td class="w-9 px-2 text-right text-muted-foreground select-none tabular-nums">{line.oldLine ?? ""}</td>
<td class="w-9 border-r border-border px-2 text-right text-muted-foreground select-none tabular-nums">{line.newLine ?? ""}</td>
<td class={cn("w-6 text-center select-none", line.type === "add" ? "text-success" : line.type === "del" ? "text-destructive" : "text-muted-foreground")}>
{line.type === "add" ? "+" : line.type === "del" ? "-" : " "}
</td>
<td class="pr-4 whitespace-pre text-foreground">{line.content}</td>
</tr>
))
}
</tbody>
</table>
</div>
<div data-diff-pane="split" hidden={view !== "split"} class="grid divide-y divide-border min-w-0 overflow-x-auto font-mono text-[0.8125rem]/6 md:grid-cols-2 md:divide-x md:divide-y-0">
<table class="w-full border-collapse">
<tbody>
{
leftLines.map((line) => (
<tr class={cn(line.type === "del" && "bg-destructive/8")}>
<td class="w-9 border-r border-border px-2 text-right text-muted-foreground select-none tabular-nums">{line.oldLine ?? ""}</td>
<td class="px-3 whitespace-pre text-foreground">{line.content}</td>
</tr>
))
}
</tbody>
</table>
<table class="w-full border-collapse">
<tbody>
{
rightLines.map((line) => (
<tr class={cn(line.type === "add" && "bg-success/8")}>
<td class="w-9 border-r border-border px-2 text-right text-muted-foreground select-none tabular-nums">{line.newLine ?? ""}</td>
<td class="px-3 whitespace-pre text-foreground">{line.content}</td>
</tr>
))
}
</tbody>
</table>
</div>
</section>
<script>
function initDiffViewers() {
document.querySelectorAll<HTMLElement>("[data-haze-diff-viewer]:not([data-enhanced])").forEach((root) => {
root.setAttribute("data-enhanced", "");
const btns = Array.from(root.querySelectorAll<HTMLButtonElement>("[data-diff-mode]"));
const panes = Array.from(root.querySelectorAll<HTMLElement>("[data-diff-pane]"));
btns.forEach((btn) => {
btn.addEventListener("click", () => {
const mode = btn.dataset.diffMode;
btns.forEach((b) => b.setAttribute("aria-pressed", String(b === btn)));
panes.forEach((p) => {
p.hidden = p.dataset.diffPane !== mode;
});
});
});
});
}
initDiffViewers();
if (!document.documentElement.hasAttribute("data-haze-diff-listener")) {
document.documentElement.setAttribute("data-haze-diff-listener", "");
document.addEventListener("astro:page-load", initDiffViewers);
}
</script>When to use
- Reviewing config updates, release diffs, or AI-proposed file changes
- Inspecting before-and-after code patches inside drawers or PR views
When not to use
- Read-only static snippets need no diff markers; use code-block
- Visual before/after image comparison belongs in compare-slider
Installation
Add the Haze registry to components.json once, then install Diff viewer with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/diff-viewernpx shadcn@latest add @haze/diff-vieweryarn dlx shadcn@latest add @haze/diff-viewerbunx --bun shadcn@latest add @haze/diff-viewerFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique DOM id for the diff viewer. |
| filename | string | "haze.config.ts" | File path shown in the header bar. |
| lines | DiffLine[] | – | Diff hunks with type (context, add, del), line numbers, and content. |
| view | "unified" | "split" | "unified" | Initial unified or side-by-side layout. |
Accessibility
- Explicit '+' and '-' markers accompany color fills so additions and deletions never depend on hue alone.
- Decorative line numbers carry select-none so copying code text skips line gutters.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Diff viewer" with `npx shadcn@latest add @haze/diff-viewer`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import DiffViewer from "@/components/haze/ui/diff-viewer.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/diff-viewer.md
Customize with real content
Adapt the Haze diff-viewer ui for our product. Replace the demo values of these props with our real content: id, filename, lines, view. Keep the layout, the heading level rules and these accessibility details: Explicit '+' and '-' markers accompany color fills so additions and deletions never depend on hue alone. Decorative line numbers carry select-none so copying code text skips line gutters. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using diff-viewer, check it fits: use it when reviewing config updates, release diffs, or ai-proposed file changes; inspecting before-and-after code patches inside drawers or pr views. Avoid it when read-only static snippets need no diff markers; use code-block; visual before/after image comparison belongs in compare-slider. Alternatives: @haze/code-block, @haze/compare-slider, @haze/json-viewer.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.