Skip to content

Commit 7024420

Browse files
committed
fix(web): move sidebar shelves as one block
1 parent ec5ede5 commit 7024420

2 files changed

Lines changed: 132 additions & 34 deletions

File tree

‎apps/web/src/components/Sidebar.motion.test.ts‎

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,16 @@ describe("sidebar list motion", () => {
281281
layout([b, fresh]);
282282
motion.update(true);
283283
expect(a.animations[0]!.cancel).toHaveBeenCalledOnce();
284-
expect(fresh.animate).toHaveBeenLastCalledWith([{ opacity: 0 }, { opacity: 1 }], {
285-
duration: 150,
286-
easing: "ease-out",
287-
});
284+
expect(fresh.animate).toHaveBeenLastCalledWith(
285+
[
286+
{ opacity: 0, transform: "translateY(0px)" },
287+
{ opacity: 1, transform: "translateY(0px)" },
288+
],
289+
{
290+
duration: 150,
291+
easing: "ease-out",
292+
},
293+
);
288294
const clone = a.clones[0]!;
289295
expect(clone.style).toMatchObject({
290296
position: "absolute",
@@ -299,10 +305,16 @@ describe("sidebar list motion", () => {
299305
expect(clone.attributes).toEqual([{ name: "aria-hidden", value: "true" }]);
300306
expect(clone.children[0]!.attributes).toEqual([{ name: "data-state", value: "open" }]);
301307
expect(clone.children[1]!.attributes).toEqual(icon.attributes);
302-
expect(clone.animate).toHaveBeenCalledWith([{ opacity: 1 }, { opacity: 0 }], {
303-
duration: 150,
304-
easing: "ease-out",
305-
});
308+
expect(clone.animate).toHaveBeenCalledWith(
309+
[
310+
{ opacity: 1, transform: "translateY(0px)" },
311+
{ opacity: 0, transform: "translateY(0px)" },
312+
],
313+
{
314+
duration: 150,
315+
easing: "ease-out",
316+
},
317+
);
306318
expect(parent.children.includes(clone)).toBe(true);
307319
motion.update(true);
308320
expect(clone.animations).toHaveLength(1);
@@ -311,6 +323,38 @@ describe("sidebar list motion", () => {
311323
expect(parent.children.includes(clone)).toBe(false);
312324
});
313325

326+
it("rides entering rows on the shelf displacement so an opened shelf moves as one block", () => {
327+
const a = new TestRow("a", 40);
328+
const header = new TestRow("header", 32);
329+
const x = new TestRow("x", 36);
330+
const y = new TestRow("y", 36);
331+
const z = new TestRow("z", 36);
332+
const { motion, layout } = fixture([a, header, x]);
333+
motion.update(true);
334+
// The shelf is anchored below the list, so two revealed rows lift the
335+
// header and its existing rows by the same 72px.
336+
layout([a, header, x, y, z]);
337+
for (const row of [header, x, y, z]) row.offsetTop -= 72;
338+
motion.update(true);
339+
expectMove(header, 72);
340+
expectMove(x, 72);
341+
expect(a.animate).not.toHaveBeenCalled();
342+
expect(y.animate).toHaveBeenLastCalledWith(
343+
[
344+
{ opacity: 0, transform: "translateY(72px)" },
345+
{ opacity: 1, transform: "translateY(0px)" },
346+
],
347+
{ duration: 150, easing: "ease-out" },
348+
);
349+
expect(z.animate).toHaveBeenLastCalledWith(
350+
[
351+
{ opacity: 0, transform: "translateY(72px)" },
352+
{ opacity: 1, transform: "translateY(0px)" },
353+
],
354+
{ duration: 150, easing: "ease-out" },
355+
);
356+
});
357+
314358
it("clears exit clones on pickup and does not fade the release commit", () => {
315359
const [a, b, c] = [new TestRow("a"), new TestRow("b"), new TestRow("c")];
316360
const { motion, layout, parent } = fixture([a, b]);
@@ -329,10 +373,16 @@ describe("sidebar list motion", () => {
329373
expect(c.animations).toHaveLength(0);
330374
layout([c, a]);
331375
motion.update(true);
332-
expect(a.animate).toHaveBeenCalledWith([{ opacity: 0 }, { opacity: 1 }], {
333-
duration: 150,
334-
easing: "ease-out",
335-
});
376+
expect(a.animate).toHaveBeenCalledWith(
377+
[
378+
{ opacity: 0, transform: "translateY(0px)" },
379+
{ opacity: 1, transform: "translateY(0px)" },
380+
],
381+
{
382+
duration: 150,
383+
easing: "ease-out",
384+
},
385+
);
336386
motion.dispose();
337387
expect(a.animations.at(-1)!.cancel).toHaveBeenCalledOnce();
338388
});
@@ -349,10 +399,16 @@ describe("sidebar list motion", () => {
349399
motion.update(true);
350400
expect(marker.clones).toHaveLength(0);
351401
const clone = a.clones[0]!;
352-
expect(clone.animate).toHaveBeenCalledWith([{ opacity: 0.4 }, { opacity: 0 }], {
353-
duration: 150,
354-
easing: "ease-out",
355-
});
402+
expect(clone.animate).toHaveBeenCalledWith(
403+
[
404+
{ opacity: 0.4, transform: "translateY(0px)" },
405+
{ opacity: 0, transform: "translateY(40px)" },
406+
],
407+
{
408+
duration: 150,
409+
easing: "ease-out",
410+
},
411+
);
356412
motion.update(false);
357413
expect(parent.children).toEqual([]);
358414
expect(clone.animations[0]!.cancel).toHaveBeenCalledOnce();

‎apps/web/src/components/Sidebar.motion.ts‎

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
const motionTiming = { duration: 150, easing: "ease-out" };
2+
// Rows normally ride their displaced neighbour's travel. Absent a moving
3+
// neighbour, a row still travels on its own, clamped so a tall card does not
4+
// slide its full height.
5+
const rowTravel = (height: number) => Math.min(height, 40);
26
// A project filter change or a bulk snooze swaps a large part of the list at
37
// once. Fades are the expensive part: every removed row gets a deep clone and
48
// every clone and entering row gets its own animation, and the layout reads
@@ -39,7 +43,7 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
3943
entering.clear();
4044
exiting.clear();
4145
};
42-
const fadeOut = (node: HTMLElement, position: RowPosition) => {
46+
const fadeOut = (node: HTMLElement, position: RowPosition, travel: number) => {
4347
if (position.height === 0) return;
4448
// React owns the removed row; only a noninteractive copy stays for the fade.
4549
const clone = node.cloneNode(true) as HTMLElement;
@@ -72,8 +76,12 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
7276
});
7377
parent.append(clone);
7478
const entry = entering.get(node);
79+
const entryProgress = entry ? progress(entry) : 1;
7580
const animation = clone.animate(
76-
[{ opacity: entry ? progress(entry) : 1 }, { opacity: 0 }],
81+
[
82+
{ opacity: entryProgress, transform: "translateY(0px)" },
83+
{ opacity: 0, transform: `translateY(${travel}px)` },
84+
],
7785
motionTiming,
7886
);
7987
exiting.set(clone, animation);
@@ -146,25 +154,59 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
146154
fadeCount <= MAX_FADED_ROWS_PER_UPDATE;
147155
if (!shouldAnimate) clearFades();
148156
else {
157+
// A shelf that opens above its collapsed anchor shifts every retained
158+
// row by the same amount. Entering rows take that same displacement so
159+
// the shelf arrives as one moving block instead of rows popping into
160+
// their final slots; exiting rows leave by it.
161+
const movedDelta = new Map<HTMLElement, number>();
162+
for (const [node, position] of next) {
163+
const previousTop = positions!.get(node)?.top;
164+
if (previousTop === undefined || previousTop === position.top) continue;
165+
movedDelta.set(node, previousTop + remainingOffset(node) - position.top);
166+
}
167+
const nextOrder = [...next.keys()];
168+
const oldOrder = [...positions!.keys()];
169+
const ridingDelta = (
170+
order: readonly HTMLElement[],
171+
index: number,
172+
retained: (node: HTMLElement) => boolean,
173+
) => {
174+
for (let cursor = index - 1; cursor >= 0; cursor--) {
175+
const node = order[cursor]!;
176+
const delta = movedDelta.get(node);
177+
if (delta !== undefined) return delta;
178+
if (retained(node)) return 0;
179+
}
180+
return undefined;
181+
};
149182
for (const [node, position] of positions!) {
150-
if (!next.has(node)) fadeOut(node, position);
183+
if (next.has(node)) continue;
184+
const delta = ridingDelta(oldOrder, oldOrder.indexOf(node), (n) => next.has(n));
185+
fadeOut(node, position, delta === undefined ? rowTravel(position.height) : -delta);
151186
}
152-
}
153-
for (const [node, animation] of entering) {
154-
if (!next.has(node)) {
155-
animation.cancel();
156-
entering.delete(node);
187+
for (const [node, animation] of entering) {
188+
if (!next.has(node)) {
189+
animation.cancel();
190+
entering.delete(node);
191+
}
157192
}
158-
}
159-
for (const node of running.keys()) {
160-
if (!shouldAnimate || !next.has(node)) cancel(node);
161-
}
162-
if (shouldAnimate) {
163-
for (const [node, position] of next) {
164-
const previousTop = positions?.get(node)?.top;
193+
for (const node of running.keys()) {
194+
if (!shouldAnimate || !next.has(node)) cancel(node);
195+
}
196+
for (const [index, node] of nextOrder.entries()) {
197+
const position = next.get(node)!;
198+
const previousTop = positions!.get(node)?.top;
165199
if (previousTop === undefined) {
166200
if (position.height > 0) {
167-
const animation = node.animate([{ opacity: 0 }, { opacity: 1 }], motionTiming);
201+
const delta = ridingDelta(nextOrder, index, (n) => positions!.has(n));
202+
const travel = delta === undefined ? -rowTravel(position.height) : delta;
203+
const animation = node.animate(
204+
[
205+
{ opacity: 0, transform: `translateY(${travel}px)` },
206+
{ opacity: 1, transform: "translateY(0px)" },
207+
],
208+
motionTiming,
209+
);
168210
entering.set(node, animation);
169211
animation.addEventListener(
170212
"finish",
@@ -176,10 +218,10 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
176218
}
177219
continue;
178220
}
179-
if (previousTop === position.top) continue;
221+
const delta = movedDelta.get(node);
180222
// Computed progress includes the effect's easing. Only our own
181223
// translate is carried forward; dnd-kit's transforms are never read.
182-
move(node, previousTop + remainingOffset(node) - position.top);
224+
if (delta !== undefined) move(node, delta);
183225
}
184226
}
185227
if (released !== null) {

0 commit comments

Comments
 (0)