Skip to content

Commit a197931

Browse files
PR Batch Testermaria-rcksclaude
committed
fix(web): recognize a diff scroller remounted mid-swipe
The gesture's ownership check read only the first wheel event's composed path. If the scroller was replaced before the axis locked, its detached element had no overflow, so a horizontal scroll in the new scroller could navigate history. Check the locking event's path too, keeping the first path so a dialog the gesture started in still blocks it. Co-authored-by: maria <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent eb7d688 commit a197931

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

‎apps/web/src/components/HistorySwipeNavigation.browser.test.tsx‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { render } from "../../test/render";
44
import { HistorySwipeNavigation } from "./HistorySwipeNavigation";
55

66
type Scroller = "shadow" | "light" | "none" | "shadow-dialog";
7-
// Each step is one wheel event, optionally followed by the scroll it caused.
8-
type Step = readonly [deltaX: number, scrollLeftAfter?: number];
7+
// Each step is one wheel event, optionally followed by the scroll it caused or a remount.
8+
type Step = readonly [deltaX: number, after?: number | "remount"];
99

1010
function mountPanel(container: HTMLElement, scroller: Scroller, scrollLeft: number) {
1111
const panel = container.appendChild(document.createElement("div"));
@@ -35,6 +35,13 @@ describe("HistorySwipeNavigation", () => {
3535
["a small first event that reaches the left edge", "shadow", 3, [[-3, 0], [-130]], null],
3636
["a small first event that reaches the right edge", "light", 797, [[3, 800], [130]], null],
3737
["a scroll that reaches the edge mid-gesture", "light", 100, [[-100, 0], [-130]], null],
38+
[
39+
"a scroller remounted after a small first event",
40+
"shadow",
41+
400,
42+
[[1, "remount"], [130]],
43+
null,
44+
],
3845
["swiping inside a dialog in a shadow root", "shadow-dialog", 0, [[-130]], null],
3946
["swiping left over content without overflow", "none", 0, [[-130]], "back"],
4047
["swiping right over content without overflow", "none", 0, [[130]], "forward"],
@@ -44,11 +51,20 @@ describe("HistorySwipeNavigation", () => {
4451
using forward = vi.spyOn(window.history, "forward").mockImplementation(() => undefined);
4552
using _canGoBack = vi.spyOn(window.navigation, "canGoBack", "get").mockReturnValue(true);
4653
using _canGoForward = vi.spyOn(window.navigation, "canGoForward", "get").mockReturnValue(true);
47-
const { viewport, content } = mountPanel(screen.container, scroller, scrollLeft);
54+
let { viewport, content } = mountPanel(screen.container, scroller, scrollLeft);
4855

49-
for (const [deltaX, scrollLeftAfter] of [...steps, [0] as const]) {
56+
for (const [deltaX, after] of [...steps, [0] as const]) {
5057
content.dispatchEvent(new WheelEvent("wheel", { deltaX, bubbles: true, composed: true }));
51-
if (scrollLeftAfter !== undefined) viewport.scrollLeft = scrollLeftAfter;
58+
if (after === "remount") {
59+
const replacement = viewport.cloneNode(true) as HTMLDivElement;
60+
const { scrollLeft: previous } = viewport;
61+
viewport.replaceWith(replacement);
62+
replacement.scrollLeft = previous;
63+
viewport = replacement;
64+
content = replacement.firstElementChild as HTMLDivElement;
65+
} else if (after !== undefined) {
66+
viewport.scrollLeft = after;
67+
}
5268
}
5369

5470
expect(back).toHaveBeenCalledTimes(navigates === "back" ? 1 : 0);

‎apps/web/src/components/HistorySwipeNavigation.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export function HistorySwipeNavigation() {
122122
gesture.sumX += event.deltaX;
123123
gesture.sumY += event.deltaY;
124124
if (Math.abs(gesture.sumX) + Math.abs(gesture.sumY) < SWIPE_AXIS_LOCK_DISTANCE) return;
125-
gesture = lockSwipeAxis(gesture);
125+
// The first event's scroller may have been replaced; keep its path for dialog checks.
126+
gesture = lockSwipeAxis({ ...gesture, path: [...gesture.path, ...event.composedPath()] });
126127
if (gesture.kind === "ignored") return;
127128
} else {
128129
const delta = gesture.direction === "back" ? -event.deltaX : event.deltaX;

0 commit comments

Comments
 (0)