Field diff

Slant / Content

Two versions of a record field by field: before, after and what changed, with the unchanged rows hidden on request.

Demo

Usage

@tuttn/slant
import { FieldDiff, type FieldDiffField } from '@tuttn/slant'

// Strings, numbers and booleans are compared for you: a missing before is
// added, a missing after removed. Pass change for values that are elements.
const fields: FieldDiffField[] = [
  { id: 'title', label: 'Title', before: 'Spring sale', after: 'Spring sale, 20% off' },
  { id: 'slug', label: 'Slug', before: 'spring', after: 'spring' },
  { id: 'hero', label: 'Hero image', after: <img src={hero} alt="Hero" />, change: 'added' },
]

// Uncontrolled: a Switch above the table hides the unchanged rows.
<FieldDiff label="Changes to Spring sale" fields={fields} defaultHideUnchanged toggle />

// Controlled: you hold the state, say to keep it in the URL or drive it from
// your own control. Name the columns after the versions.
const [hide, setHide] = useState(true)
<FieldDiff label="Revision 12 against 14" fields={fields}
  hideUnchanged={hide} onHideUnchangedChange={setHide} toggle
  beforeLabel="Revision 12" afterLabel="Revision 14" />

// A heading you show names the table instead of the hidden caption.
<h3 id="changes">Changes to Spring sale</h3>
<FieldDiff aria-labelledby="changes" fields={fields} />

Props

PropTypeDefaultDescription
fieldsreadonly FieldDiffField[]—The fields, in order: `{ id, label, before?, after?, change? }`.
labelstring—The table's caption, which names it for screen readers and is visually hidden. Required unless `aria-labelledby` points at a heading you show.
hideUnchangedboolean—Whether unchanged fields are hidden. Pass it to control them; leave it out to let the component keep its own.
defaultHideUnchangedbooleanfalseWhether unchanged fields start hidden when uncontrolled.
onHideUnchangedChange(hide: boolean) => void—Called with the next state when the Switch flips it.
togglebooleanfalseShows a "Hide unchanged" Switch (`messages.diffHideUnchanged`) above the table.
beforeLabelstringmessages.beforeThe old version's column header, and its label on a stacked row.
afterLabelstringmessages.afterThe new version's column header, and its label on a stacked row.
…restHTMLAttributes<HTMLDivElement>—className, style, the ref and other attributes go on the root <div>; aria-label, aria-labelledby and aria-describedby go on the <table>.

Keyboard

Tabreaches the “Hide unchanged” Switch (with toggle), then anything focusable in the values
Space / Enteron the Switch, hides or shows the unchanged rows

Accessibility

  • A real <table>: label is its caption, visually hidden, and each field is a row header (th scope="row"), so a screen reader reads a cell with its field and its column, “Title, After, Spring sale, 20% off”.
  • What changed is a word in a Badge (messages.diffAdded, diffRemoved, diffChanged, diffUnchanged), never colour alone. The tinted value behind a changed side is a cue on top of that, its text at --sl-text so it keeps its contrast in Paper.
  • A missing side shows a muted dash, hidden from screen readers, which hear messages.noValue (“Not set”) instead.
  • The Switch is named by its visible “Hide unchanged” label (messages.diffHideUnchanged), and clicking the label flips it. With rows hidden, a line under the table says how many (messages.diffHiddenFields).
  • Stacked in a narrow container, the column headers stay in the table for screen readers, and each cell shows its side’s label for sighted readers, hidden from screen readers so it isn’t read twice.

What counts as a change

Without change, a field whose before is missing (undefined or null) was added, one whose after is missing was removed, and two strings, numbers or booleans compare with Object.is, so NaN equals itself and 0 and -0 differ. Anything else, an element above all, counts as changed: pass change when you know better. An empty string is a value for the comparison but shows as missing. A boolean shows as true or false; pass your own text for anything friendlier.

Hiding unchanged rows

hideUnchanged, defaultHideUnchanged and onHideUnchangedChange hold the state, and toggle adds the Switch that flips it. Without the Switch you can still drive it from your own control. With every row hidden or no fields at all, the table shows one row reading messages.diffNoChanges.

Layout

The root is a named size container: under 30rem each row stacks into a block, its field as the heading with the Badge beside it, then the before and after values under their labels. Values wrap anywhere, so a long slug or URL never widens the table; size images and other rich values yourself.