React diff viewer

Two ways to show a code change in React: a diff you can watch happen, and two versions behind a divider you drag. Both are plain React and CSS, with nothing else to install.

Ship it
src/cart/total.ts +4−3
  1. import { rates } from "./rates";
  2. export function total(items: Item[]) {
  3. export function total(items: Item[], region = "GB") {
  4. let sum = 0;
  5. for (const item of items) {
  6. sum += item.price;
  7. sum += item.price * item.count;
  8. }
  9. return sum;
  10. const tax = rates[region] ?? 0;
  11. return sum * (1 + tax);
  12. }
Charge tax by regiona1f93c2
Code diffBlocks
Before
const response = await fetch(url);
if (!response.ok) {
  throw new Error("Request failed");
}
const data = await response.json();
const items = data.items.filter(
  (item) => item.active
);
setItems(items);
setLoading(false);
Code compareBlocksPro

What makes a good one

  1. Before, the change, after

    A reader wants to see what the code was, what moved and what it is now. Code diff switches between the three.

  2. Mark the words too

    A changed line is easier to read when the changed words inside it are lit, not only the line.

  3. Motion that explains

    Removed lines fold away and new ones open up, so the eye can follow what happened.

Questions

Does it work out the diff for me?

No. These show a change that you write out: which lines were removed, added and kept. To compare two files as they are, work out the lines with a library such as jsdiff and pass them in.

What is it good for?

Showing one change well: a release note, a step in a tutorial, or a landing page for a developer tool.

Is the code free?

Code diff is free, in React and HTML. Code compare comes with Pro.