@devries-cameron20 / t3code-1 / commits / a8d6b0e66b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): mouse Back/Forward over a streamed preview navigate the preview (#22522)

Nikita Maximilian Grimm committed Oct 30, 2026 at 21:41 UTC a8d6b0e66bba41ae447ea9a1f06079e6a823f2a6
2 files changed +141
apps/web/src/browser/ServerBrowserSurface.browser.test.tsx new
+123
@@ -0,0 +1,123 @@
1 +import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2 +import type {
3 + PreviewStreamEvents,
4 + PreviewStreamInput,
5 +} from "@t3tools/client-runtime/preview/server-browser-stream";
6 +
7 +import "~/index.css";
8 +import { describe, expect, it, vi } from "vite-plus/test";
9 +
10 +import { render } from "../../test/render";
11 +
12 +const mocks = vi.hoisted(() => ({
13 + send: vi.fn<(input: PreviewStreamInput) => void>(),
14 + connect: vi.fn(),
15 + events: null as PreviewStreamEvents | null,
16 + painted: null as (() => void) | null,
17 +}));
18 +
19 +vi.mock(import("@t3tools/client-runtime/preview/server-browser-stream"), async (original) => ({
20 + ...(await original()),
21 + createPreviewFramePainter: (_canvas, onPainted) => {
22 + mocks.painted = onPainted ?? null;
23 + return { paint: () => undefined, stop: () => undefined };
24 + },
25 + createPreviewStreamClient: (_options, events) => {
26 + mocks.events = events;
27 + mocks.connect();
28 + return { send: mocks.send, stop: () => undefined } as never;
29 + },
30 +}));
31 +
32 +vi.mock(import("~/state/previewStream"), async (original) => ({
33 + ...(await original()),
34 + usePreviewStreamAccess: () => ({}) as never,
35 +}));
36 +
37 +import { ServerBrowserSurface } from "./ServerBrowserSurface";
38 +
39 +async function renderSurface() {
40 + mocks.send.mockReset();
41 + mocks.connect.mockReset();
42 + const container = document.createElement("div");
43 + container.style.cssText = "width: 400px; height: 300px;";
44 + await using stack = new AsyncDisposableStack();
45 + stack.use(
46 + await render(
47 + <ServerBrowserSurface
48 + environmentId={EnvironmentId.make("environment-1")}
49 + threadId={ThreadId.make("thread-1")}
50 + tabId="tab-1"
51 + visible
52 + />,
53 + { container },
54 + ),
55 + );
56 + const canvas = container.querySelector("canvas");
57 + if (!canvas) throw new Error("The surface did not render its canvas.");
58 + // The stream connects once the surface has measured its size.
59 + await vi.waitFor(() => expect(mocks.connect).toHaveBeenCalled());
60 + return Object.assign(stack.move(), { canvas });
61 +}
62 +
63 +const mouseUp = (canvas: HTMLCanvasElement, button: number) => {
64 + const event = new MouseEvent("mouseup", { button, bubbles: true, cancelable: true });
65 + canvas.dispatchEvent(event);
66 + return event;
67 +};
68 +
69 +describe("ServerBrowserSurface", () => {
70 + it("routes mouse thumb buttons to the preview's history instead of the app", async () => {
71 + await using surface = await renderSurface();
72 + const { canvas } = surface;
73 +
74 + expect(mouseUp(canvas, 3).defaultPrevented).toBe(true);
75 + expect(mouseUp(canvas, 4).defaultPrevented).toBe(true);
76 + expect(mocks.send.mock.calls).toEqual([
77 + [{ type: "history", delta: -1 }],
78 + [{ type: "history", delta: 1 }],
79 + ]);
80 + });
81 +
82 + it("leaves other mouse buttons alone", async () => {
83 + await using surface = await renderSurface();
84 + const { canvas } = surface;
85 +
86 + expect(mouseUp(canvas, 0).defaultPrevented).toBe(false);
87 + expect(mocks.send).not.toHaveBeenCalled();
88 + });
89 +
90 + it("does not forward thumb-button presses to the page", async () => {
91 + await using surface = await renderSurface();
92 + const { canvas } = surface;
93 + canvas.width = 400;
94 + canvas.height = 300;
95 + // Synthetic pointers have no active pointer id to capture.
96 + canvas.setPointerCapture = () => undefined;
97 + mocks.painted?.();
98 + mocks.events?.onViewport({ width: 400, height: 300 });
99 + mocks.events?.onControl?.({ canOperate: true, controller: "you", generation: 1, dialog: null });
100 + const { left, top } = canvas.getBoundingClientRect();
101 + const press = (type: "pointerdown" | "pointerup", button: number) =>
102 + canvas.dispatchEvent(
103 + new PointerEvent(type, {
104 + button,
105 + buttons: type === "pointerdown" ? 1 << button : 0,
106 + clientX: left + 10,
107 + clientY: top + 10,
108 + pointerType: "mouse",
109 + bubbles: true,
110 + }),
111 + );
112 + const mouseInputs = () => mocks.send.mock.calls.filter(([input]) => input.type === "mouse");
113 +
114 + for (const button of [3, 4]) {
115 + press("pointerdown", button);
116 + press("pointerup", button);
117 + }
118 + expect(mouseInputs()).toEqual([]);
119 +
120 + press("pointerdown", 0);
121 + expect(mouseInputs()).not.toEqual([]);
122 + });
123 +});
apps/web/src/browser/ServerBrowserSurface.tsx
+18
@@ -22,6 +22,7 @@ import type {
22 import {
23 type FormEvent,
24 type KeyboardEvent,
25 + type MouseEvent,
26 type PointerEvent,
27 type Ref,
28 useEffect,
@@ -94,6 +95,10 @@ const buttonOf = (button: number): PreviewStreamMouseButton =>
95 const pressedButtonOf = (buttons: number): PreviewStreamMouseButton =>
96 buttons & 1 ? "left" : buttons & 2 ? "right" : buttons & 4 ? "middle" : "none";
97
98 +// Mouse thumb buttons: `button === 3` is Back, `button === 4` is Forward.
99 +const historyDeltaOf = (button: number): -1 | 1 | null =>
100 + button === 3 ? -1 : button === 4 ? 1 : null;
101 +
102 /** Browsers may refuse a write that arrives after the key press; a toast button is a fresh gesture. */
103 async function copyPageText(text: string) {
104 try {
@@ -490,6 +495,8 @@ export function ServerBrowserSurface(props: {
495
496 const handlePointer = (event: PointerEvent<HTMLCanvasElement>) => {
497 if (controlRef.current?.controller !== "you") return;
498 + // Thumb buttons drive this tab's history in handleMouseUp, never the page.
499 + if (historyDeltaOf(event.button) !== null) return;
500 const { type, pointerId, clientX, clientY } = event;
501 const cancelled = type === "pointercancel";
502 const touch = touchRef.current;
@@ -626,6 +633,16 @@ export function ServerBrowserSurface(props: {
633 }
634 };
635
636 + // Chromium runs a thumb button's back/forward on mouseup in this document, which
637 + // would move the app's router. Cancel it and send the press to the preview, as
638 + // the toolbar does.
639 + const handleMouseUp = (event: MouseEvent<HTMLDivElement>) => {
640 + const delta = historyDeltaOf(event.button);
641 + if (delta === null) return;
642 + event.preventDefault();
643 + send({ type: "history", delta });
644 + };
645 +
646 const resetInput = (textarea: HTMLTextAreaElement) => {
647 keySentRef.current = false;
648 textarea.value = INPUT_SENTINEL;
@@ -703,6 +720,7 @@ export function ServerBrowserSurface(props: {
720 <div
721 className={cn("relative flex flex-col overflow-hidden", className)}
722 data-server-browser-surface={tabId}
723 + onMouseUp={handleMouseUp}
724 >
725 <div
726 className={cn(