Skip to content

Commit 2c5840e

Browse files
committed
fix(web): keep mouse history navigation inside the right panel
1 parent 9e85538 commit 2c5840e

5 files changed

Lines changed: 143 additions & 3 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
9999
import { PreviewPanelShell, type PreviewPanelMode } from "./preview/PreviewPanelShell";
100100
import { FaviconImage } from "./preview/PreviewFaviconIcon";
101101
import { previewBridge } from "./preview/previewBridge";
102+
import { dispatchPreviewAction } from "./preview/previewActionBus";
102103
import { PierreEntryIcon } from "./chat/PierreEntryIcon";
103104
import { resolvePullRequestState } from "./pullRequest/pullRequestPresentation";
104105
import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
@@ -1024,6 +1025,25 @@ function RightPanelTab(
10241025

10251026
export function RightPanelTabs(props: RightPanelTabsProps) {
10261027
const focusedSurface = props.surfaces.find((surface) => surface.id === props.activeSurfaceId);
1028+
const handleHistoryMouseButton = (event: ReactMouseEvent) => {
1029+
if (event.button !== 3 && event.button !== 4) return;
1030+
// Chromium navigates the host document on mouseup unless the panel cancels it.
1031+
event.preventDefault();
1032+
event.stopPropagation();
1033+
if (
1034+
event.type === "mouseup" &&
1035+
props.environmentId &&
1036+
focusedSurface?.kind === "preview" &&
1037+
focusedSurface.resourceId
1038+
) {
1039+
dispatchPreviewAction({
1040+
type: "history",
1041+
environmentId: props.environmentId,
1042+
tabId: focusedSurface.resourceId,
1043+
delta: event.button === 3 ? -1 : 1,
1044+
});
1045+
}
1046+
};
10271047
const ownsDesktopTitleBar = isElectron && props.mode === "inline";
10281048
const browserProfiles = useBrowserDefaults().profiles;
10291049
const { resolvedTheme } = useTheme();
@@ -1317,6 +1337,8 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
13171337

13181338
return (
13191339
<PreviewPanelShell
1340+
onMouseUpCapture={handleHistoryMouseButton}
1341+
onAuxClickCapture={handleHistoryMouseButton}
13201342
workspaceArea
13211343
workspaceFocusKey={`panel:${props.environmentId}:${props.activeSurfaceId ?? "empty"}`}
13221344
workspaceLabel={

‎apps/web/src/components/preview/PreviewPanelShell.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { type CSSProperties, type ReactNode, useLayoutEffect, useRef, useState } from "react";
1+
import {
2+
type CSSProperties,
3+
type MouseEventHandler,
4+
type ReactNode,
5+
useLayoutEffect,
6+
useRef,
7+
useState,
8+
} from "react";
29
import {
310
type PreviewPanelInlineSize,
411
usePreviewPanelInlineSize,
@@ -23,6 +30,8 @@ interface PreviewPanelShellProps {
2330
widthStorageKey?: string;
2431
defaultWidth?: number;
2532
children: ReactNode;
33+
onMouseUpCapture?: MouseEventHandler<HTMLDivElement>;
34+
onAuxClickCapture?: MouseEventHandler<HTMLDivElement>;
2635
}
2736

2837
export function PreviewPanelShell(props: PreviewPanelShellProps) {
@@ -117,6 +126,8 @@ function PreviewPanelShellFrame(
117126
return (
118127
<div
119128
ref={attachHost}
129+
onMouseUpCapture={props.onMouseUpCapture}
130+
onAuxClickCapture={props.onAuxClickCapture}
120131
{...(props.workspaceArea ? panelArea : {})}
121132
data-workspace-focus-key={props.workspaceFocusKey}
122133
data-workspace-unavailable={collapsible && !open ? "true" : undefined}

‎apps/web/src/components/preview/PreviewView.browser.test.tsx‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";
157157
import { usePreviewMiniPlayerStore } from "~/previewMiniPlayerStore";
158158
import { dispatchPreviewAction } from "./previewActionBus";
159159
import { PreviewView } from "./PreviewView";
160+
import { previewBridge } from "./previewBridge";
161+
import { RightPanelTabs } from "../RightPanelTabs";
162+
import type { RightPanelSurface } from "~/rightPanelStore";
160163
import { ToastProvider } from "~/components/ui/toast";
161164

162165
const TEST_THREAD_REF = {
@@ -215,6 +218,100 @@ function setup() {
215218
}
216219

217220
describe("PreviewView", () => {
221+
it("keeps thumb-button navigation inside the active right panel", async () => {
222+
setup();
223+
const browser: RightPanelSurface = {
224+
id: "browser:tab-1",
225+
kind: "preview",
226+
resourceId: "tab-1",
227+
};
228+
const diff: RightPanelSurface = { id: "diff", kind: "diff" };
229+
const noop = () => {};
230+
const panel = (activeSurfaceId: string) => (
231+
<>
232+
<button>Main chat</button>
233+
<RightPanelTabs
234+
mode="embedded"
235+
keybindings={[]}
236+
getShortcutContext={() => ({
237+
terminalFocus: false,
238+
terminalOpen: false,
239+
previewFocus: false,
240+
previewOpen: true,
241+
isWeb: true,
242+
isDesktop: false,
243+
})}
244+
surfaces={[browser, diff]}
245+
environmentId={TEST_THREAD_REF.environmentId}
246+
activeSurfaceId={activeSurfaceId}
247+
pendingSurfaceIds={new Set()}
248+
previewSessions={{}}
249+
desktopByTabId={{}}
250+
terminalLabelsById={new Map()}
251+
onActivate={noop}
252+
onCloseSurface={noop}
253+
onCloseOtherSurfaces={noop}
254+
onCloseSurfacesToRight={noop}
255+
onCloseAllSurfaces={noop}
256+
onCopyFilePath={noop}
257+
onAddBrowser={noop}
258+
onAddBrowserInProfile={noop}
259+
onAddTerminal={noop}
260+
onAddDiff={noop}
261+
onAddFiles={noop}
262+
onAddPullRequest={noop}
263+
onAddPullRequests={noop}
264+
onAddDevice={noop}
265+
browserAvailable
266+
terminalAvailable={false}
267+
diffAvailable
268+
filesAvailable={false}
269+
pullRequestAvailable={false}
270+
pullRequestsAvailable={false}
271+
deviceAvailable={false}
272+
>
273+
<PreviewView
274+
threadRef={TEST_THREAD_REF}
275+
tabId="tab-1"
276+
visible={activeSurfaceId === browser.id}
277+
/>
278+
<PreviewView
279+
threadRef={{ ...TEST_THREAD_REF, environmentId: EnvironmentId.make("other") }}
280+
tabId="tab-1"
281+
visible
282+
/>
283+
</RightPanelTabs>
284+
</>
285+
);
286+
await using screen = await render(panel(browser.id));
287+
const press = (target: Element, type: string, button: number) => {
288+
const event = new MouseEvent(type, { button, bubbles: true, cancelable: true });
289+
target.dispatchEvent(event);
290+
return event.defaultPrevented;
291+
};
292+
const tabbar = screen.container.querySelector("[data-right-panel-tabbar]")!;
293+
const address = screen.getByRole("textbox").elements()[0]!;
294+
for (const target of [tabbar, address]) {
295+
for (const button of [3, 4]) {
296+
expect(press(target, "mouseup", button)).toBe(true);
297+
expect(press(target, "auxclick", button)).toBe(true);
298+
}
299+
}
300+
expect(previewBridge!.goBack).toHaveBeenCalledTimes(2);
301+
expect(previewBridge!.goForward).toHaveBeenCalledTimes(2);
302+
expect(previewBridge!.goBack).toHaveBeenCalledWith(TEST_RUNTIME_TAB_ID);
303+
expect(previewBridge!.goForward).toHaveBeenCalledWith(TEST_RUNTIME_TAB_ID);
304+
for (const button of [0, 1, 2]) expect(press(tabbar, "mouseup", button)).toBe(false);
305+
expect(press(screen.getByRole("button", { name: "Main chat" }).element(), "mouseup", 3)).toBe(
306+
false,
307+
);
308+
309+
await screen.rerender(panel(diff.id));
310+
for (const button of [3, 4]) expect(press(tabbar, "mouseup", button)).toBe(true);
311+
expect(previewBridge!.goBack).toHaveBeenCalledTimes(2);
312+
expect(previewBridge!.goForward).toHaveBeenCalledTimes(2);
313+
});
314+
218315
it.each(["mailto:[email protected]", "http://"])(
219316
"reports rejected address %s without navigating or recording it",
220317
async (address) => {

‎apps/web/src/components/preview/PreviewView.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,13 @@ export function PreviewView({
836836
// Subscribe only while visible; ChatView always owns URL-aware toggle-panel handling.
837837
const onPreviewAction = useEffectEvent(
838838
(action: Parameters<Parameters<typeof subscribePreviewAction>[0]>[0]) => {
839+
if (typeof action !== "string") {
840+
if (action.environmentId !== threadRef.environmentId || action.tabId !== tabId) return;
841+
if (serverInputDisabled) return;
842+
if (action.delta === -1) handleBack();
843+
else handleForward();
844+
return;
845+
}
839846
switch (action) {
840847
case "refresh":
841848
handleRefresh();

‎apps/web/src/components/preview/previewActionBus.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import type { EnvironmentId } from "@t3tools/contracts";
2+
13
export type PreviewAction =
24
| "toggle-panel"
35
| "refresh"
46
| "focus-url"
57
| "zoom-in"
68
| "zoom-out"
7-
| "reset-zoom";
9+
| "reset-zoom"
10+
| { type: "history"; environmentId: EnvironmentId; tabId: string; delta: -1 | 1 };
811

912
const EVENT_NAME = "t3code:preview-action";
1013

@@ -17,7 +20,7 @@ export function subscribePreviewAction(listener: (action: PreviewAction) => void
1720
if (typeof window === "undefined") return () => {};
1821
const handler = (event: Event) => {
1922
const detail = (event as CustomEvent<PreviewAction>).detail;
20-
if (typeof detail === "string") listener(detail);
23+
if (typeof detail === "string" || detail?.type === "history") listener(detail);
2124
};
2225
window.addEventListener(EVENT_NAME, handler);
2326
return () => window.removeEventListener(EVENT_NAME, handler);

0 commit comments

Comments
 (0)