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