Change the layout, sort it, filter it, pull one item out to full width. Every move is a transform animated from where the element used to be to nothing, so layout runs once and the animation runs on the compositor. Interrupt it mid-flight — press two buttons fast — and nothing jumps, because each element starts from where it currently looks.
Click an item to pull it to the front of the order. It is tab-reachable and Enter does the same thing, which is the part a drag-only version of this loses.
function flip(nodes, change){
if (matchMedia("(prefers-reduced-motion:reduce)").matches) return change();
const first = new Map();
for (const n of nodes) first.set(n, n.getBoundingClientRect());
change(); /* the DOM moves. Layout once. */
for (const n of nodes){
const f = first.get(n), l = n.getBoundingClientRect();
if (!f || !l.width) continue;
const dx = f.left - l.left, dy = f.top - l.top;
const sx = f.width / l.width, sy = f.height / l.height;
if (!dx && !dy && Math.abs(sx-1) < .01 && Math.abs(sy-1) < .01) continue;
n.animate(
[{ transform:`translate(${dx}px,${dy}px) scale(${sx},${sy})` },
{ transform:"none" }],
{ duration:400, easing:"cubic-bezier(.05,.7,.1,1)",
composite:"replace", fill:"none" }
);
}
}transform-origin is set to the top left
in CSS so the two scales are independent and the box does not drift. Running animations are
cancelled first, which is what makes an interruption continue smoothly instead of snapping.