Version compare
The versions of a document or record in a list: pick two and see what changed.
import { VersionCompare } from "@forgeron/ui";- v4Current versionCamille Roy · Nov 18, 15:40
- v3Nov 14, 09:12Léa MartinBudget raised
- v2Nov 9, 17:30Hugo BernardBigger goal
- v1Nov 2, 11:05Léa Martin
v3v4
| 8 | 8 | 1. Announce on the blog | |
| 9 | 9 | 2. Email the waiting list | |
| 10 | 10 | 3. Open the community | |
| 11 | + | 4. Press briefing | |
import { VersionCompare, type CompareVersion } from "@forgeron/ui";
import { useState } from "react";
const page = (n: number) =>
[
"# Launch plan",
"",
n >= 2 ? "Goal: 500 sign-ups in the first month." : "Goal: 300 sign-ups.",
"Owner: Léa Martin",
n >= 3 ? "Budget: 12 000 EUR, reviewed weekly." : "Budget: 10 000 EUR.",
"",
"## Steps",
"1. Announce on the blog",
"2. Email the waiting list",
n >= 3 ? "3. Open the community" : null,
n >= 4 ? "4. Press briefing" : null,
]
.filter((l): l is string => l !== null)
.join("\n");
const versions: CompareVersion[] = [
{ id: "v4", label: "v4", at: "Nov 18, 15:40", by: "Camille Roy", content: page(4) },
{
id: "v3",
label: "v3",
at: "Nov 14, 09:12",
by: "Léa Martin",
note: "Budget raised",
content: page(3),
},
{
id: "v2",
label: "v2",
at: "Nov 9, 17:30",
by: "Hugo Bernard",
note: "Bigger goal",
content: page(2),
},
{ id: "v1", label: "v1", at: "Nov 2, 11:05", by: "Léa Martin", content: page(1) },
];
export default function Text() {
const [log, setLog] = useState("");
return (
<div style={{ display: "grid", gap: 8, width: "100%" }}>
<VersionCompare versions={versions} onRestore={(id) => setLog(`Restore ${id}`)} />
{log ? (
<span style={{ color: "var(--ink-dim)", font: "400 11px/16px var(--font-mono)" }}>
{log}
</span>
) : null}
</div>
);
}- v3Current versionMaya Lopez · Nov 18, 10:02
- v2Nov 16, 14:20Hugo BernardDiscount applied
- v1Nov 15, 09:41Hugo Bernard
v2v3
| Field | v2 | v3 |
|---|---|---|
| Total (Changed) | 19200 | 18400 |
| status (Changed) | Draft | Sent |
// The versions of a record: each one's content is its fields, and the diff is a before and after.
import { VersionCompare, type CompareVersion } from "@forgeron/ui";
const versions: CompareVersion[] = [
{
id: "r3",
label: "v3",
at: "Nov 18, 10:02",
by: "Maya Lopez",
content: {
title: "Quote 2026-114",
amount: 18400,
currency: "EUR",
status: "Sent",
terms: "Net 30",
},
},
{
id: "r2",
label: "v2",
at: "Nov 16, 14:20",
by: "Hugo Bernard",
note: "Discount applied",
content: {
title: "Quote 2026-114",
amount: 19200,
currency: "EUR",
status: "Draft",
terms: "Net 30",
},
},
{
id: "r1",
label: "v1",
at: "Nov 15, 09:41",
by: "Hugo Bernard",
content: { title: "Quote 2026-114", amount: 21000, currency: "EUR", status: "Draft" },
},
];
export default function Record() {
return (
<div style={{ width: "100%" }}>
<VersionCompare versions={versions} labels={{ amount: "Total", terms: "Payment terms" }} />
</div>
);
}API → Props
| Prop | Type | Default | Description |
|---|---|---|---|
versions | CompareVersion[] | — | Newest first: { id, label, at, by, note?, content }. content is a string (text diff) or a record (field diff). |
from / defaultFrom / onFromChange | string | — | The older side. Default: the version before the right one. |
to / defaultTo / onToChange | string | — | The newer side. Default: the current version. |
onRestore | (id) => void | — | Makes an older version the current one; leave out on a read-only record. |
labels | Record<string, string> | — | Names of the fields, for records. |
mode / defaultMode / onModeChange | "unified" | "split" | — | The text diff layout. |
className | string | — | Extra classes. |