Skip to content
Merged
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
45 changes: 43 additions & 2 deletions apps/web/src/components/HistorySwipeNavigation.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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`;
Expand Down Expand Up @@ -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(<HistorySwipeNavigation />);
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([
Expand Down
26 changes: 18 additions & 8 deletions apps/web/src/components/HistorySwipeNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Expand Down Expand Up @@ -134,6 +139,11 @@ export function HistorySwipeNavigation() {
gesture = null;
});
const onWheel = (event: WheelEvent) => {
if (event.shiftKey) {
if (gesture?.kind === "tracking") hideIndicator();
Comment thread
Yash-Singh1 marked this conversation as resolved.
Resolved
gesture = { kind: "ignored" };
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return;
}
if (gesture === null) {
gesture =
event.ctrlKey || event.deltaMode !== WheelEvent.DOM_DELTA_PIXEL
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/RightPanelTabs.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
});
});

Expand Down
Loading