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

PreviewMoreMenu.tsx

177 lines5.3 KB

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