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