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 MenuGroup,
14 MenuGroupLabel,
15 MenuSeparator,
16 MenuSub,
17 MenuSubPopup,
18 MenuSubTrigger,
19 MenuTrigger,
20} from "~/components/ui/menu";
21import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
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
32/**
33 * What the menu's items do for the active tab. Desktop tabs run them through
34 * the desktop bridge; server tabs, whether streamed or rendered natively by
35 * the desktop app, run them through the environment server.
36 */
37export interface PreviewMoreMenuActions {
38 readonly hardReload: () => void;
39 readonly setColorScheme: (colorScheme: DesktopPreviewColorScheme) => void;
40 readonly zoomIn: () => void;
41 readonly zoomOut: () => void;
42 readonly resetZoom: () => void;
43 readonly clearCookies: () => void;
44 readonly clearCache: () => void;
45 /** Absent where there is no inspector to open, such as a streamed server tab. */
46 readonly openDevTools?: () => void;
47 /** Absent where the tab cannot get its own window. */
48 readonly toggleNativePictureInPicture?: () => void;
49}
50
51interface Props {
52 /** False until the tab can take these actions, such as before its page attaches. */
53 enabled: boolean;
54 actions: PreviewMoreMenuActions;
55 /** Current zoom factor as a number (1.0 = 100%). */
56 zoomFactor: number;
57 /** Emulated `prefers-color-scheme` for the page. */
58 colorScheme: DesktopPreviewColorScheme;
59 /** Fixed viewport modes expose the device toolbar and resize rails. */
60 deviceToolbarVisible: boolean;
61 /** Switches between fill-panel mode and a fixed responsive viewport. */
62 onToggleDeviceToolbar: () => void;
63 /** Whether the separate native always-on-top preview window is open. */
64 nativePictureInPicture: boolean;
65 /** Profile display name, shown so the menu says which data is being cleared. */
66 profileName: string | undefined;
67}
68
69const MenuTriggerButton = () => (
70 <Tooltip>
71 <TooltipTrigger
72 render={
73 <MenuTrigger
74 render={<Button variant="ghost" size="icon-xs" type="button" aria-label="Preview menu" />}
75 />
76 }
77 >
78 <MoreVertical />
79 </TooltipTrigger>
80 <TooltipPopup>More</TooltipPopup>
81 </Tooltip>
82);
83
84/** Three-dot menu in the chrome row; the same items for every kind of tab. */
85export function PreviewMoreMenu({
86 enabled,
87 actions,
88 zoomFactor,
89 colorScheme,
90 deviceToolbarVisible,
91 onToggleDeviceToolbar,
92 nativePictureInPicture,
93 profileName,
94}: Props) {
95 const zoomLabel = `${Math.round(zoomFactor * 100)}%`;
96 const disabled = !enabled;
97 return (
98 <Menu>
99 <MenuTriggerButton />
100 <MenuPopup align="end" sideOffset={6}>
101 <MenuItem onClick={actions.hardReload} disabled={disabled}>
102 Hard reload
103 </MenuItem>
104 {actions.openDevTools ? (
105 <MenuItem onClick={actions.openDevTools} disabled={disabled}>
106 Open DevTools
107 </MenuItem>
108 ) : null}
109 {actions.toggleNativePictureInPicture ? (
110 <MenuItem onClick={actions.toggleNativePictureInPicture} disabled={disabled}>
111 {nativePictureInPicture
112 ? "Close separate preview window"
113 : "Open separate preview window"}
114 </MenuItem>
115 ) : null}
116 <MenuItem onClick={onToggleDeviceToolbar} disabled={disabled}>
117 {deviceToolbarVisible ? "Hide device toolbar" : "Show device toolbar"}
118 </MenuItem>
119 <MenuSub>
120 <MenuSubTrigger disabled={disabled}>Appearance</MenuSubTrigger>
121 <MenuSubPopup>
122 <MenuRadioGroup
123 value={colorScheme}
124 onValueChange={(value) => actions.setColorScheme(value as DesktopPreviewColorScheme)}
125 >
126 {COLOR_SCHEME_OPTIONS.map((option) => (
127 <MenuRadioItem key={option.value} value={option.value}>
128 {option.label}
129 </MenuRadioItem>
130 ))}
131 </MenuRadioGroup>
132 </MenuSubPopup>
133 </MenuSub>
134 <MenuSeparator />
135 {/*
136 Zoom row: label + inline control cluster. `closeOnClick=false`
137 keeps the menu open while the user clicks the +/− buttons.
138 */}
139 <MenuItem
140 closeOnClick={false}
141 onClick={(event: React.MouseEvent) => event.preventDefault()}
142 className="justify-between"
143 disabled={disabled}
144 >
145 <span>Zoom</span>
146 <span className="flex items-center gap-1">
147 <Button
148 variant="outline"
149 size="icon-xs"
150 type="button"
151 onClick={actions.zoomOut}
152 aria-label="Zoom out"
153 disabled={disabled}
154 >
155 <Minus />
156 </Button>
157 <span className="min-w-12 text-center text-xs tabular-nums text-muted-foreground">
158 {zoomLabel}
159 </span>
160 <Button
161 variant="outline"
162 size="icon-xs"
163 type="button"
164 onClick={actions.zoomIn}
165 aria-label="Zoom in"
166 disabled={disabled}
167 >
168 <PlusIcon />
169 </Button>
170 <Button
171 variant="ghost"
172 size="icon-xs"
173 type="button"
174 onClick={actions.resetZoom}
175 aria-label="Reset zoom"
176 disabled={disabled}
177 >
178 <RotateCcw />
179 </Button>
180 </span>
181 </MenuItem>
182 <MenuSeparator />
183 {/*
184 Grouped so the heading has a `MenuGroup` ancestor — `MenuGroupLabel`
185 reads its context and throws without one. The heading also answers
186 which profile the tab is in, which is otherwise invisible: it is fixed
187 at open and nothing else in the chrome shows it.
188 */}
189 <MenuGroup>
190 {profileName ? (
191 // Truncation needs a block box: `text-overflow` on an inline child
192 // never applies and a long name would push the popup past its width.
193 <MenuGroupLabel className="max-w-64">
194 <span className="block truncate">Profile: {profileName}</span>
195 </MenuGroupLabel>
196 ) : null}
197 <MenuItem onClick={actions.clearCookies}>Clear cookies</MenuItem>
198 <MenuItem onClick={actions.clearCache}>Clear cache</MenuItem>
199 </MenuGroup>
200 </MenuPopup>
201 </Menu>
202 );
203}