Skip to content

Commit 0038a61

Browse files
committed
fix(web): sidebar threads no longer replay motion when the window regains focus
1 parent 4013183 commit 0038a61

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { expect, it, vi } from "vite-plus/test";
2+
import { createSidebarListMotion } from "./Sidebar.motion";
3+
4+
function mountList(names: readonly string[]) {
5+
const list = document.createElement("ul");
6+
list.style.position = "relative";
7+
const rows = new Map<string, HTMLLIElement>();
8+
for (const name of names) {
9+
const row = document.createElement("li");
10+
row.style.height = "20px";
11+
row.textContent = name;
12+
rows.set(name, row);
13+
list.append(row);
14+
}
15+
document.body.append(list);
16+
const motion = createSidebarListMotion(list);
17+
motion.update(false);
18+
return {
19+
list,
20+
rows,
21+
motion,
22+
[Symbol.dispose]: () => {
23+
motion.dispose();
24+
list.remove();
25+
},
26+
};
27+
}
28+
29+
it("does not queue row motion while the document is hidden", () => {
30+
using fixture = mountList(["a", "b"]);
31+
using visibility = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");
32+
fixture.list.prepend(fixture.rows.get("b")!);
33+
fixture.motion.update(true);
34+
expect(fixture.list.getAnimations({ subtree: true })).toHaveLength(0);
35+
36+
visibility.mockReturnValue("visible");
37+
fixture.list.prepend(fixture.rows.get("a")!);
38+
fixture.motion.update(true);
39+
expect(fixture.list.getAnimations({ subtree: true }).length).toBeGreaterThan(0);
40+
});
41+
42+
it("drops in-flight row motion when the document becomes hidden", () => {
43+
using fixture = mountList(["a", "b"]);
44+
using visibility = vi.spyOn(document, "visibilityState", "get").mockReturnValue("visible");
45+
fixture.list.prepend(fixture.rows.get("b")!);
46+
fixture.motion.update(true);
47+
expect(fixture.list.getAnimations({ subtree: true }).length).toBeGreaterThan(0);
48+
49+
visibility.mockReturnValue("hidden");
50+
document.dispatchEvent(new Event("visibilitychange"));
51+
expect(fixture.list.getAnimations({ subtree: true })).toHaveLength(0);
52+
});

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ function progress(animation: Animation) {
1515
export function createSidebarListMotion(parent: HTMLUListElement) {
1616
let positions: Map<HTMLElement, RowPosition> | null = null;
1717
let disposed = false;
18-
const reducedMotion = parent.ownerDocument.defaultView?.matchMedia(
19-
"(prefers-reduced-motion: reduce)",
20-
);
18+
const document = parent.ownerDocument;
19+
const reducedMotion = document.defaultView?.matchMedia("(prefers-reduced-motion: reduce)");
2120
const running = new Map<HTMLElement, { animation: Animation; offset: number }>();
2221
const entering = new Map<HTMLElement, { animation: Animation; travel: number }>();
2322
const exiting = new Map<HTMLElement, Animation>();
@@ -117,6 +116,15 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
117116
);
118117
};
119118

119+
// Hidden documents freeze the animation timeline, so motion queued while minimized or
120+
// occluded would replay on return instead of having already played.
121+
const onVisibilityChange = () => {
122+
if (document.visibilityState !== "hidden") return;
123+
for (const node of running.keys()) cancel(node);
124+
clearFades();
125+
};
126+
document.addEventListener("visibilitychange", onVisibilityChange);
127+
120128
return {
121129
update(animate: boolean) {
122130
if (disposed) return;
@@ -145,6 +153,7 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
145153
const shouldAnimate =
146154
animate &&
147155
positions !== null &&
156+
document.visibilityState === "visible" &&
148157
!reducedMotion?.matches &&
149158
fadeCount <= MAX_FADED_ROWS_PER_UPDATE;
150159
const movedDelta = new Map<HTMLElement, number>();
@@ -239,6 +248,7 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
239248
},
240249
suspend,
241250
dispose() {
251+
document.removeEventListener("visibilitychange", onVisibilityChange);
242252
suspend();
243253
disposed = true;
244254
},

0 commit comments

Comments
 (0)