The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 282 lines 8.11 KB
Raw
1 import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
2 import { useRender } from "@base-ui/react/use-render";
3 import { useGT } from "gt-react";
4 import { XIcon } from "lucide-react";
5 import {
6 type ComponentProps,
7 type CSSProperties,
8 createContext,
9 type ReactNode,
10 use,
11 useRef,
12 useState,
13 } from "react";
14 import { cn } from "~/lib/utils";
15 import { useNativeTitlebarHeight } from "~/lib/windowControlsOverlay";
16 import { Button, type ButtonVariant } from "~/components/ui/button";
17 import {
18 DIALOG_BACKDROP_CLASS,
19 DIALOG_MEDIA_BACKDROP_CLASS,
20 DIALOG_MEDIA_POPUP_CLASS,
21 DIALOG_MOBILE_SHEET_CLASS,
22 DIALOG_POPUP_CLASS,
23 } from "~/components/ui/dialog-styles";
24 import { ScrollArea } from "~/components/ui/scroll-area";
25
26 const ControlledOpenContext = createContext<boolean | undefined>(undefined);
27
28 function RetainedWhileClosed({ open, children }: { open: boolean; children: ReactNode }) {
29 const controlledOpen = use(ControlledOpenContext);
30 const isOpen = open && controlledOpen !== false;
31 const [retained, setRetained] = useState(children);
32 if (isOpen && retained !== children) {
33 setRetained(children);
34 }
35 return isOpen ? children : retained;
36 }
37
38 function renderRetainedPopup(props: ComponentProps<"div">, state: { open: boolean }) {
39 return (
40 <div {...props}>
41 <RetainedWhileClosed open={state.open}>{props.children}</RetainedWhileClosed>
42 </div>
43 );
44 }
45
46 function Dialog<Payload>(props: DialogPrimitive.Root.Props<Payload>) {
47 return (
48 <ControlledOpenContext value={props.open}>
49 <DialogPrimitive.Root {...props} />
50 </ControlledOpenContext>
51 );
52 }
53
54 function useDialogActionInitialFocus() {
55 const popupRef = useRef<HTMLDivElement>(null);
56 const initialFocus = (openType: string) => {
57 // Match Base UI: touch opens focus the popup to avoid opening the virtual keyboard.
58 if (openType === "touch") return popupRef.current;
59 return (
60 popupRef.current?.querySelector<HTMLElement>(
61 '[data-slot="dialog-action"]:not(:disabled):not([aria-disabled="true"])',
62 ) ?? true
63 );
64 };
65 return { popupRef, initialFocus };
66 }
67
68 const DialogPortal = DialogPrimitive.Portal;
69
70 function DialogTrigger(props: DialogPrimitive.Trigger.Props) {
71 return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />;
72 }
73
74 function DialogClose(props: DialogPrimitive.Close.Props) {
75 return <DialogPrimitive.Close data-slot="dialog-close" {...props} />;
76 }
77
78 function DialogBackdrop({
79 className,
80 variant = "default",
81 ...props
82 }: DialogPrimitive.Backdrop.Props & { variant?: "default" | "media" }) {
83 return (
84 <DialogPrimitive.Backdrop
85 forceRender
86 className={cn(
87 variant === "media" ? DIALOG_MEDIA_BACKDROP_CLASS : DIALOG_BACKDROP_CLASS,
88 className,
89 )}
90 data-slot="dialog-backdrop"
91 {...props}
92 />
93 );
94 }
95
96 function DialogViewport({ className, ...props }: DialogPrimitive.Viewport.Props) {
97 return (
98 <DialogPrimitive.Viewport
99 // Electron drag regions ignore z-index, so without no-drag a click over a header drag strip never reaches the page.
100 className={cn(
101 "fixed inset-0 z-50 grid grid-rows-[1fr_auto_1fr] justify-items-center p-4 [-webkit-app-region:no-drag]",
102 className,
103 )}
104 data-slot="dialog-viewport"
105 {...props}
106 />
107 );
108 }
109
110 function DialogPopup({
111 className,
112 children,
113 showCloseButton = true,
114 bottomStickOnMobile = true,
115 variant = "default",
116 mediaControlsPlacement = "outside",
117 ...props
118 }: DialogPrimitive.Popup.Props & {
119 showCloseButton?: boolean;
120 bottomStickOnMobile?: boolean;
121 variant?: "default" | "media";
122 mediaControlsPlacement?: "inside" | "outside";
123 }) {
124 const gt = useGT();
125 const { popupRef, initialFocus } = useDialogActionInitialFocus();
126 const popup = useRender({
127 ref: popupRef,
128 render: (
129 <DialogPrimitive.Popup
130 className={cn(
131 variant === "media" ? DIALOG_MEDIA_POPUP_CLASS : DIALOG_POPUP_CLASS,
132 "row-start-2 text-popover-foreground",
133 variant === "default" && "max-h-full max-w-lg",
134 bottomStickOnMobile && DIALOG_MOBILE_SHEET_CLASS,
135 className,
136 )}
137 data-slot="dialog-popup"
138 initialFocus={initialFocus}
139 render={renderRetainedPopup}
140 {...props}
141 >
142 {children}
143 {showCloseButton && (
144 <DialogPrimitive.Close
145 aria-label={gt("Close", { $context: "button, closes the dialog" })}
146 className="absolute end-2 top-2"
147 render={<Button size="icon" variant="ghost" />}
148 >
149 <XIcon />
150 </DialogPrimitive.Close>
151 )}
152 </DialogPrimitive.Popup>
153 ),
154 });
155 // macOS has no CSS window-controls overlay, so use the platform titlebar height.
156 const nativeTitlebarHeight = useNativeTitlebarHeight();
157 return (
158 <DialogPortal>
159 {/* Media opens from inside other overlays (a composer chip, a popover), so it sits above them. */}
160 <DialogBackdrop className={variant === "media" ? "z-[60]" : undefined} variant={variant} />
161 <DialogViewport
162 className={cn(
163 bottomStickOnMobile && "max-sm:grid-rows-[1fr_auto] max-sm:p-0 max-sm:pt-12",
164 variant === "media" &&
165 "z-[60] grid-rows-1 place-items-center px-4 max-[8rem]:px-0 pb-6 pt-[calc(var(--media-dialog-titlebar-height,0px)+1.5rem)] [-webkit-app-region:no-drag]",
166 variant === "media" &&
167 mediaControlsPlacement === "inside" &&
168 "pt-[min(calc(var(--media-dialog-titlebar-height,0px)+1.5rem),50dvh)]",
169 )}
170 style={
171 variant === "media"
172 ? ({ "--media-dialog-titlebar-height": `${nativeTitlebarHeight}px` } as CSSProperties)
173 : undefined
174 }
175 >
176 {popup}
177 </DialogViewport>
178 </DialogPortal>
179 );
180 }
181
182 type DialogActionProps = Omit<React.ComponentProps<typeof Button>, "variant"> & {
183 variant?: Exclude<ButtonVariant, `${string}destructive${string}`>;
184 };
185
186 function DialogAction(props: DialogActionProps) {
187 return <Button data-slot="dialog-action" {...props} />;
188 }
189
190 function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
191 return (
192 <div
193 className={cn(
194 "flex flex-col gap-2 p-6 in-[[data-slot=dialog-popup]:has([data-slot=dialog-panel])]:pb-3 max-sm:pb-4",
195 className,
196 )}
197 data-slot="dialog-header"
198 {...props}
199 />
200 );
201 }
202
203 function DialogFooter({
204 className,
205 variant = "default",
206 ...props
207 }: React.ComponentProps<"div"> & {
208 variant?: "default" | "bare";
209 }) {
210 return (
211 <div
212 className={cn(
213 "flex flex-col-reverse gap-2 px-6 sm:flex-row sm:justify-end sm:rounded-b-[calc(var(--radius-2xl)-1px)]",
214 variant === "default" && "border-t bg-muted/72 py-4",
215 variant === "bare" && "py-4",
216 className,
217 )}
218 data-slot="dialog-footer"
219 {...props}
220 />
221 );
222 }
223
224 function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props) {
225 return (
226 <DialogPrimitive.Title
227 className={cn("wrap-anywhere font-semibold text-xl leading-snug", className)}
228 data-slot="dialog-title"
229 {...props}
230 />
231 );
232 }
233
234 function DialogDescription({ className, ...props }: DialogPrimitive.Description.Props) {
235 return (
236 <DialogPrimitive.Description
237 className={cn("text-muted-foreground text-sm", className)}
238 data-slot="dialog-description"
239 {...props}
240 />
241 );
242 }
243
244 function DialogPanel({
245 className,
246 scrollFade = true,
247 ...props
248 }: React.ComponentProps<"div"> & { scrollFade?: boolean }) {
249 return (
250 <ScrollArea scrollFade={scrollFade}>
251 <div
252 className={cn(
253 "space-y-4 p-6 in-[[data-slot=dialog-popup]:has([data-slot=dialog-header])]:pt-1 in-[[data-slot=dialog-popup]:has([data-slot=dialog-footer]:not(.border-t))]:pb-1",
254 className,
255 )}
256 data-slot="dialog-panel"
257 {...props}
258 />
259 </ScrollArea>
260 );
261 }
262
263 export {
264 ControlledOpenContext,
265 renderRetainedPopup,
266 Dialog,
267 DialogTrigger,
268 DialogPortal,
269 DialogClose,
270 DialogAction,
271 useDialogActionInitialFocus,
272 DialogBackdrop,
273 DialogBackdrop as DialogOverlay,
274 DialogPopup,
275 DialogPopup as DialogContent,
276 DialogHeader,
277 DialogFooter,
278 DialogTitle,
279 DialogDescription,
280 DialogPanel,
281 DialogViewport,
282 };