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

PreviewChromeRow.tsx

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