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

PreviewChromeRow.tsx

291 lines9.3 KB

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