diff --git a/apps/web/src/components/HistorySwipeNavigation.browser.test.tsx b/apps/web/src/components/HistorySwipeNavigation.browser.test.tsx index d2a15729b0df2..d36efc1d535d7 100644 --- a/apps/web/src/components/HistorySwipeNavigation.browser.test.tsx +++ b/apps/web/src/components/HistorySwipeNavigation.browser.test.tsx @@ -24,7 +24,19 @@ describe("HistorySwipeNavigation", () => { false, ], ["scrolling left inside a scroller keeps the thread", "light", 100, -130, false], - ["swiping back at a shadow-root scroller's left edge navigates", "shadow", 0, -130, true], + ["scrolling at a shadow-root scroller's left edge keeps the thread", "shadow", 0, -130, false], + [ + "scrolling at a shadow-root scroller's right edge keeps the thread", + "shadow", + 800, + 130, + false, + ], + ["scrolling at a scroller's left edge keeps the thread", "light", 0, -130, false], + ["scrolling at a scroller's right edge keeps the thread", "light", 800, 130, false], + ["scrolling over a preview panel keeps the thread", "preview-panel", 0, -130, false], + ["scrolling over a desktop preview keeps the thread", "preview-viewport", 0, 130, false], + ["scrolling over a streamed preview keeps the thread", "server-browser", 0, -130, false], ["swiping back over a panel without overflow navigates", "none", 0, -130, true], ["a swipe that started in a closed dialog keeps the thread", "closed-dialog", 0, -130, false], ] as const)("%s", async (_name, scroller, scrollLeft, deltaX, navigates) => { @@ -34,7 +46,10 @@ describe("HistorySwipeNavigation", () => { const panel = screen.container.appendChild(document.createElement("div")); const root = scroller.startsWith("shadow") ? panel.attachShadow({ mode: "open" }) : panel; const viewport = root.appendChild(document.createElement("div")); - const noOverflow = scroller === "none" || scroller === "closed-dialog"; + const noOverflow = !scroller.startsWith("shadow") && scroller !== "light"; + if (scroller === "preview-panel") panel.setAttribute("data-preview-panel-mode", "inline"); + if (scroller === "preview-viewport") panel.setAttribute("data-preview-viewport", "tab"); + if (scroller === "server-browser") panel.setAttribute("data-server-browser-surface", "tab"); viewport.style.cssText = `width: 200px; overflow-x: ${noOverflow ? "visible" : "auto"}`; const content = viewport.appendChild(document.createElement("div")); content.style.cssText = `height: 20px; width: ${noOverflow ? 100 : 1000}px`; @@ -72,6 +87,32 @@ describe("HistorySwipeNavigation", () => { expect(navigateHistory).not.toHaveBeenCalled(); }); + it.each([ + ["Shift-scroll keeps the thread", [[-130, true]]], + [ + "pressing Shift during a swipe cancels navigation", + [ + [-130, false], + [-1, true], + ], + ], + [ + "releasing Shift during a scroll keeps the thread", + [ + [-1, true], + [-130, false], + ], + ], + ] as const)("%s", async (_name, events) => { + navigateHistory.mockReset(); + using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel"); + await using screen = await render(); + for (const [deltaX, shiftKey] of [...events, [0, false] as const]) { + screen.container.dispatchEvent(new WheelEvent("wheel", { deltaX, shiftKey, bubbles: true })); + } + expect(navigateHistory).not.toHaveBeenCalled(); + }); + // A vertical scroll 15° off vertical. const drift = Array.from({ length: 30 }, () => [-4, 15] as const); it.each([ diff --git a/apps/web/src/components/HistorySwipeNavigation.tsx b/apps/web/src/components/HistorySwipeNavigation.tsx index 2924f82cd3136..09b363c2d1f7e 100644 --- a/apps/web/src/components/HistorySwipeNavigation.tsx +++ b/apps/web/src/components/HistorySwipeNavigation.tsx @@ -44,16 +44,12 @@ function readMainContentBounds(): SwipeBounds { } // `event.target` stops at a shadow host; the composed path also reaches scrollers inside it. -function canScrollHorizontally(path: readonly EventTarget[], deltaX: number): boolean { +function hasHorizontalScroller(path: readonly EventTarget[]): boolean { for (const element of path) { if (!(element instanceof Element) || element.scrollWidth <= element.clientWidth) continue; const { overflowX } = getComputedStyle(element); if (overflowX !== "auto" && overflowX !== "scroll") continue; - const canScroll = - deltaX < 0 - ? element.scrollLeft > 0 - : element.scrollLeft + element.clientWidth < element.scrollWidth - 1; - if (canScroll) return true; + return true; } return false; } @@ -71,10 +67,19 @@ function lockSwipeAxis( if (Math.abs(sumX) <= Math.abs(sumY) || isVerticalScroll(Math.abs(sumX), Math.abs(sumY))) { return { kind: "ignored" }; } - if (path.some((node) => node instanceof Element && node.matches('[role="dialog"]'))) { + // Preview contents can scroll in another document or on the server, outside this DOM. + if ( + path.some( + (node) => + node instanceof Element && + node.matches( + '[role="dialog"], [data-preview-panel-mode], [data-preview-viewport], [data-server-browser-surface]', + ), + ) + ) { return { kind: "ignored" }; } - if (canScrollHorizontally(path, sumX)) return { kind: "ignored" }; + if (hasHorizontalScroller(path)) return { kind: "ignored" }; const direction = sumX < 0 ? "back" : "forward"; if (!canNavigateHistory(direction)) return { kind: "ignored" }; return { @@ -134,6 +139,11 @@ export function HistorySwipeNavigation() { gesture = null; }); const onWheel = (event: WheelEvent) => { + if (event.shiftKey) { + if (gesture?.kind === "tracking") hideIndicator(); + gesture = { kind: "ignored" }; + return; + } if (gesture === null) { gesture = event.ctrlKey || event.deltaMode !== WheelEvent.DOM_DELTA_PIXEL diff --git a/apps/web/src/components/RightPanelTabs.browser.test.tsx b/apps/web/src/components/RightPanelTabs.browser.test.tsx index 515d5cdbfb32b..347971993c7c3 100644 --- a/apps/web/src/components/RightPanelTabs.browser.test.tsx +++ b/apps/web/src/components/RightPanelTabs.browser.test.tsx @@ -68,7 +68,7 @@ const surfaces: RightPanelSurface[] = Array.from({ length: 16 }, (_, index) => ( })); describe("RightPanelTabs", () => { - it("a flick that scrolls the tab strip to its left edge keeps the thread, a swipe from the edge goes back", async () => { + it("a flick that scrolls the tab strip to its left edge keeps the thread, and so does a swipe from the edge", async () => { navigateHistory.mockReset(); const { bridge, scroll } = trackpad(); using _bridge = stubGlobal("desktopBridge", bridge); @@ -139,7 +139,7 @@ describe("RightPanelTabs", () => { viewport.scrollLeft = 0; scroll(viewport.querySelector("[data-active-tab]")!, [-40, -40, -40, -40, -40]); - expect(navigateHistory).toHaveBeenCalledExactlyOnceWith("back"); + expect(navigateHistory).not.toHaveBeenCalled(); }); });