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