Skip to content

Commit 84a9d17

Browse files
committed
fix(web): sidebar rows no longer slide from stale positions after a resize
1 parent ed2254c commit 84a9d17

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@ it("drops in-flight row motion when the document becomes hidden", () => {
5050
document.dispatchEvent(new Event("visibilitychange"));
5151
expect(fixture.list.getAnimations({ subtree: true })).toHaveLength(0);
5252
});
53+
54+
it("does not slide rows from positions measured before a window resize", () => {
55+
using fixture = mountList(["a", "b"]);
56+
using _visibility = vi.spyOn(document, "visibilityState", "get").mockReturnValue("visible");
57+
fixture.rows.get("a")!.style.height = "60px";
58+
window.dispatchEvent(new Event("resize"));
59+
fixture.motion.update(true);
60+
expect(fixture.list.getAnimations({ subtree: true })).toHaveLength(0);
61+
});

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
1616
let positions: Map<HTMLElement, RowPosition> | null = null;
1717
let disposed = false;
1818
const document = parent.ownerDocument;
19-
const reducedMotion = document.defaultView?.matchMedia("(prefers-reduced-motion: reduce)");
19+
const window = document.defaultView;
20+
const reducedMotion = window?.matchMedia("(prefers-reduced-motion: reduce)");
2021
const running = new Map<HTMLElement, { animation: Animation; offset: number }>();
2122
const entering = new Map<HTMLElement, { animation: Animation; travel: number }>();
2223
const exiting = new Map<HTMLElement, Animation>();
@@ -117,13 +118,13 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
117118
};
118119

119120
// Hidden documents freeze the animation timeline, so motion queued while minimized or
120-
// occluded would replay on return instead of having already played.
121+
// occluded would replay on return. Resizes move the bottom-anchored shelves, which
122+
// makes the cached positions stale.
121123
const onVisibilityChange = () => {
122-
if (document.visibilityState !== "hidden") return;
123-
for (const node of running.keys()) cancel(node);
124-
clearFades();
124+
if (document.visibilityState === "hidden") suspend();
125125
};
126126
document.addEventListener("visibilitychange", onVisibilityChange);
127+
window?.addEventListener("resize", suspend);
127128

128129
return {
129130
update(animate: boolean) {
@@ -249,6 +250,7 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
249250
suspend,
250251
dispose() {
251252
document.removeEventListener("visibilitychange", onVisibilityChange);
253+
window?.removeEventListener("resize", suspend);
252254
suspend();
253255
disposed = true;
254256
},

0 commit comments

Comments
 (0)