Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PreviewMoreMenu.tsx

209 lines6.2 KB

1import type { DesktopPreviewColorScheme } from "@t3tools/contracts";
2import { msg, T, useGT, useMessages, Var } from "gt-react";
3import { Minus, MoreVertical, Plus as PlusIcon, RotateCcw } from "lucide-react";
4​
5import { Button } from "~/components/ui/button";
6import {
7 Menu,
8 MenuItem,
9 MenuPopup,
10 MenuShortcut,
11 MenuRadioGroup,
12 MenuRadioItem,
13 MenuGroup,
14 MenuGroupLabel,
15 MenuSeparator,
16 MenuSub,
17 MenuSubPopup,
18 MenuSubTrigger,
19 MenuTrigger,
20} from "~/components/ui/menu";
21import { SharedTooltipTrigger } from "~/components/ui/shared-tooltip";
22​
23const COLOR_SCHEME_OPTIONS: ReadonlyArray<{
24 value: DesktopPreviewColorScheme;
25 label: string;
26}> = [
27 { value: "system", label: msg("System", { $context: "color scheme" }) },
28 { value: "light", label: msg("Light", { $context: "color scheme" }) },
29 { value: "dark", label: msg("Dark", { $context: "color scheme" }) },
30];
31​
32export interface PreviewMoreMenuActions {
33 readonly hardReload: () => void;
34 readonly setColorScheme: (colorScheme: DesktopPreviewColorScheme) => void;
35 readonly zoomIn: () => void;
36 readonly zoomOut: () => void;
37 readonly resetZoom: () => void;
38 readonly clearCookies: () => void;
39 readonly clearCache: () => void;
40 readonly openDevTools?: () => void;
41 readonly toggleNativePictureInPicture?: () => void;
42 readonly find?: () => void;
44​
45interface Props {
46 enabled: boolean;
47 actions: PreviewMoreMenuActions;
48 zoomFactor: number;
49 colorScheme: DesktopPreviewColorScheme;
50 deviceToolbarVisible: boolean;
51 onToggleDeviceToolbar: () => void;
52 nativePictureInPicture: boolean;
53 profileName: string | undefined;
54 findShortcutLabel?: string | null;
55 move?: { readonly label: string; readonly onMove: () => void };
57​
58const MenuTriggerButton = () => {
59 const gt = useGT();
60 return (
61 <SharedTooltipTrigger
62 render={
63 <MenuTrigger
64 render={
65 <Button variant="ghost" size="icon-xs" type="button" aria-label={gt("Preview menu")} />
66 }
67 />
68 }
69 label={
70 <>
71 <T context="more actions menu">More</T>
72 </>
73 }
74 >
75 <MoreVertical />
76 </SharedTooltipTrigger>
77 );
78};
79​
80export function PreviewMoreMenu({
81 enabled,
82 actions,
83 zoomFactor,
84 colorScheme,
85 deviceToolbarVisible,
86 onToggleDeviceToolbar,
87 nativePictureInPicture,
88 profileName,
89 findShortcutLabel,
90 move,
91}: Props) {
92 const gt = useGT();
93 const m = useMessages();
94 const zoomLabel = `${Math.round(zoomFactor * 100)}%`;
95 const disabled = !enabled;
96 return (
97 <Menu>
98 <MenuTriggerButton />
99 <MenuPopup align="end" sideOffset={6}>
100 {actions.find ? (
101 <MenuItem onClick={actions.find} disabled={disabled}>
102 <T>Find on page</T>
103 {findShortcutLabel ? <MenuShortcut>{findShortcutLabel}</MenuShortcut> : null}
104 </MenuItem>
105 ) : null}
106 <MenuItem onClick={actions.hardReload} disabled={disabled}>
107 <T>Hard reload</T>
108 </MenuItem>
109 {actions.openDevTools ? (
110 <MenuItem onClick={actions.openDevTools} disabled={disabled}>
111 <T>Open DevTools</T>
112 </MenuItem>
113 ) : null}
114 {actions.toggleNativePictureInPicture ? (
115 <MenuItem onClick={actions.toggleNativePictureInPicture} disabled={disabled}>
116 {nativePictureInPicture
117 ? gt("Close separate preview window")
118 : gt("Open separate preview window")}
119 </MenuItem>
120 ) : null}
121 {move ? <MenuItem onClick={move.onMove}>{move.label}</MenuItem> : null}
122 <MenuItem onClick={onToggleDeviceToolbar} disabled={disabled}>
123 {deviceToolbarVisible ? gt("Hide device toolbar") : gt("Show device toolbar")}
124 </MenuItem>
125 <MenuSub>
126 <MenuSubTrigger disabled={disabled}>
127 <T>Appearance</T>
128 </MenuSubTrigger>
129 <MenuSubPopup>
130 <MenuRadioGroup
131 value={colorScheme}
132 onValueChange={(value) => actions.setColorScheme(value as DesktopPreviewColorScheme)}
133 >
134 {COLOR_SCHEME_OPTIONS.map((option) => (
135 <MenuRadioItem key={option.value} value={option.value}>
136 {m(option.label)}
137 </MenuRadioItem>
138 ))}
139 </MenuRadioGroup>
140 </MenuSubPopup>
141 </MenuSub>
142 <MenuSeparator />
143 <MenuItem
144 closeOnClick={false}
145 onClick={(event: React.MouseEvent) => event.preventDefault()}
146 className="justify-between"
147 disabled={disabled}
148 >
149 <span>
150 <T>Zoom</T>
151 </span>
152 <span className="flex items-center gap-1">
153 <Button
154 variant="outline"
155 size="icon-xs"
156 type="button"
157 onClick={actions.zoomOut}
158 aria-label={gt("Zoom out")}
159 disabled={disabled}
160 >
161 <Minus />
162 </Button>
163 <span className="min-w-12 text-center text-xs tabular-nums text-muted-foreground">
164 {zoomLabel}
165 </span>
166 <Button
167 variant="outline"
168 size="icon-xs"
169 type="button"
170 onClick={actions.zoomIn}
171 aria-label={gt("Zoom in")}
172 disabled={disabled}
173 >
174 <PlusIcon />
175 </Button>
176 <Button
177 variant="ghost"
178 size="icon-xs"
179 type="button"
180 onClick={actions.resetZoom}
181 aria-label={gt("Reset zoom")}
182 disabled={disabled}
183 >
184 <RotateCcw />
185 </Button>
186 </span>
187 </MenuItem>
188 <MenuSeparator />
189 <MenuGroup>
190 {profileName ? (
191 <MenuGroupLabel className="max-w-64">
192 <span className="block truncate">
193 <T>
194 Profile: <Var>{profileName}</Var>
195 </T>
196 </span>
197 </MenuGroupLabel>
198 ) : null}
199 <MenuItem onClick={actions.clearCookies}>
200 <T>Clear cookies</T>
201 </MenuItem>
202 <MenuItem onClick={actions.clearCache}>
203 <T>Clear cache</T>
204 </MenuItem>
205 </MenuGroup>
206 </MenuPopup>
207 </Menu>
208 );