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