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

PreviewChromeRow.tsx

296 lines9.7 KB

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