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

PreviewChromeRow.tsx

317 lines10.4 KB

1import {
2 ArrowLeft,
3 ArrowRight,
4 Camera,
5 ExternalLink,
6 MousePointerClick,
7 PictureInPicture2,
8 RotateCw,
9} from "lucide-react";
10import {
11 type FormEvent,
12 type KeyboardEvent,
13 type ReactNode,
14 useEffect,
15 useRef,
16 useState,
17} from "react";
18​
19import { Button } from "~/components/ui/button";
20import { InputGroup, InputGroupAddon, InputGroupInput } from "~/components/ui/input-group";
21import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
22import { cn } from "~/lib/utils";
23​
24interface Props {
25 url: string;
26 loading: boolean;
27 canGoBack: boolean;
28 canGoForward: boolean;
29 refreshDisabled: boolean;
30 inputDisabled?: boolean | undefined;
31 /** Bumping this value re-focuses and selects the URL input. */
32 focusUrlNonce?: number | undefined;
33 onBack: () => void;
34 onForward: () => void;
35 onRefresh: () => void;
36 onSubmit: (url: string) => void;
37 /** When provided, renders an "Open in browser" affordance to the right. */
38 onOpenInBrowser?: (() => void) | undefined;
39 onCapture?: ((record: boolean) => void) | undefined;
40 captureDisabled?: boolean | undefined;
41 recording?: boolean | undefined;
42 onPictureInPicture?: (() => void) | undefined;
43 pictureInPicture?: boolean | undefined;
44 pictureInPictureDisabled?: boolean | undefined;
45 /**
46 * When provided, renders an annotation-mode toggle button to the right of
47 * the URL input. Pressed while annotation mode is active (button shows in `pressed`
48 * state). Disabled in `pickDisabled` mode.
49 */
50 onPickElement?: (() => void) | undefined;
51 pickActive?: boolean | undefined;
52 pickDisabled?: boolean | undefined;
53 /** Optional reason string surfaced in the disabled tooltip. */
54 pickDisabledReason?: string | undefined;
55 /**
56 * Trailing slot rendered after the URL input. Used by the preview view
57 * to mount the three-dot menu (hard reload, devtools, zoom, clear data).
58 */
59 trailingActions?: ReactNode;
61​
62const NOOP = () => {};
63​
64export function PreviewChromeRow({
65 url,
66 loading,
67 canGoBack,
68 canGoForward,
69 refreshDisabled,
70 inputDisabled,
71 focusUrlNonce,
72 onBack,
73 onForward,
74 onRefresh,
75 onSubmit,
76 onOpenInBrowser,
77 onCapture,
78 captureDisabled,
79 recording,
80 onPictureInPicture,
81 pictureInPicture,
82 pictureInPictureDisabled,
83 onPickElement,
84 pickActive,
85 pickDisabled,
86 pickDisabledReason,
87 trailingActions,
88}: Props) {
89 const inputRef = useRef<HTMLInputElement | null>(null);
90 const [draft, setDraft] = useState(url);
91 const [inputFocused, setInputFocused] = useState(false);
92​
93 useEffect(() => {
94 if (focusUrlNonce == null) return;
95 const node = inputRef.current;
96 if (!node) return;
97 node.focus();
98 }, [focusUrlNonce]);
99​
100 const submit = (event?: FormEvent | KeyboardEvent) => {
101 event?.preventDefault();
102 const next = draft.trim();
103 if (next.length === 0) return;
104 onSubmit(next);
105 inputRef.current?.blur();
106 };
107​
108 return (
109 <div className="relative">
110 <form
111 onSubmit={submit}
112 className="flex h-10 min-h-10 shrink-0 items-center gap-1 border-b border-border/60 bg-background px-2 in-data-[preview-panel-mode=inline]:mb-3 in-data-[preview-panel-mode=inline]:h-7 in-data-[preview-panel-mode=inline]:min-h-7 in-data-[preview-panel-mode=inline]:border-b-transparent"
113 data-surface-subheader
114 >
115 <div className="flex items-center gap-0.5" role="group" aria-label="Navigation">
116 <Tooltip>
117 <TooltipTrigger
118 render={
119 <Button
120 variant="ghost"
121 size="icon-xs"
122 onClick={canGoBack ? onBack : NOOP}
123 disabled={!canGoBack}
124 aria-label="Back"
125 type="button"
126 />
127 }
128 >
129 <ArrowLeft />
130 </TooltipTrigger>
131 <TooltipPopup>Back</TooltipPopup>
132 </Tooltip>
133 <Tooltip>
134 <TooltipTrigger
135 render={
136 <Button
137 variant="ghost"
138 size="icon-xs"
139 onClick={canGoForward ? onForward : NOOP}
140 disabled={!canGoForward}
141 aria-label="Forward"
142 type="button"
143 />
144 }
145 >
146 <ArrowRight />
147 </TooltipTrigger>
148 <TooltipPopup>Forward</TooltipPopup>
149 </Tooltip>
150 <Tooltip>
151 <TooltipTrigger
152 render={
153 <Button
154 variant="ghost"
155 size="icon-xs"
156 onClick={refreshDisabled ? NOOP : onRefresh}
157 disabled={refreshDisabled}
158 aria-label={loading ? "Stop" : "Refresh"}
159 type="button"
160 />
161 }
162 >
163 <RotateCw className={cn(loading && "animate-spin")} />
164 </TooltipTrigger>
165 <TooltipPopup>{loading ? "Loading…" : "Refresh"}</TooltipPopup>
166 </Tooltip>
167 </div>
168​
169 <InputGroup variant="ghost" className="group/address h-7 flex-1">
170 <Tooltip>
171 <TooltipTrigger
172 render={
173 <InputGroupInput
174 ref={inputRef}
175 value={inputFocused ? draft : url}
176 className={cn(
177 onOpenInBrowser &&
178 !inputFocused &&
179 "group-hover/address:pe-7 transition-[padding]",
180 )}
181 onChange={(event) => setDraft(event.target.value)}
182 onFocus={() => {
183 setDraft(url);
184 setInputFocused(true);
185 queueMicrotask(() => inputRef.current?.select());
186 }}
187 onBlur={() => {
188 setInputFocused(false);
189 }}
190 onKeyDown={(event) => {
191 if (event.key === "Enter") submit(event);
192 if (event.key === "Escape") {
193 event.preventDefault();
194 setDraft(url);
195 inputRef.current?.blur();
196 }
197 }}
198 placeholder="Search or enter URL"
199 spellCheck={false}
200 disabled={inputDisabled}
201 data-preview-url-input
202 size="sm"
203 />
204 }
205 />
206 </Tooltip>
207 {onOpenInBrowser && !inputFocused ? (
208 <InputGroupAddon
209 align="inline-end"
210 className="pointer-events-none absolute inset-y-0 right-0 opacity-0 transition-opacity group-hover/address:pointer-events-auto group-hover/address:opacity-100"
211 >
212 <Tooltip>
213 <TooltipTrigger
214 render={
215 <Button
216 variant="ghost"
217 size="icon-xs"
218 onClick={onOpenInBrowser}
219 aria-label="Open in system browser"
220 type="button"
221 />
222 }
223 >
224 <ExternalLink />
225 </TooltipTrigger>
226 <TooltipPopup>Open in system browser</TooltipPopup>
227 </Tooltip>
228 </InputGroupAddon>
229 ) : null}
230 </InputGroup>
231​
232 {onPickElement ? (
233 <Tooltip>
234 <TooltipTrigger
235 render={
236 <Button
237 variant={pickActive ? "secondary" : "ghost"}
238 size="icon-xs"
239 onClick={onPickElement}
240 disabled={pickDisabled}
241 aria-label={pickActive ? "Cancel annotation" : "Annotate preview"}
242 aria-pressed={pickActive ? "true" : "false"}
243 type="button"
244 />
245 }
246 >
247 <MousePointerClick className={cn(pickActive && "text-primary")} />
248 </TooltipTrigger>
249 <TooltipPopup>
250 {pickDisabled && pickDisabledReason
251 ? pickDisabledReason
252 : pickActive
253 ? "Cancel annotation (Esc)"
254 : "Annotate elements, regions, and drawings"}
255 </TooltipPopup>
256 </Tooltip>
257 ) : null}
258 {onCapture ? (
259 <Tooltip>
260 <TooltipTrigger
261 render={
262 <Button
263 variant={recording ? "secondary" : "ghost"}
264 size="icon-xs"
265 onClick={(event) => onCapture(event.shiftKey)}
266 aria-label={recording ? "Stop recording" : "Capture screenshot"}
267 type="button"
268 className="relative"
269 disabled={captureDisabled}
270 />
271 }
272 >
273 <Camera className={cn(recording && "text-destructive")} />
274 {recording ? (
275 <span className="absolute right-0.5 top-0.5 size-1.5 animate-status-pulse rounded-full bg-destructive" />
276 ) : null}
277 </TooltipTrigger>
278 <TooltipPopup>
279 {recording ? "Stop recording" : "Screenshot · Shift-click to record"}
280 </TooltipPopup>
281 </Tooltip>
282 ) : null}
283 {onPictureInPicture ? (
284 <Tooltip>
285 <TooltipTrigger
286 render={
287 <Button
288 variant={pictureInPicture ? "secondary" : "ghost"}
289 size="icon-xs"
290 onClick={onPictureInPicture}
291 aria-label={
292 pictureInPicture ? "Close floating preview" : "Float preview over chat"
293 }
294 aria-pressed={pictureInPicture ? "true" : "false"}
295 type="button"
296 disabled={pictureInPictureDisabled}
297 />
298 }
299 >
300 <PictureInPicture2 className={cn(pictureInPicture && "text-primary")} />
301 </TooltipTrigger>
302 <TooltipPopup>
303 {pictureInPicture ? "Close floating preview" : "Float preview over chat"}
304 </TooltipPopup>
305 </Tooltip>
306 ) : null}
307 {trailingActions}
308 </form>
309 <div
310 aria-hidden
311 data-loading={loading}
312 className="preview-loading-progress pointer-events-none absolute bottom-0 left-0 z-10 h-0.5 w-full origin-left rounded-r-full bg-primary"
313 style={{ boxShadow: "0 0 6px 1px var(--color-ring)" }}
314 />
315 </div>
316 );