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