Skip to content

Commit 64dc783

Browse files
morgmartCarl
andcommitted
Let the return cue finish independently of reaction animations
Only the highlighted row's own after-pseudo animation retires the cue. Keep explicit cancel, channel change and unmount cleanup unconditional, with a real-hook regression. Co-authored-by: Carl <[email protected]> Signed-off-by: morgmart <[email protected]>
1 parent 7ae0ce0 commit 64dc783

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// @vitest-environment jsdom
2+
import "@testing-library/jest-dom/vitest";
3+
import { act, cleanup, renderHook } from "@testing-library/react";
4+
import { afterEach, expect, it, vi } from "vitest";
5+
import { useReturnHighlight } from "./use-return-highlight";
6+
import styles from "./Messages.module.css";
7+
8+
afterEach(() => {
9+
cleanup();
10+
document.body.replaceChildren();
11+
vi.restoreAllMocks();
12+
vi.unstubAllGlobals();
13+
});
14+
15+
it("ignores other animation completions while its own cue and cancellation retire it", () => {
16+
const container = document.createElement("div");
17+
container.innerHTML =
18+
'<section data-channel-timeline="alpha"><div data-message-id="root"><div data-layout="timeline"><span>Reaction</span></div></div></section>';
19+
document.body.append(container);
20+
const scroller = container.querySelector("section");
21+
const row = container.querySelector<HTMLElement>("[data-layout]");
22+
const reaction = row?.querySelector("span");
23+
const highlight = styles.returnHighlight;
24+
if (!scroller || !row || !reaction || !highlight)
25+
throw new Error("Missing fixture elements");
26+
vi.spyOn(scroller, "getBoundingClientRect").mockReturnValue(
27+
new DOMRect(0, 0, 300, 300),
28+
);
29+
vi.spyOn(row, "getBoundingClientRect").mockReturnValue(
30+
new DOMRect(0, 50, 300, 50),
31+
);
32+
let frame: FrameRequestCallback | undefined;
33+
vi.stubGlobal("requestAnimationFrame", (next: FrameRequestCallback) => {
34+
frame = next;
35+
return 1;
36+
});
37+
vi.stubGlobal("cancelAnimationFrame", () => {
38+
frame = undefined;
39+
});
40+
const ref = { current: container };
41+
const h = renderHook(({ channel }) => useReturnHighlight(ref, channel), {
42+
initialProps: { channel: "alpha" },
43+
});
44+
const paint = () =>
45+
act(() => {
46+
h.result.current("root");
47+
frame?.(0);
48+
});
49+
const finish = (target: Element, pseudoElement = "") => {
50+
const event = new Event("animationend", { bubbles: true });
51+
Object.defineProperty(event, "pseudoElement", { value: pseudoElement });
52+
act(() => target.dispatchEvent(event));
53+
};
54+
paint();
55+
expect(row).toHaveClass(highlight);
56+
finish(reaction, "::after");
57+
finish(row);
58+
finish(row, "::before");
59+
expect(row).toHaveClass(highlight);
60+
finish(row, "::after");
61+
expect(row).not.toHaveClass(highlight);
62+
paint();
63+
act(() => h.result.current(undefined));
64+
expect(row).not.toHaveClass(highlight);
65+
paint();
66+
h.rerender({ channel: "beta" });
67+
expect(row).not.toHaveClass(highlight);
68+
h.rerender({ channel: "alpha" });
69+
paint();
70+
h.unmount();
71+
expect(row).not.toHaveClass(highlight);
72+
});

‎src/features/messages/use-return-highlight.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export function useReturnHighlight(
1515
let row: HTMLElement | undefined;
1616
const clear = () => {
1717
row?.classList.remove(styles.returnHighlight ?? "");
18-
row?.removeEventListener("animationend", clear);
18+
row?.removeEventListener("animationend", finish);
19+
};
20+
const finish = (event: AnimationEvent) => {
21+
if (event.target === row && event.pseudoElement === "::after") clear();
1922
};
2023
// Let tab selection and the current close/navigation commit settle first.
2124
// Check once: hidden, unloaded and offscreen roots get no deferred cue.
@@ -49,7 +52,7 @@ export function useReturnHighlight(
4952
)
5053
return;
5154
row.classList.add(styles.returnHighlight ?? "");
52-
row.addEventListener("animationend", clear);
55+
row.addEventListener("animationend", finish);
5356
});
5457
pending.current = () => {
5558
cancelAnimationFrame(frame);

0 commit comments

Comments
 (0)