Skip to content
EspañolEnglish
All components (244)

Text comparer

Management · DiffViewer · .amset-diff

What it is and when to use it

Compares two texts, one after the other or side by side: line by line and, inside the changed lines, word by word. It serves for two versions of a text, of a document or of a JSON.

When to use it

  • Showing what changed between two versions: a note, a template, a configuration.
  • layout="split" puts the two versions side by side; unified (default) puts the removed and added lines one after the other.
  • format="json" sorts the keys and indents both texts before comparing them, so only what really changes shows.
  • context is the equal lines kept on each side of a change; the rest is folded into a button that opens them. null folds nothing.

When not to

  • Reviewing changes inside the rich text editor: it already has its own review.
  • Comparing structured data field by field: a two-column table.

Keyboard: the table is a region that scrolls with the arrows when it does not fit, and the buttons for folded lines are normal buttons.

Accessibility: the table has a name (label, required). Each changed line carries a sign and a hidden text ("Line added"), so the change does not depend on colour. The counts are announced as status.

Fine appearance: variables --amset-diff-added and --amset-diff-removed (the tone of each side) and --amset-diff-line (the line height).

What it looks like

1 line added1 line removed
1Line removed: Price: €89 Material: 925 silver Delivery: 3 to 5 days Warranty: 1 year
1Line added: Price: €95 Material: 925 silver Delivery: 24 hours Warranty: 2 years Engraving included

<DiffViewer
  label="Changes to the ring's page"
  before={antes}
  after={despues}
  beforeLabel="Before"
  afterLabel="Now"
  layout="split"
  summary
/>

Props

Import it with import { DiffViewer } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
afterrequiredstring—
beforerequiredstring—
labelrequiredstringWhat is being compared: it names the table for whoever uses a screen reader. Required.
afterLabelstring—
beforeLabelstring—
classNamestringMerged with the library classes; it does not replace them.
contextnumber | nullEqual lines kept on each side of a change; the rest is folded. `null` folds nothing. 3 by default.
formatDiffFormat`json` sorts the keys and indents both texts before comparing them, so only what really changes shows.
layoutDiffLayout`unified` puts the removed and the added lines one after the other; `split` puts them side by side.
lineNumbersbooleanThe line numbers. On by default.
summarybooleanThe count of added and removed lines above the table. On by default.

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

CSS variables

These CSS variables are how you adapt it. Declare them on the component's selector, not on :root: the stylesheet redeclares them in the rule itself, and a declaration on the same element always wins.

VariableDefault
--amset-diff-addedvar(--amset-success)
--amset-diff-removedvar(--amset-error)
--amset-diff-line1.5

How to change it

.amset-diff {
  --amset-diff-added: ...;
  --amset-diff-removed: ...;
}

In the test bench

Every state and variant, to try one at a time, on its page at ui.amset.tech: