Open History to see the last commit
1import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
2import { useRender } from "@base-ui/react/use-render";
3import { useGT } from "gt-react";
4import { XIcon } from "lucide-react";
5import {
6 type ComponentProps,
7 type CSSProperties,
8 createContext,
9 type ReactNode,
10 use,
11 useRef,
12 useState,
13} from "react";
14import { cn } from "~/lib/utils";
15import { useNativeTitlebarHeight } from "~/lib/windowControlsOverlay";
16import { Button, type ButtonSize, type ButtonVariant } from "~/components/ui/button";
17import {
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";
24import { ScrollArea } from "~/components/ui/scroll-area";
25
26const ControlledOpenContext = createContext<boolean | undefined>(undefined);
27
28function 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
38function 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
46function Dialog<Payload>(props: DialogPrimitive.Root.Props<Payload>) {
47 return (
48 <ControlledOpenContext value={props.open}>
49 <DialogPrimitive.Root {...props} />
50 </ControlledOpenContext>
51 );
52}
53
54function 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
68const DialogPortal = DialogPrimitive.Portal;
69
70function DialogTrigger(props: DialogPrimitive.Trigger.Props) {
71 return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />;
72}
73
74function DialogClose(props: DialogPrimitive.Close.Props) {
75 return <DialogPrimitive.Close data-slot="dialog-close" {...props} />;
76}
77
78function 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
96function 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
110function DialogPopup({
111 className,
112 children,
113 showCloseButton = true,
114 closeButtonSize = "icon",
115 bottomStickOnMobile = true,
116 variant = "default",
117 mediaControlsPlacement = "outside",
118 ...props
119}: DialogPrimitive.Popup.Props & {
120 showCloseButton?: boolean;
121 closeButtonSize?: ButtonSize;
122 bottomStickOnMobile?: boolean;
123 variant?: "default" | "media";
124 mediaControlsPlacement?: "inside" | "outside";
125}) {
126 const gt = useGT();
127 const { popupRef, initialFocus } = useDialogActionInitialFocus();
128 const popup = useRender({
129 ref: popupRef,
130 render: (
131 <DialogPrimitive.Popup
132 className={cn(
133 variant === "media" ? DIALOG_MEDIA_POPUP_CLASS : DIALOG_POPUP_CLASS,
134 "row-start-2 text-popover-foreground",
135 variant === "default" && "max-h-full max-w-lg",
136 bottomStickOnMobile && DIALOG_MOBILE_SHEET_CLASS,
137 className,
138 )}
139 data-slot="dialog-popup"
140 initialFocus={initialFocus}
141 render={renderRetainedPopup}
142 {...props}
143 >
144 {children}
145 {showCloseButton && (
146 <DialogPrimitive.Close
147 aria-label={gt({ context: "button, closes the dialog" })`Close`}
148 className="absolute end-2 top-2"
149 render={<Button size={closeButtonSize} variant="ghost" />}
150 >
151 <XIcon />
152 </DialogPrimitive.Close>
153 )}
154 </DialogPrimitive.Popup>
155 ),
156 });
157 // macOS has no CSS window-controls overlay, so use the platform titlebar height.
158 const nativeTitlebarHeight = useNativeTitlebarHeight();
159 return (
160 <DialogPortal>
161 {/* Media opens from inside other overlays (a composer chip, a popover), so it sits above them. */}
162 <DialogBackdrop className={variant === "media" ? "z-[60]" : undefined} variant={variant} />
163 <DialogViewport
164 className={cn(
165 bottomStickOnMobile && "max-sm:grid-rows-[1fr_auto] max-sm:p-0 max-sm:pt-12",
166 variant === "media" &&
167 "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]",
168 variant === "media" &&
169 mediaControlsPlacement === "inside" &&
170 "pt-[min(calc(var(--media-dialog-titlebar-height,0px)+1.5rem),50dvh)]",
171 )}
172 style={
173 variant === "media"
174 ? ({ "--media-dialog-titlebar-height": `${nativeTitlebarHeight}px` } as CSSProperties)
175 : undefined
176 }
177 >
178 {popup}
179 </DialogViewport>
180 </DialogPortal>
181 );
182}
183
184type DialogActionProps = Omit<React.ComponentProps<typeof Button>, "variant"> & {
185 variant?: Exclude<ButtonVariant, `${string}destructive${string}`>;
186};
187
188function DialogAction(props: DialogActionProps) {
189 return <Button data-slot="dialog-action" {...props} />;
190}
191
192function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
193 return (
194 <div
195 className={cn(
196 "flex flex-col gap-2 p-6 in-[[data-slot=dialog-popup]:has([data-slot=dialog-panel])]:pb-3 max-sm:pb-4",
197 className,
198 )}
199 data-slot="dialog-header"
200 {...props}
201 />
202 );
203}
204
205function DialogFooter({
206 className,
207 variant = "default",
208 ...props
209}: React.ComponentProps<"div"> & {
210 variant?: "default" | "bare";
211}) {
212 return (
213 <div
214 className={cn(
215 "flex flex-col-reverse gap-2 px-6 sm:flex-row sm:flex-wrap sm:justify-end sm:rounded-b-[calc(var(--radius-2xl)-1px)]",
216 variant === "default" && "border-t bg-muted/72 py-4",
217 variant === "bare" && "py-4",
218 className,
219 )}
220 data-slot="dialog-footer"
221 {...props}
222 />
223 );
224}
225
226function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props) {
227 return (
228 <DialogPrimitive.Title
229 className={cn("wrap-anywhere font-semibold text-xl leading-snug", className)}
230 data-slot="dialog-title"
231 {...props}
232 />
233 );
234}
235
236function DialogDescription({ className, ...props }: DialogPrimitive.Description.Props) {
237 return (
238 <DialogPrimitive.Description
239 className={cn("text-muted-foreground text-sm", className)}
240 data-slot="dialog-description"
241 {...props}
242 />
243 );
244}
245
246function DialogPanel({
247 className,
248 scrollFade = true,
249 ...props
250}: React.ComponentProps<"div"> & { scrollFade?: boolean }) {
251 return (
252 <ScrollArea scrollFade={scrollFade}>
253 <div
254 className={cn(
255 "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",
256 className,
257 )}
258 data-slot="dialog-panel"
259 {...props}
260 />
261 </ScrollArea>
262 );
263}
264
265export {
266 ControlledOpenContext,
267 renderRetainedPopup,
268 Dialog,
269 DialogTrigger,
270 DialogPortal,
271 DialogClose,
272 DialogAction,
273 useDialogActionInitialFocus,
274 DialogBackdrop,
275 DialogBackdrop as DialogOverlay,
276 DialogPopup,
277 DialogPopup as DialogContent,
278 DialogHeader,
279 DialogFooter,
280 DialogTitle,
281 DialogDescription,
282 DialogPanel,
283 DialogViewport,
284};