Diff Table
SaveAn agent's proposed edits to table rows, with added, removed and changed cells, an include toggle on each change, and an Apply footer that folds the accepted changes into the table.
- Category
- AI elements
- Version
- 0.1.0
- Frameworks
- Next.js, Vite + React
- Styling
- Tailwind CSS v4
Preview
Diff Table preview
Proposed changes to Accounts
From the renewal cleanup: reassign one owner, upgrade Bramble & Rye, merge a duplicate, add a signed deal.
+1 added,−1 removed,~2 changed
ChangeAccountOwnerPlanARRInclude
Northwind FreightPriya RamanScale$148,000
ChangedHalcyon DentalTeam$18,400
Was Marcus Oyelaran
Now Priya Raman
ChangedBramble & RyeInes Carvalho
Was Team
Now Scale
Was $22,900
Now $61,200
RemovedBramble and RyeInes CarvalhoTeam$22,900
Kestrel RoboticsTomas LindqvistEnterprise$310,000
AddedOsprey HealthMarcus OyelaranTeam$27,500
Installation
npx @shakenui/cli add diff-tableReview the plan first. A dry run lists every file and package without changing anything:
npx @shakenui/cli add diff-table --dry-runUsage
diff-table-example.tsx
"use client";
import { DiffTable, type DiffTableColumn, type DiffTableRow } from "@/components/shaken/agent/diff-table";
const columns: DiffTableColumn[] = [
{ key: "account", header: "Account", width: "minmax(0,1.6fr)" },
{ key: "owner", header: "Owner", width: "minmax(0,1.2fr)" },
{ key: "plan", header: "Plan", width: "minmax(0,0.9fr)" },
{ key: "arr", header: "ARR", width: "6.5rem", align: "end", mono: true },
];
const rows: DiffTableRow[] = [
{ id: "northwind", values: { account: "Northwind Freight", owner: "Priya Raman", plan: "Scale", arr: "$148,000" } },
{
id: "halcyon",
values: { account: "Halcyon Dental", owner: "Marcus Oyelaran", plan: "Team", arr: "$18,400" },
change: { type: "changed", values: { owner: "Priya Raman" } },
},
{
id: "bramble",
values: { account: "Bramble & Rye", owner: "Ines Carvalho", plan: "Team", arr: "$22,900" },
change: { type: "changed", values: { plan: "Scale", arr: "$61,200" } },
},
{ id: "bramble-dup", values: { account: "Bramble and Rye", owner: "Ines Carvalho", plan: "Team", arr: "$22,900" }, change: { type: "removed" } },
{ id: "kestrel", values: { account: "Kestrel Robotics", owner: "Tomas Lindqvist", plan: "Enterprise", arr: "$310,000" } },
{ id: "osprey", values: { account: "Osprey Health", owner: "Marcus Oyelaran", plan: "Team", arr: "$27,500" }, change: { type: "added" } },
];
export default function DiffTableExample() {
return (
<DiffTable
className="max-w-2xl"
title="Proposed changes to Accounts"
description="From the renewal cleanup: reassign one owner, upgrade Bramble & Rye, merge a duplicate, add a signed deal."
columns={columns}
rows={rows}
onUndo={() => {}}
/>
);
}API
| Prop | Type | Default | Description |
|---|---|---|---|
| columns | { key, header, width?, align?, mono? }[] | — | Columns. width is a grid track such as "minmax(0,2fr)" or "7rem". |
| rows | { id, values, change? }[] | — | Rows in order. change is { type: "added" }, { type: "removed" } or { type: "changed", values }; rows without one are context. |
| title / description | ReactNode | "Proposed changes" | Header text. |
| rowLabel | (row) => string | the first column's text | Names a row in its toggle's label. |
| selected / defaultSelected / onSelectedChange | string[] | all changes | Ids of the included changes. |
| phase / defaultPhase / onPhaseChange | "review" | "applied" | "discarded" | "review" | Review shows the diff; applied and discarded show the folded result. |
| onApply | (ids) => void | — | Called with the included change ids when Apply is pressed. |
| onDiscard | () => void | — | Called when Discard is pressed. |
| onUndo | () => void | — | Shows an Undo button after applying or discarding, which returns to review. |
Accessibility
- The grid uses role="table", "row", "columnheader" and "cell". Each change has a hidden sign label (Added, Removed, Changed), and changed cells read as "Was" and "Now".
- Each change has a Base UI checkbox labelled "Include changed row Halcyon Dental" and so on; pressing anywhere on the row toggles it too.
- Apply and Discard are buttons; the result ("Applied 3 changes.") is announced through a polite live region. Folded rows are aria-hidden.
- Narrow screens scroll the table sideways inside the card instead of widening the page.
- Under reduced motion the folds, tints and footer swap are instant.
Files
Installed into src/components/shaken, or components/shaken when the project has no src directory.
src/components/shaken/agent/diff-table.tsxdiff-table- Also installs checkbox@0.1.1registry dependency
- npm packages: @base-ui/react@^1.9.0React and Tailwind CSS v4 required
Source
The exact source the CLI installs, from /r/items/react/diff-table/0.1.0.json.
agent/diff-table.tsxShowHide
agent/diff-table.tsx
"use client";
import { type ComponentProps, type CSSProperties, type ReactNode, useEffect, useId, useState } from "react";
import { Checkbox } from "../ui/checkbox";
/*
* Diff table: an agent's proposed edits to table rows, reviewed in place. Added rows are tinted
* green, removed rows red and struck through, and changed cells show the old value struck above
* the new one. Every changed row has its own include toggle.
*
* Applying folds the review into the table: accepted old values and accepted removals collapse
* away, rejected additions and rejected new values collapse away, and the tints fade, so what is
* left is simply the resulting table. Collapses use grid rows (0fr to 1fr), never measured heights.
* Under reduced motion the table switches to the result at once.
*/
export interface DiffTableColumn {
key: string;
header: ReactNode;
/** A grid track, for example "minmax(0,2fr)" or "7rem". Defaults to "minmax(0,1fr)". */
width?: string;
align?: "start" | "end";
/** Mono and tabular figures, for ids, amounts and dates. */
mono?: boolean;
}
export type DiffTableChange = { type: "added" } | { type: "removed" } | { type: "changed"; values: Record<string, ReactNode> };
export interface DiffTableRow {
id: string;
/** The row as it is today (for added rows, the proposed row). */
values: Record<string, ReactNode>;
/** The proposed change. Rows without one are shown as context. */
change?: DiffTableChange;
}
export type DiffTablePhase = "review" | "applied" | "discarded";
export interface DiffTableProps extends Omit<ComponentProps<"section">, "title"> {
columns: DiffTableColumn[];
rows: DiffTableRow[];
title?: ReactNode;
description?: ReactNode;
/** Names a row for its toggle's label. Defaults to the first column's text. */
rowLabel?: (row: DiffTableRow) => string;
/** Ids of changed rows that start included. Defaults to all of them. */
defaultSelected?: string[];
selected?: string[];
onSelectedChange?: (ids: string[]) => void;
phase?: DiffTablePhase;
defaultPhase?: DiffTablePhase;
onPhaseChange?: (phase: DiffTablePhase) => void;
/** Called with the included change ids when Apply is pressed. */
onApply?: (ids: string[]) => void;
/** Called when Discard is pressed. */
onDiscard?: () => void;
/** Shows an Undo button after applying or discarding, which returns to review. */
onUndo?: () => void;
}
function cx(...classes: (string | false | null | undefined)[]) {
return classes.filter(Boolean).join(" ");
}
const ease = "ease-[cubic-bezier(0.23,1,0.32,1)]";
/** A grid-rows disclosure: 1fr when shown, 0fr when folded. */
function Fold({ open, delay = 0, className, children }: { open: boolean; delay?: number; className?: string; children: ReactNode }) {
return (
<div
role="presentation"
aria-hidden={!open || undefined}
className={cx("grid grid-cols-[minmax(0,1fr)] transition-[grid-template-rows,opacity] duration-[260ms] motion-reduce:transition-none", ease, open ? "grid-rows-[1fr] opacity-100" : "grid-rows-[0fr] opacity-0", className)}
style={{ transitionDelay: `${delay}ms` }}
>
<div role="presentation" className="min-h-0 overflow-hidden">
{children}
</div>
</div>
);
}
const signs = {
added: { char: "+", label: "Added", tone: "text-emerald-600 dark:text-emerald-400" },
removed: { char: "−", label: "Removed", tone: "text-red-600 dark:text-red-400" },
changed: { char: "~", label: "Changed", tone: "text-amber-600 dark:text-amber-400" },
} as const;
function textOf(node: ReactNode): string {
if (typeof node === "string" || typeof node === "number") return String(node);
return "";
}
/** An agent's proposed edits to table rows, with a toggle per change and an Apply footer. */
export function DiffTable({
columns,
rows,
title = "Proposed changes",
description,
rowLabel,
defaultSelected,
selected: selectedProp,
onSelectedChange,
phase: phaseProp,
defaultPhase = "review",
onPhaseChange,
onApply,
onDiscard,
onUndo,
className,
...props
}: DiffTableProps) {
const changeIds = rows.filter((row) => row.change).map((row) => row.id);
const [selectedState, setSelectedState] = useState<string[]>(() => defaultSelected ?? changeIds);
const selected = selectedProp ?? selectedState;
const [phaseState, setPhaseState] = useState<DiffTablePhase>(defaultPhase);
const phase = phaseProp ?? phaseState;
const reviewing = phase === "review";
const headingId = useId();
// The count that Apply used, so the footer keeps saying "Applied 3" after the fold.
const [resultCount, setResultCount] = useState(0);
const [announcement, setAnnouncement] = useState("");
function setSelected(next: string[]) {
if (selectedProp === undefined) setSelectedState(next);
onSelectedChange?.(next);
}
function setPhase(next: DiffTablePhase) {
if (phaseProp === undefined) setPhaseState(next);
onPhaseChange?.(next);
}
function toggle(id: string, include: boolean) {
if (!reviewing) return;
setSelected(include ? [...selected.filter((s) => s !== id), id] : selected.filter((s) => s !== id));
}
const included = changeIds.filter((id) => selected.includes(id));
const counts = { added: 0, removed: 0, changed: 0 };
for (const row of rows) if (row.change) counts[row.change.type]++;
useEffect(() => {
if (phase === "review") setAnnouncement("");
}, [phase]);
const template = ["1.75rem", ...columns.map((c) => c.width ?? "minmax(0,1fr)"), "2.75rem"].join(" ");
const gridStyle: CSSProperties = { gridTemplateColumns: template };
const labelFor = (row: DiffTableRow) => rowLabel?.(row) ?? (textOf(row.values[columns[0]?.key ?? ""]) || row.id);
return (
<section
aria-labelledby={headingId}
data-phase={phase}
className={cx(
"w-full overflow-hidden rounded-xl border border-zinc-950/10 bg-white text-left text-zinc-950 shadow-[0_1px_2px_rgb(0_0_0/0.04)]",
"dark:border-white/10 dark:bg-black dark:text-white dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)]",
className,
)}
{...props}
>
<header className="flex flex-wrap items-start justify-between gap-x-6 gap-y-2 px-4 pt-4 pb-3">
<div className="min-w-0">
<h3 id={headingId} className="text-sm font-semibold">
{title}
</h3>
{description ? <p className="mt-0.5 text-sm text-zinc-600 dark:text-zinc-400">{description}</p> : null}
</div>
<p className="flex items-center gap-3 font-mono text-xs tabular-nums">
<span className={signs.added.tone}>
+{counts.added}
<span className="sr-only"> added,</span>
</span>
<span className={signs.removed.tone}>
{"−"}
{counts.removed}
<span className="sr-only"> removed,</span>
</span>
<span className={signs.changed.tone}>
~{counts.changed}
<span className="sr-only"> changed</span>
</span>
</p>
</header>
{/* inline-size containment keeps the table's minimum width from widening the page; it scrolls instead. */}
<div className="overflow-x-auto [contain:inline-size]">
<div role="table" aria-labelledby={headingId} className="relative min-w-[34rem] text-sm">
<div role="rowgroup">
<div role="row" className="grid border-y border-zinc-950/[0.07] dark:border-white/[0.08]" style={gridStyle}>
<span role="columnheader" className="sr-only">
Change
</span>
{columns.map((column, i) => (
<span
key={column.key}
role="columnheader"
className={cx("truncate py-2 pr-3 text-xs font-medium text-zinc-500", column.align === "end" && "text-right", i === 0 && "col-start-2")}
>
{column.header}
</span>
))}
<span role="columnheader" className="sr-only">
Include
</span>
</div>
</div>
<div role="rowgroup">
{rows.map((row, index) => (
<Row
key={row.id}
row={row}
columns={columns}
gridStyle={gridStyle}
phase={phase}
included={included.includes(row.id)}
label={labelFor(row)}
last={index === rows.length - 1}
onToggle={(include) => toggle(row.id, include)}
/>
))}
</div>
</div>
</div>
<footer className="relative border-t border-zinc-950/[0.07] dark:border-white/[0.08]">
<span role="status" className="sr-only">
{announcement}
</span>
<div
inert={!reviewing || undefined}
className={cx(
"flex flex-wrap items-center justify-between gap-3 px-4 py-3 transition-[opacity,translate] duration-200 motion-reduce:transition-none",
ease,
reviewing ? "opacity-100" : "pointer-events-none translate-y-1 opacity-0",
)}
>
<p className="text-xs text-zinc-500 tabular-nums">
{included.length} of {changeIds.length} changes selected
</p>
<div className="ml-auto flex items-center gap-2">
<button
type="button"
onClick={() => {
setResultCount(changeIds.length);
setAnnouncement(`Discarded ${changeIds.length} proposed changes.`);
setPhase("discarded");
onDiscard?.();
}}
className={cx(
"inline-flex h-8 items-center rounded-lg px-3 text-sm font-medium text-zinc-600 select-none dark:text-zinc-400",
"transition-[background-color,color,scale] duration-150 ease-out hover:bg-zinc-950/[0.04] hover:text-zinc-950 motion-safe:active:scale-[0.97] motion-reduce:transition-none",
"dark:hover:bg-white/[0.06] dark:hover:text-white",
"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500",
)}
>
Discard
</button>
<button
type="button"
disabled={included.length === 0}
onClick={() => {
setResultCount(included.length);
setAnnouncement(`Applied ${included.length} ${included.length === 1 ? "change" : "changes"}.`);
setPhase("applied");
onApply?.(included);
}}
className={cx(
"inline-flex h-8 items-center gap-2 rounded-lg px-3 text-sm font-medium whitespace-nowrap select-none",
"bg-zinc-950 text-white shadow-[inset_0_1px_0_rgb(255_255_255/0.12),0_1px_2px_rgb(0_0_0/0.2)] hover:bg-zinc-800",
"dark:bg-white dark:text-black dark:shadow-[inset_0_-1px_0_rgb(0_0_0/0.12)] dark:hover:bg-zinc-200",
"transition-[background-color,scale,opacity] duration-150 ease-out motion-safe:active:scale-[0.97] motion-reduce:transition-none",
"disabled:pointer-events-none disabled:opacity-40",
"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500",
)}
>
Apply <span className="tabular-nums">{included.length}</span> {included.length === 1 ? "change" : "changes"}
</button>
</div>
</div>
<div
inert={reviewing || undefined}
className={cx(
"absolute inset-0 flex items-center justify-between gap-3 px-4 transition-[opacity,translate] duration-200 motion-reduce:transition-none",
ease,
reviewing ? "pointer-events-none translate-y-1 opacity-0" : "opacity-100 delay-200",
)}
>
<p className="flex items-center gap-2 text-sm">
<svg aria-hidden="true" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" className={cx("size-4", phase === "applied" ? "text-emerald-600 dark:text-emerald-400" : "text-zinc-500")}>
{phase === "discarded" ? <path d="m4.5 4.5 7 7m0-7-7 7" /> : <path d="m3.5 8.5 3 3 6-7" />}
</svg>
<span className="text-zinc-600 dark:text-zinc-400">
{phase === "discarded" ? "Discarded" : "Applied"} <span className="tabular-nums">{resultCount}</span> {resultCount === 1 ? "change" : "changes"}
</span>
</p>
{onUndo ? (
<button
type="button"
onClick={() => {
setPhase("review");
onUndo();
}}
className={cx(
"inline-flex h-8 items-center rounded-lg border border-zinc-950/12 bg-white px-3 text-sm font-medium select-none",
"transition-[background-color,border-color,scale] duration-150 ease-out hover:border-zinc-950/20 hover:bg-zinc-950/[0.03] motion-safe:active:scale-[0.97] motion-reduce:transition-none",
"dark:border-white/15 dark:bg-black dark:hover:border-white/25 dark:hover:bg-white/[0.06]",
"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500",
)}
>
Undo
</button>
) : null}
</div>
</footer>
</section>
);
}
function Row({
row,
columns,
gridStyle,
phase,
included,
label,
last,
onToggle,
}: {
row: DiffTableRow;
columns: DiffTableColumn[];
gridStyle: CSSProperties;
phase: DiffTablePhase;
included: boolean;
label: string;
last: boolean;
onToggle: (include: boolean) => void;
}) {
const type = row.change?.type;
const reviewing = phase === "review";
// What the row resolves to: did this change happen?
const accepted = phase === "applied" && included;
const settled = !reviewing;
// The row disappears when a removal is accepted or an addition is not.
const gone = settled && ((type === "removed" && accepted) || (type === "added" && !accepted));
const dimmed = reviewing && type != null && !included;
const tint =
!settled && type === "added"
? "bg-emerald-500/[0.07]"
: !settled && type === "removed"
? "bg-red-500/[0.06]"
: "bg-transparent";
return (
<Fold open={!gone} delay={gone ? 120 : 0}>
<div
role="row"
aria-hidden={gone || undefined}
onClick={(event) => {
// The whole row toggles on press; the checkbox stays the keyboard path.
if (!type || !reviewing) return;
if ((event.target as HTMLElement).closest("button,a,input,label")) return;
onToggle(!included);
}}
className={cx(
"grid items-center",
!last && "border-b border-zinc-950/[0.07] dark:border-white/[0.08]",
"transition-[background-color,opacity,filter] duration-300 motion-reduce:transition-none",
ease,
tint,
type && reviewing && "cursor-pointer hover:bg-zinc-950/[0.03] dark:hover:bg-white/[0.04]",
type === "added" && reviewing && "hover:bg-emerald-500/[0.1] dark:hover:bg-emerald-500/[0.1]",
type === "removed" && reviewing && "hover:bg-red-500/[0.09] dark:hover:bg-red-500/[0.09]",
dimmed && "opacity-50 grayscale",
)}
style={gridStyle}
>
<span role="cell" className={cx("py-2.5 pl-4 font-mono text-xs leading-5 transition-opacity duration-200 motion-reduce:transition-none", type && signs[type].tone, settled && "opacity-0")}>
{type ? (
<>
<span aria-hidden="true">{signs[type].char}</span>
<span className="sr-only">{signs[type].label}</span>
</>
) : null}
</span>
{columns.map((column) => (
<Cell key={column.key} row={row} column={column} type={type} settled={settled} accepted={accepted} />
))}
<span role="cell" className="flex justify-center py-2.5">
{type ? (
<span className={cx("transition-opacity duration-200 motion-reduce:transition-none", settled && "opacity-0")}>
<Checkbox
checked={included}
disabled={!reviewing}
onCheckedChange={(checked) => onToggle(checked === true)}
aria-label={`Include ${signs[type].label.toLowerCase()} row ${label}`}
/>
</span>
) : null}
</span>
</div>
</Fold>
);
}
function Cell({
row,
column,
type,
settled,
accepted,
}: {
row: DiffTableRow;
column: DiffTableColumn;
type: DiffTableChange["type"] | undefined;
settled: boolean;
accepted: boolean;
}) {
const before = row.values[column.key];
const proposed = row.change?.type === "changed" ? row.change.values : undefined;
const changed = proposed != null && column.key in proposed;
const after = changed ? proposed[column.key] : undefined;
const base = cx("min-w-0 py-2.5 pr-3 leading-5", column.align === "end" && "text-right", column.mono && "font-mono text-[13px] tabular-nums");
if (!changed) {
const struck = type === "removed" && !settled;
return (
<span role="cell" className={base}>
<span
className={cx(
"block truncate decoration-red-500/60 transition-[color] duration-300 motion-reduce:transition-none",
struck ? "text-zinc-500 line-through" : type === "removed" ? "text-zinc-950 dark:text-white" : "text-zinc-800 dark:text-zinc-200",
)}
>
{before}
</span>
</span>
);
}
// Changed: old value struck above the new one. Settling folds away whichever side lost.
const keepOld = settled && !accepted;
const keepNew = !settled || accepted;
return (
<span role="cell" className={base}>
<Fold open={!settled || keepOld}>
<span
className={cx(
"block truncate transition-[color] duration-300 motion-reduce:transition-none",
settled ? "text-zinc-800 dark:text-zinc-200" : "text-xs text-zinc-500 line-through decoration-red-500/60",
)}
>
<span className="sr-only">{settled ? "" : "Was "}</span>
{before}
</span>
</Fold>
<Fold open={keepNew}>
<span className={cx("block truncate font-medium transition-[color] duration-300 motion-reduce:transition-none", settled ? "text-zinc-800 dark:text-zinc-200 font-normal" : "text-zinc-950 dark:text-white")}>
<span className="sr-only">{settled ? "" : "Now "}</span>
{after}
</span>
</Fold>
</span>
);
}