Skip to content

Commit 0e48fd7

Browse files
author
PR Batch Tester
committed
fix(web): retain scroll ownership when its viewport disappears
1 parent a197931 commit 0e48fd7

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { HistorySwipeNavigation } from "./HistorySwipeNavigation";
55

66
type Scroller = "shadow" | "light" | "none" | "shadow-dialog";
77
// 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"];
8+
type Step = readonly [deltaX: number, after?: number | "remount" | "remove"];
99

1010
function mountPanel(container: HTMLElement, scroller: Scroller, scrollLeft: number) {
1111
const panel = container.appendChild(document.createElement("div"));
@@ -42,6 +42,7 @@ describe("HistorySwipeNavigation", () => {
4242
[[1, "remount"], [130]],
4343
null,
4444
],
45+
["a scroller removed after a small first event", "shadow", 400, [[1, "remove"], [130]], null],
4546
["swiping inside a dialog in a shadow root", "shadow-dialog", 0, [[-130]], null],
4647
["swiping left over content without overflow", "none", 0, [[-130]], "back"],
4748
["swiping right over content without overflow", "none", 0, [[130]], "forward"],
@@ -62,6 +63,9 @@ describe("HistorySwipeNavigation", () => {
6263
replacement.scrollLeft = previous;
6364
viewport = replacement;
6465
content = replacement.firstElementChild as HTMLDivElement;
66+
} else if (after === "remove") {
67+
viewport.remove();
68+
({ viewport, content } = mountPanel(screen.container, "none", 0));
6569
} else if (after !== undefined) {
6670
viewport.scrollLeft = after;
6771
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ function lockSwipeAxis(
5757
if (path.some((node) => node instanceof Element && node.matches('[role="dialog"]'))) {
5858
return { kind: "ignored" };
5959
}
60-
if (isInHorizontalScroller(path)) return { kind: "ignored" };
6160
const direction = sumX < 0 ? "back" : "forward";
6261
if (!canNavigateHistory(direction)) return { kind: "ignored" };
6362
return {
@@ -119,11 +118,16 @@ export function HistorySwipeNavigation() {
119118
}
120119
if (gesture.kind === "ignored") return;
121120
if (gesture.kind === "pending") {
121+
const path = event.composedPath();
122+
// Claim content scrolling while its layout still exists, before a possible remount.
123+
if (isInHorizontalScroller(path)) {
124+
gesture = { kind: "ignored" };
125+
return;
126+
}
122127
gesture.sumX += event.deltaX;
123128
gesture.sumY += event.deltaY;
124129
if (Math.abs(gesture.sumX) + Math.abs(gesture.sumY) < SWIPE_AXIS_LOCK_DISTANCE) return;
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()] });
130+
gesture = lockSwipeAxis({ ...gesture, path: [...gesture.path, ...path] });
127131
if (gesture.kind === "ignored") return;
128132
} else {
129133
const delta = gesture.direction === "back" ? -event.deltaX : event.deltaX;

0 commit comments

Comments
 (0)