Open History to see the last commit
1"use client";
2
3import type { DesktopPreviewColorScheme } from "@t3tools/contracts";
4import { Minus, MoreVertical, Plus as PlusIcon, RotateCcw } from "lucide-react";
5
6import { Button } from "~/components/ui/button";
7import {
8 Menu,
9 MenuItem,
10 MenuPopup,
11 MenuRadioGroup,
12 MenuRadioItem,
13 MenuSeparator,
14 MenuSub,
15 MenuSubPopup,
16 MenuSubTrigger,
17 MenuTrigger,
18} from "~/components/ui/menu";
19import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
20
21import { previewBridge } from "./previewBridge";
22
23const COLOR_SCHEME_OPTIONS: ReadonlyArray<{
24 value: DesktopPreviewColorScheme;
25 label: string;
26}> = [
27 { value: "system", label: "System" },
28 { value: "light", label: "Light" },
29 { value: "dark", label: "Dark" },
30];
31
32interface Props {
33 /** Active preview tab id. Tab-targeting actions are disabled without it. */
34 tabId: string | null;
35 /**
36 * True only after the desktop bridge has registered a `webContentsId` for
37 * the active tab. Tab-targeting actions throw on the desktop side until
38 * then; we disable those items so the menu doesn't fire silent no-ops.
39 */
40 hasWebContents: boolean;
41 /** Current zoom factor as a number (1.0 = 100%). */
42 zoomFactor: number;
43 /** Emulated `prefers-color-scheme` for the guest page. */
44 colorScheme: DesktopPreviewColorScheme;
45 /** Fixed viewport modes expose the device toolbar and resize rails. */
46 deviceToolbarVisible: boolean;
47 /** Switches between fill-panel mode and a fixed responsive viewport. */
48 onToggleDeviceToolbar: () => void;
49 /** Whether the separate native always-on-top preview window is open. */
50 nativePictureInPicture: boolean;
51 /** Toggles the optional native always-on-top preview window. */
52 onNativePictureInPicture: () => void;
53}
54
55/**
56 * Three-dot menu in the chrome row. Wires Hard reload, DevTools, zoom
57 * controls, and storage-clearing actions. Only mounted by `PreviewView`
58 * when the desktop bridge is present, so we can call it unconditionally.
59 */
60export function PreviewMoreMenu({
61 tabId,
62 hasWebContents,
63 zoomFactor,
64 colorScheme,
65 deviceToolbarVisible,
66 onToggleDeviceToolbar,
67 nativePictureInPicture,
68 onNativePictureInPicture,
69}: Props) {
70 if (!previewBridge) return null;
71 const bridge = previewBridge;
72 const tabDisabled = !tabId || !hasWebContents;
73 const callTab = (op: (tabId: string) => Promise<void>) => () => {
74 if (!tabId) return;
75 void op(tabId).catch(() => undefined);
76 };
77
78 const zoomLabel = `${Math.round(zoomFactor * 100)}%`;
79 return (
80 <Menu>
81 <Tooltip>
82 <TooltipTrigger
83 render={
84 <MenuTrigger
85 render={
86 <Button variant="ghost" size="icon-xs" type="button" aria-label="Preview menu" />
87 }
88 />
89 }
90 >
91 <MoreVertical />
92 </TooltipTrigger>
93 <TooltipPopup>More</TooltipPopup>
94 </Tooltip>
95 <MenuPopup align="end" sideOffset={6} className="min-w-56">
96 <MenuItem onClick={callTab(bridge.hardReload)} disabled={tabDisabled}>
97 Hard reload
98 </MenuItem>
99 <MenuItem onClick={callTab(bridge.openDevTools)} disabled={tabDisabled}>
100 Open DevTools
101 </MenuItem>
102 <MenuItem onClick={onNativePictureInPicture} disabled={tabDisabled}>
103 {nativePictureInPicture
104 ? "Close separate preview window"
105 : "Open separate preview window"}
106 </MenuItem>
107 <MenuItem onClick={onToggleDeviceToolbar} disabled={tabDisabled}>
108 {deviceToolbarVisible ? "Hide device toolbar" : "Show device toolbar"}
109 </MenuItem>
110 <MenuSub>
111 <MenuSubTrigger disabled={tabDisabled}>Appearance</MenuSubTrigger>
112 <MenuSubPopup className="min-w-32">
113 <MenuRadioGroup
114 value={colorScheme}
115 onValueChange={(value) => {
116 if (!tabId) return;
117 void bridge
118 .setColorScheme(tabId, value as DesktopPreviewColorScheme)
119 .catch(() => undefined);
120 }}
121 >
122 {COLOR_SCHEME_OPTIONS.map((option) => (
123 <MenuRadioItem key={option.value} value={option.value}>
124 {option.label}
125 </MenuRadioItem>
126 ))}
127 </MenuRadioGroup>
128 </MenuSubPopup>
129 </MenuSub>
130 <MenuSeparator />
131 {/*
132 Zoom row: label + inline control cluster. `closeOnClick=false`
133 keeps the menu open while the user clicks the +/− buttons.
134 */}
135 <MenuItem
136 closeOnClick={false}
137 onClick={(event: React.MouseEvent) => event.preventDefault()}
138 className="justify-between"
139 disabled={tabDisabled}
140 >
141 <span>Zoom</span>
142 <span className="flex items-center gap-1">
143 <Button
144 variant="outline"
145 size="icon-xs"
146 type="button"
147 onClick={callTab(bridge.zoomOut)}
148 aria-label="Zoom out"
149 disabled={tabDisabled}
150 >
151 <Minus />
152 </Button>
153 <span className="min-w-12 text-center text-xs tabular-nums text-muted-foreground">
154 {zoomLabel}
155 </span>
156 <Button
157 variant="outline"
158 size="icon-xs"
159 type="button"
160 onClick={callTab(bridge.zoomIn)}
161 aria-label="Zoom in"
162 disabled={tabDisabled}
163 >
164 <PlusIcon />
165 </Button>
166 <Button
167 variant="ghost"
168 size="icon-xs"
169 type="button"
170 onClick={callTab(bridge.resetZoom)}
171 aria-label="Reset zoom"
172 className="[:hover,[data-pressed]]:bg-foreground/10"
173 disabled={tabDisabled}
174 >
175 <RotateCcw />
176 </Button>
177 </span>
178 </MenuItem>
179 <MenuSeparator />
180 <MenuItem onClick={() => void bridge.clearCookies().catch(() => undefined)}>
181 Clear cookies
182 </MenuItem>
183 <MenuItem onClick={() => void bridge.clearCache().catch(() => undefined)}>
184 Clear cache
185 </MenuItem>
186 </MenuPopup>
187 </Menu>
188 );
189}