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
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}
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 move?: { readonly label: string; readonly onMove: () => void };
54}
55
56const MenuTriggerButton = () => (
57 <Tooltip>
58 <TooltipTrigger
59 render={
60 <MenuTrigger
61 render={<Button variant="ghost" size="icon-xs" type="button" aria-label="Preview menu" />}
62 />
63 }
64 >
65 <MoreVertical />
66 </TooltipTrigger>
67 <TooltipPopup>More</TooltipPopup>
68 </Tooltip>
69);
70
71export function PreviewMoreMenu({
72 enabled,
73 actions,
74 zoomFactor,
75 colorScheme,
76 deviceToolbarVisible,
77 onToggleDeviceToolbar,
78 nativePictureInPicture,
79 profileName,
80 move,
81}: Props) {
82 const zoomLabel = `${Math.round(zoomFactor * 100)}%`;
83 const disabled = !enabled;
84 return (
85 <Menu>
86 <MenuTriggerButton />
87 <MenuPopup align="end" sideOffset={6}>
88 <MenuItem onClick={actions.hardReload} disabled={disabled}>
89 Hard reload
90 </MenuItem>
91 {actions.openDevTools ? (
92 <MenuItem onClick={actions.openDevTools} disabled={disabled}>
93 Open DevTools
94 </MenuItem>
95 ) : null}
96 {actions.toggleNativePictureInPicture ? (
97 <MenuItem onClick={actions.toggleNativePictureInPicture} disabled={disabled}>
98 {nativePictureInPicture
99 ? "Close separate preview window"
100 : "Open separate preview window"}
101 </MenuItem>
102 ) : null}
103 {move ? <MenuItem onClick={move.onMove}>{move.label}</MenuItem> : null}
104 <MenuItem onClick={onToggleDeviceToolbar} disabled={disabled}>
105 {deviceToolbarVisible ? "Hide device toolbar" : "Show device toolbar"}
106 </MenuItem>
107 <MenuSub>
108 <MenuSubTrigger disabled={disabled}>Appearance</MenuSubTrigger>
109 <MenuSubPopup>
110 <MenuRadioGroup
111 value={colorScheme}
112 onValueChange={(value) => actions.setColorScheme(value as DesktopPreviewColorScheme)}
113 >
114 {COLOR_SCHEME_OPTIONS.map((option) => (
115 <MenuRadioItem key={option.value} value={option.value}>
116 {option.label}
117 </MenuRadioItem>
118 ))}
119 </MenuRadioGroup>
120 </MenuSubPopup>
121 </MenuSub>
122 <MenuSeparator />
123 <MenuItem
124 closeOnClick={false}
125 onClick={(event: React.MouseEvent) => event.preventDefault()}
126 className="justify-between"
127 disabled={disabled}
128 >
129 <span>Zoom</span>
130 <span className="flex items-center gap-1">
131 <Button
132 variant="outline"
133 size="icon-xs"
134 type="button"
135 onClick={actions.zoomOut}
136 aria-label="Zoom out"
137 disabled={disabled}
138 >
139 <Minus />
140 </Button>
141 <span className="min-w-12 text-center text-xs tabular-nums text-muted-foreground">
142 {zoomLabel}
143 </span>
144 <Button
145 variant="outline"
146 size="icon-xs"
147 type="button"
148 onClick={actions.zoomIn}
149 aria-label="Zoom in"
150 disabled={disabled}
151 >
152 <PlusIcon />
153 </Button>
154 <Button
155 variant="ghost"
156 size="icon-xs"
157 type="button"
158 onClick={actions.resetZoom}
159 aria-label="Reset zoom"
160 disabled={disabled}
161 >
162 <RotateCcw />
163 </Button>
164 </span>
165 </MenuItem>
166 <MenuSeparator />
167 {/*
168 * Grouped so the heading has a `MenuGroup` ancestor — `MenuGroupLabel`
169 reads its context and throws without one.
170 */}
171 <MenuGroup>
172 {profileName ? (
173 <MenuGroupLabel className="max-w-64">
174 <span className="block truncate">Profile: {profileName}</span>
175 </MenuGroupLabel>
176 ) : null}
177 <MenuItem onClick={actions.clearCookies}>Clear cookies</MenuItem>
178 <MenuItem onClick={actions.clearCache}>Clear cache</MenuItem>
179 </MenuGroup>
180 </MenuPopup>
181 </Menu>
182 );
183}