The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | PREVIEW_VIEWPORT_MAX_AREA, |
| 3 | PREVIEW_VIEWPORT_MAX_DIMENSION, |
| 4 | PREVIEW_VIEWPORT_MIN_DIMENSION, |
| 5 | type PreviewViewportSetting, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { PREVIEW_VIEWPORT_PRESETS, resolvePreviewViewport } from "@t3tools/shared/previewViewport"; |
| 8 | import { Link2, Unlink2, X } from "lucide-react"; |
| 9 | import { useState } from "react"; |
| 10 | import { msg, T, useGT, useMessages } from "gt-react"; |
| 11 | |
| 12 | import { Button } from "~/components/ui/button"; |
| 13 | import { Input } from "~/components/ui/input"; |
| 14 | import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip"; |
| 15 | import { |
| 16 | Select, |
| 17 | SelectGroup, |
| 18 | SelectGroupLabel, |
| 19 | SelectItem, |
| 20 | SelectPopup, |
| 21 | SelectTrigger, |
| 22 | SelectValue, |
| 23 | } from "~/components/ui/select"; |
| 24 | import { cn } from "~/lib/utils"; |
| 25 | |
| 26 | import { BROWSER_DEVICE_TOOLBAR_HEIGHT, resizeFreeformViewport } from "./browserViewportLayout"; |
| 27 | import { commitViewportAndAspectRatio } from "./browserDeviceToolbarState"; |
| 28 | import { ScreenRotationIcon } from "./ScreenRotationIcon"; |
| 29 | |
| 30 | const RESPONSIVE_VALUE = "responsive"; |
| 31 | const SELECT_ITEMS = [ |
| 32 | { |
| 33 | value: RESPONSIVE_VALUE, |
| 34 | label: msg("Responsive", { $context: "browser viewport preset that follows the panel size" }), |
| 35 | }, |
| 36 | ...PREVIEW_VIEWPORT_PRESETS.map((preset) => ({ value: preset.id, label: preset.label })), |
| 37 | ]; |
| 38 | |
| 39 | interface Props { |
| 40 | readonly setting: Exclude<PreviewViewportSetting, { readonly _tag: "fill" }>; |
| 41 | readonly width: number; |
| 42 | readonly aspectRatio: number | null; |
| 43 | readonly onAspectRatioChange: (aspectRatio: number | null) => void; |
| 44 | readonly onChange: (setting: PreviewViewportSetting) => Promise<void>; |
| 45 | } |
| 46 | |
| 47 | export function BrowserDeviceToolbar({ |
| 48 | setting, |
| 49 | width, |
| 50 | aspectRatio, |
| 51 | onAspectRatioChange, |
| 52 | onChange, |
| 53 | }: Props) { |
| 54 | const gt = useGT(); |
| 55 | const m = useMessages(); |
| 56 | const [pending, setPending] = useState(false); |
| 57 | const [customSize, setCustomSize] = useState<{ |
| 58 | readonly width: string; |
| 59 | readonly height: string; |
| 60 | readonly axis?: "width" | "height"; |
| 61 | } | null>(null); |
| 62 | const presentedSize = customSize ?? { |
| 63 | width: String(setting.width), |
| 64 | height: String(setting.height), |
| 65 | }; |
| 66 | const selectedValue = |
| 67 | setting._tag === "preset" && |
| 68 | PREVIEW_VIEWPORT_PRESETS.some((preset) => preset.id === setting.presetId) |
| 69 | ? setting.presetId |
| 70 | : RESPONSIVE_VALUE; |
| 71 | const typedWidth = Number(presentedSize.width); |
| 72 | const typedHeight = Number(presentedSize.height); |
| 73 | const { width: customWidth, height: customHeight } = |
| 74 | aspectRatio === null || customSize?.axis === undefined |
| 75 | ? { width: typedWidth, height: typedHeight } |
| 76 | : resizeFreeformViewport( |
| 77 | { width: typedWidth, height: typedHeight }, |
| 78 | { x: 0, y: 0 }, |
| 79 | 1, |
| 80 | customSize?.axis === "height" ? "south" : "east", |
| 81 | aspectRatio, |
| 82 | ); |
| 83 | const customValid = |
| 84 | Number.isInteger(typedWidth) && |
| 85 | Number.isInteger(typedHeight) && |
| 86 | typedWidth >= PREVIEW_VIEWPORT_MIN_DIMENSION && |
| 87 | typedWidth <= PREVIEW_VIEWPORT_MAX_DIMENSION && |
| 88 | typedHeight >= PREVIEW_VIEWPORT_MIN_DIMENSION && |
| 89 | typedHeight <= PREVIEW_VIEWPORT_MAX_DIMENSION && |
| 90 | customWidth * customHeight <= PREVIEW_VIEWPORT_MAX_AREA; |
| 91 | |
| 92 | const apply = (next: PreviewViewportSetting, nextAspectRatio = aspectRatio) => { |
| 93 | setPending(true); |
| 94 | void commitViewportAndAspectRatio(next, nextAspectRatio, onChange, onAspectRatioChange).then( |
| 95 | () => { |
| 96 | setPending(false); |
| 97 | setCustomSize(null); |
| 98 | }, |
| 99 | () => setPending(false), |
| 100 | ); |
| 101 | }; |
| 102 | |
| 103 | const applyCustomSize = () => { |
| 104 | if (!customValid || (customWidth === setting.width && customHeight === setting.height)) { |
| 105 | setCustomSize(null); |
| 106 | return; |
| 107 | } |
| 108 | apply({ _tag: "freeform", width: customWidth, height: customHeight }); |
| 109 | }; |
| 110 | |
| 111 | const updateCustomDimension = (axis: "width" | "height", value: string) => { |
| 112 | setCustomSize((current) => { |
| 113 | const next = { |
| 114 | width: axis === "width" ? value : (current?.width ?? String(setting.width)), |
| 115 | height: axis === "height" ? value : (current?.height ?? String(setting.height)), |
| 116 | axis, |
| 117 | }; |
| 118 | const numeric = Number(value); |
| 119 | if ( |
| 120 | aspectRatio === null || |
| 121 | !Number.isInteger(numeric) || |
| 122 | numeric < PREVIEW_VIEWPORT_MIN_DIMENSION || |
| 123 | numeric > PREVIEW_VIEWPORT_MAX_DIMENSION |
| 124 | ) { |
| 125 | return next; |
| 126 | } |
| 127 | const resized = resizeFreeformViewport( |
| 128 | setting, |
| 129 | axis === "width" |
| 130 | ? { x: numeric - setting.width, y: 0 } |
| 131 | : { x: 0, y: numeric - setting.height }, |
| 132 | 1, |
| 133 | axis === "width" ? "east" : "south", |
| 134 | aspectRatio, |
| 135 | ); |
| 136 | return { |
| 137 | ...next, |
| 138 | width: axis === "width" ? value : String(resized.width), |
| 139 | height: axis === "height" ? value : String(resized.height), |
| 140 | }; |
| 141 | }); |
| 142 | }; |
| 143 | |
| 144 | const selectViewport = (value: string | null) => { |
| 145 | if (!value) return; |
| 146 | if (value === RESPONSIVE_VALUE) { |
| 147 | if (setting._tag === "freeform") return; |
| 148 | apply({ _tag: "freeform", width: setting.width, height: setting.height }); |
| 149 | return; |
| 150 | } |
| 151 | const preset = PREVIEW_VIEWPORT_PRESETS.find((candidate) => candidate.id === value); |
| 152 | if (!preset) return; |
| 153 | apply( |
| 154 | resolvePreviewViewport({ mode: "preset", preset: preset.id }), |
| 155 | aspectRatio === null ? null : preset.width / preset.height, |
| 156 | ); |
| 157 | }; |
| 158 | |
| 159 | const rotate = () => { |
| 160 | const hasCustomSize = |
| 161 | customValid && (customWidth !== setting.width || customHeight !== setting.height); |
| 162 | const source = hasCustomSize |
| 163 | ? ({ _tag: "freeform", width: customWidth, height: customHeight } as const) |
| 164 | : setting; |
| 165 | apply( |
| 166 | { ...source, width: source.height, height: source.width }, |
| 167 | aspectRatio === null ? null : 1 / aspectRatio, |
| 168 | ); |
| 169 | }; |
| 170 | |
| 171 | const toggleAspectRatio = () => { |
| 172 | onAspectRatioChange(aspectRatio === null ? customWidth / customHeight : null); |
| 173 | }; |
| 174 | |
| 175 | return ( |
| 176 | <div |
| 177 | className="sticky left-0 top-0 z-50 flex items-center gap-0.5 overflow-x-auto border-b border-border/70 bg-background/95 px-1.5 shadow-xs backdrop-blur-md [scrollbar-width:none] [&::-webkit-scrollbar]:hidden" |
| 178 | style={{ width, height: BROWSER_DEVICE_TOOLBAR_HEIGHT }} |
| 179 | role="toolbar" |
| 180 | aria-label={gt("Browser device toolbar")} |
| 181 | data-browser-device-toolbar |
| 182 | onBlur={(event) => { |
| 183 | const nextTarget = event.relatedTarget; |
| 184 | if (nextTarget instanceof Node && event.currentTarget.contains(nextTarget)) return; |
| 185 | const eventTarget = event.target; |
| 186 | if ( |
| 187 | (nextTarget instanceof HTMLElement && |
| 188 | nextTarget.closest('[data-slot="select-positioner"]')) || |
| 189 | (eventTarget instanceof HTMLElement && |
| 190 | eventTarget.closest('[data-slot="select-positioner"]')) |
| 191 | ) { |
| 192 | return; |
| 193 | } |
| 194 | applyCustomSize(); |
| 195 | }} |
| 196 | > |
| 197 | {width >= 560 ? ( |
| 198 | <span className="mr-0.5 shrink-0 text-2xs font-medium text-muted-foreground"> |
| 199 | <T>Dimensions</T> |
| 200 | </span> |
| 201 | ) : null} |
| 202 | <Select |
| 203 | modal={false} |
| 204 | value={selectedValue} |
| 205 | onValueChange={selectViewport} |
| 206 | items={SELECT_ITEMS.map((item) => ({ ...item, label: m(item.label) }))} |
| 207 | disabled={pending} |
| 208 | > |
| 209 | <SelectTrigger |
| 210 | variant="ghost" |
| 211 | size="xs" |
| 212 | className={cn("shrink-0 justify-between", width >= 440 ? "w-36" : "w-24")} |
| 213 | aria-label={gt("Browser device preset")} |
| 214 | > |
| 215 | <SelectValue /> |
| 216 | </SelectTrigger> |
| 217 | <SelectPopup align="start" alignItemWithTrigger={false}> |
| 218 | <SelectItem value={RESPONSIVE_VALUE}>{m(SELECT_ITEMS[0]!.label)}</SelectItem> |
| 219 | <SelectGroup> |
| 220 | <SelectGroupLabel> |
| 221 | <T context="group of standard device sizes">Standard</T> |
| 222 | </SelectGroupLabel> |
| 223 | {PREVIEW_VIEWPORT_PRESETS.map((preset) => ( |
| 224 | <SelectItem key={preset.id} value={preset.id}> |
| 225 | <span className="flex w-full items-center justify-between gap-5"> |
| 226 | <span>{preset.label}</span> |
| 227 | <span className="text-xs tabular-nums text-muted-foreground"> |
| 228 | {preset.detail} |
| 229 | </span> |
| 230 | </span> |
| 231 | </SelectItem> |
| 232 | ))} |
| 233 | </SelectGroup> |
| 234 | </SelectPopup> |
| 235 | </Select> |
| 236 | |
| 237 | <form |
| 238 | className="m-0 flex min-w-0 shrink-0 items-center gap-0.5 border-0 p-0" |
| 239 | aria-label={gt("Viewport dimensions")} |
| 240 | onSubmit={(event) => { |
| 241 | event.preventDefault(); |
| 242 | applyCustomSize(); |
| 243 | }} |
| 244 | > |
| 245 | <Input |
| 246 | nativeInput |
| 247 | type="number" |
| 248 | inputMode="numeric" |
| 249 | size="compact" |
| 250 | font="mono" |
| 251 | min={PREVIEW_VIEWPORT_MIN_DIMENSION} |
| 252 | max={PREVIEW_VIEWPORT_MAX_DIMENSION} |
| 253 | value={presentedSize.width} |
| 254 | disabled={pending} |
| 255 | onFocus={() => |
| 256 | setCustomSize( |
| 257 | (current) => |
| 258 | current ?? { |
| 259 | width: String(setting.width), |
| 260 | height: String(setting.height), |
| 261 | }, |
| 262 | ) |
| 263 | } |
| 264 | onChange={(event) => updateCustomDimension("width", event.target.value)} |
| 265 | aria-label={gt("Viewport width")} |
| 266 | aria-invalid={!customValid} |
| 267 | className={width >= 360 ? "w-14" : "w-13"} |
| 268 | /> |
| 269 | <span className="text-xs text-muted-foreground">×</span> |
| 270 | <Input |
| 271 | nativeInput |
| 272 | type="number" |
| 273 | inputMode="numeric" |
| 274 | size="compact" |
| 275 | font="mono" |
| 276 | min={PREVIEW_VIEWPORT_MIN_DIMENSION} |
| 277 | max={PREVIEW_VIEWPORT_MAX_DIMENSION} |
| 278 | value={presentedSize.height} |
| 279 | disabled={pending} |
| 280 | onFocus={() => |
| 281 | setCustomSize( |
| 282 | (current) => |
| 283 | current ?? { |
| 284 | width: String(setting.width), |
| 285 | height: String(setting.height), |
| 286 | }, |
| 287 | ) |
| 288 | } |
| 289 | onChange={(event) => updateCustomDimension("height", event.target.value)} |
| 290 | aria-label={gt("Viewport height")} |
| 291 | aria-invalid={!customValid} |
| 292 | className={width >= 360 ? "w-14" : "w-13"} |
| 293 | /> |
| 294 | </form> |
| 295 | |
| 296 | <Tooltip> |
| 297 | <TooltipTrigger |
| 298 | render={ |
| 299 | <Button |
| 300 | variant={aspectRatio === null ? "ghost" : "secondary"} |
| 301 | size="icon-xs" |
| 302 | type="button" |
| 303 | aria-label={ |
| 304 | aspectRatio === null |
| 305 | ? gt("Lock viewport aspect ratio") |
| 306 | : gt("Unlock viewport aspect ratio") |
| 307 | } |
| 308 | aria-pressed={aspectRatio !== null} |
| 309 | disabled={pending || !customValid} |
| 310 | onPointerDown={(event) => event.preventDefault()} |
| 311 | onClick={toggleAspectRatio} |
| 312 | /> |
| 313 | } |
| 314 | > |
| 315 | {aspectRatio === null ? ( |
| 316 | <Unlink2 className={cn(aspectRatio !== null && "text-foreground")} /> |
| 317 | ) : ( |
| 318 | <Link2 className={cn(aspectRatio !== null && "text-foreground")} /> |
| 319 | )} |
| 320 | </TooltipTrigger> |
| 321 | <TooltipPopup side="top"> |
| 322 | {aspectRatio === null ? <T>Lock aspect ratio</T> : <T>Unlock aspect ratio</T>} |
| 323 | </TooltipPopup> |
| 324 | </Tooltip> |
| 325 | <Button |
| 326 | variant="ghost" |
| 327 | size="icon-xs" |
| 328 | type="button" |
| 329 | aria-label={gt("Rotate viewport")} |
| 330 | disabled={pending} |
| 331 | onClick={rotate} |
| 332 | > |
| 333 | <ScreenRotationIcon /> |
| 334 | </Button> |
| 335 | <span className="sticky right-0 ml-auto flex bg-background/95"> |
| 336 | <Button |
| 337 | variant="ghost" |
| 338 | size="icon-xs" |
| 339 | type="button" |
| 340 | aria-label={gt("Close device toolbar")} |
| 341 | disabled={pending} |
| 342 | onClick={() => { |
| 343 | apply({ _tag: "fill" }, null); |
| 344 | }} |
| 345 | > |
| 346 | <X /> |
| 347 | </Button> |
| 348 | </span> |
| 349 | </div> |
| 350 | ); |
| 351 | } |