How the diff-order toggle works

The demo in "I Don't Read Diffs Anymore", written by Claude for this post

Version 1 · familiar

What you're about to read

This covers the demo in the middle of the post: the markup, the 20 or so lines of JavaScript that drive it, and the CSS that changes when you switch. It's for someone who knows HTML, CSS and JavaScript but hasn't seen this site's code.

We'll follow one click the whole way through: a reader clicks "Walkthrough order", and commands/slowmode.py moves from third place to first.

The big picture

Two ideas matter before the code:

The markup

82<figure class="order rv" data-order>1
83 <div class="order-bar">
84 <span class="order-label">A made-up Discord command, five files</span>
85 <div class="order-toggle" role="group" aria-label="File order">
86 <button type="button" data-mode="diff" aria-pressed="true">Diff order</button>
87 <button type="button" data-mode="run" aria-pressed="false">Walkthrough order</button>2
88 </div>
89 </div>
90 <ol class="order-list">
91 <li data-run="3"><code>activity.py</code><span class="order-delta">+24 &minus;2</span><span class="order-why"><span>Counts how many messages the channel gets per minute.</span></span></li>
92 <li data-run="2"><code>checks.py</code><span class="order-delta">+9 &minus;0</span><span class="order-why"><span>Makes sure the person running it is allowed to.</span></span></li>
93 <li data-run="1"><code>commands/slowmode.py</code><span class="order-delta">+31 &minus;4</span><span class="order-why"><span>The command arrives here, and this is where everything else gets called from.</span></span></li>3
94 <li data-run="4"><code>db.py</code><span class="order-delta">+12 &minus;1</span><span class="order-why"><span>Saves the new setting for the channel.</span></span></li>
95 <li data-run="5"><code>responses.py</code><span class="order-delta">+7 &minus;0</span><span class="order-why"><span>Builds the reply the user actually sees.</span></span></li>
96 </ol>
97 <figcaption>Same files, same change. A diff shows them alphabetically. A walkthrough shows them in the order they run.</figcaption>

1 data-order is what the script looks for, so the same demo could be dropped into any post. 2 Each button carries the order it asks for in data-mode, and aria-pressed says which one is active. 3 Every file has a data-run number: its position in the order the code runs. In the page, the files are written alphabetically, which is the diff order.

Switching order

355document.querySelectorAll("[data-order]").forEach((fig) => {
356 const list = fig.querySelector(".order-list");
357 const items = [...list.children];
358 const buttons = fig.querySelectorAll("[data-mode]");
359 const show = (mode) => {
360 const before = new Map(items.map((li) => [li, li.getBoundingClientRect()]));1
361 const sorted = mode === "run" ? [...items].sort((a, b) => a.dataset.run - b.dataset.run) : items;2
362 list.append(...sorted); // real DOM order, so screen readers get it too3
363 fig.classList.toggle("run", mode === "run");4
364 buttons.forEach((b) => b.setAttribute("aria-pressed", b.dataset.mode === mode));5
365 if (reduceMotion) return;
366 sorted.forEach((li) => { // FLIP: start where it was, glide to where it is now
367 const a = before.get(li), b = li.getBoundingClientRect();
368 const dy = a.top - b.top;
369 if (dy) li.animate([{ transform: `translateY(${dy}px)` }, { transform: "none" }],
370 { duration: 650, easing: "cubic-bezier(0.16, 1, 0.3, 1)" });
371 });
372 };
373 buttons.forEach((b) => b.addEventListener("click", () => show(b.dataset.mode)));6
374});

6 wires both buttons to show(), passing the button's data-mode. Inside it, 1 records where every item is on screen before anything moves. 2 picks the new order: sorted by data-run for the walkthrough order, or the original alphabetical list. 3 moves the items in the page; append moves an element that's already in the list instead of copying it. 4 adds or removes the run class that the CSS keys off, and 5 updates which button shows as pressed.

The glide

365 if (reduceMotion) return;1
366 sorted.forEach((li) => { // FLIP: start where it was, glide to where it is now
367 const a = before.get(li), b = li.getBoundingClientRect();2
368 const dy = a.top - b.top;3
369 if (dy) li.animate([{ transform: `translateY(${dy}px)` }, { transform: "none" }],4
370 { duration: 650, easing: "cubic-bezier(0.16, 1, 0.3, 1)" });
371 });

1 skips the animation entirely for people who've asked their system for less motion. The items still reorder, they just don't slide. For everyone else, 2 measures where each item ended up, and 3 works out how far it moved. 4 starts it back at its old position and animates it to none, which is its new position.

Where reduceMotion comes from

It's defined once near the top of the file and shared with the zoom and tilt effects.

84const reduceMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;

What the CSS changes

201.order-list li::before { /* "M" (modified) like git status in diff order, the step number in walkthrough order */
202 counter-increment: step; content: "M";1
203 width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center;
204 font: 500 11px/1 var(--mono); color: var(--ink-3); background: transparent;
205 transition: background 0.4s var(--ease), color 0.4s var(--ease);
206}
207.order.run li::before { content: counter(step); color: var(--bg); background: var(--accent); }2
208.post .order-list code { font: 400 13px/1.3 var(--mono); color: var(--ink); padding: 0; background: none; box-shadow: none; }
209.order-delta { font: 400 12px/1 var(--mono); color: var(--ink-3); transition: opacity 0.3s; }
210.order.run .order-delta { opacity: 0; }3
211.order-why {
212 grid-column: 2 / -1; display: grid; grid-template-rows: 0fr; opacity: 0;4
213 font-size: 13.5px; line-height: 1.45; color: var(--ink-2);
214 transition: grid-template-rows 0.45s var(--ease), opacity 0.3s, margin 0.45s var(--ease);
215}
216.order-why > span { overflow: hidden; }
217.order.run .order-why { grid-template-rows: 1fr; opacity: 1; margin-top: 4px; }5

1 is a CSS counter that numbers each item in the order it currently sits in the page, which is why the numbers come out right after the reorder. In diff order the circle shows a grey M, like git status does for a modified file. 2 swaps that for the step number and fills the circle in, only in run order. 3 fades out the line counts, since those belong to the diff view. The explanation under each file uses the grid rows trick: 4 starts it at 0fr (no height), and 5 opens it to 1fr.

Decisions the AI made

These are choices in the code that the post didn't ask for.

Reordering the page, not just the picture

Web Animations instead of a CSS transition

No step numbers in the HTML

What to check when reviewing

Words used here

FLIP
First, Last, Invert, Play. Measure where something is, move it, then animate it from the old spot to the new one.
CSS counter
A number the browser keeps while drawing the page, which goes up by one for each element that asks it to.
Grid rows trick
Animating a grid row from 0fr to 1fr, which lets something open to its natural height smoothly.

What happens next

Nothing is planned. If a future post needs the same comparison, the figure can be reused with different files and a different data-run order.