Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/web/src/components/HistorySwipeNavigation.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,16 @@ describe("HistorySwipeNavigation", () => {

expect(navigateHistory).toHaveBeenCalledTimes(navigates ? 1 : 0);
});

it("navigates once the swipe passes the trigger distance, without waiting for the fingers to lift", async () => {
navigateHistory.mockReset();
await using screen = await render(<HistorySwipeNavigation />);
const panel = screen.container.appendChild(document.createElement("div"));
for (let step = 0; step < 20; step++) {
panel.dispatchEvent(new WheelEvent("wheel", { deltaX: -10, bubbles: true, composed: true }));
}

expect(navigateHistory).toHaveBeenCalledTimes(1);
expect(navigateHistory).toHaveBeenCalledWith("back");
});
});
11 changes: 7 additions & 4 deletions apps/web/src/components/HistorySwipeNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,8 @@ export function HistorySwipeNavigation() {
let gesture: SwipeGesture | null = null;
const hideIndicator = () =>
setSwipe((current) => (current === null ? null : { ...current, tracking: false }));
// The fingers lifted: navigate if the swipe went far enough, then ignore its momentum.
// The swipe ended or was decided: ignore the rest of it, momentum included.
const release = () => {
if (gesture?.kind === "tracking" && gesture.distance >= SWIPE_TRIGGER_DISTANCE) {
navigateHistory(gesture.direction);
}
gesture = { kind: "ignored" };
hideIndicator();
};
Expand Down Expand Up @@ -134,6 +131,12 @@ export function HistorySwipeNavigation() {
if (isMomentumTail(gesture.deltas)) return release();
gesture.distance = Math.max(0, gesture.distance + delta);
}
// Chromium only ends a trackpad scroll after its momentum stops, so waiting for the
// fingers to lift would delay navigation by the whole momentum tail.
if (gesture.distance >= SWIPE_TRIGGER_DISTANCE) {
navigateHistory(gesture.direction);
return release();
}
setSwipe({
direction: gesture.direction,
progress: Math.min(1, gesture.distance / SWIPE_TRIGGER_DISTANCE),
Expand Down
Loading