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 { ScrollArea } from "~/components/ui/scroll-area";
8
9const DialogCreateHandle = DialogPrimitive.createHandle;
10
11const Dialog = DialogPrimitive.Root;
12
13const DialogPortal = DialogPrimitive.Portal;
14
15function DialogTrigger(props: DialogPrimitive.Trigger.Props) {
16 return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />;
17}
18
19function DialogClose(props: DialogPrimitive.Close.Props) {
20 return <DialogPrimitive.Close data-slot="dialog-close" {...props} />;
21}
22
23function DialogBackdrop({ className, ...props }: DialogPrimitive.Backdrop.Props) {
24 return (
25 <DialogPrimitive.Backdrop
26 className={cn(
27 "fixed inset-0 z-50 bg-black/32 backdrop-blur-sm transition-all duration-200 data-ending-style:opacity-0 data-starting-style:opacity-0",
28 className,
29 )}
30 data-slot="dialog-backdrop"
31 {...props}
32 />
33 );
34}
35
36function DialogViewport({ className, ...props }: DialogPrimitive.Viewport.Props) {
37 return (
38 <DialogPrimitive.Viewport
39 className={cn(
40 "fixed inset-0 z-50 grid grid-rows-[1fr_auto_3fr] justify-items-center p-4",
41 className,
42 )}
43 data-slot="dialog-viewport"
44 {...props}
45 />
46 );
47}
48
49function DialogPopup({
50 className,
51 children,
52 showCloseButton = true,
53 bottomStickOnMobile = true,
54 ...props
55}: DialogPrimitive.Popup.Props & {
56 showCloseButton?: boolean;
57 bottomStickOnMobile?: boolean;
58}) {
59 return (
60 <DialogPortal>
61 <DialogBackdrop />
62 <DialogViewport
63 className={cn(bottomStickOnMobile && "max-sm:grid-rows-[1fr_auto] max-sm:p-0 max-sm:pt-12")}
64 >
65 <DialogPrimitive.Popup
66 className={cn(
67 "-translate-y-[calc(1.25rem*var(--nested-dialogs))] relative row-start-2 flex max-h-full min-h-0 w-full min-w-0 max-w-lg scale-[calc(1-0.1*var(--nested-dialogs))] flex-col rounded-2xl border bg-popover not-dark:bg-clip-padding text-popover-foreground opacity-[calc(1-0.1*var(--nested-dialogs))] shadow-lg/5 transition-[scale,opacity,translate] duration-200 ease-in-out will-change-transform before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-2xl)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] data-nested:data-ending-style:translate-y-8 data-nested:data-starting-style:translate-y-8 data-nested-dialog-open:origin-top data-ending-style:scale-98 data-starting-style:scale-98 data-ending-style:opacity-0 data-starting-style:opacity-0 dark:before:shadow-[0_-1px_--theme(--color-white/6%)]",
68 bottomStickOnMobile &&
69 "max-sm:max-w-none max-sm:rounded-none max-sm:border-x-0 max-sm:border-t max-sm:border-b-0 max-sm:opacity-[calc(1-min(var(--nested-dialogs),1))] max-sm:data-ending-style:translate-y-4 max-sm:data-starting-style:translate-y-4 max-sm:before:hidden max-sm:before:rounded-none",
70 className,
71 )}
72 data-slot="dialog-popup"
73 {...props}
74 >
75 {children}
76 {showCloseButton && (
77 <DialogPrimitive.Close
78 aria-label="Close"
79 className="absolute end-2 top-2"
80 render={<Button size="icon" variant="ghost" />}
81 >
82 <XIcon />
83 </DialogPrimitive.Close>
84 )}
85 </DialogPrimitive.Popup>
86 </DialogViewport>
87 </DialogPortal>
88 );
89}
90
91function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
92 return (
93 <div
94 className={cn(
95 "flex flex-col gap-2 p-6 in-[[data-slot=dialog-popup]:has([data-slot=dialog-panel])]:pb-3 max-sm:pb-4",
96 className,
97 )}
98 data-slot="dialog-header"
99 {...props}
100 />
101 );
102}
103
104function DialogFooter({
105 className,
106 variant = "default",
107 ...props
108}: React.ComponentProps<"div"> & {
109 variant?: "default" | "bare";
110}) {
111 return (
112 <div
113 className={cn(
114 "flex flex-col-reverse gap-2 px-6 sm:flex-row sm:justify-end sm:rounded-b-[calc(var(--radius-2xl)-1px)]",
115 variant === "default" && "border-t bg-muted/72 py-4",
116 variant === "bare" &&
117 "in-[[data-slot=dialog-popup]:has([data-slot=dialog-panel])]:pt-3 pt-4 pb-6",
118 className,
119 )}
120 data-slot="dialog-footer"
121 {...props}
122 />
123 );
124}
125
126function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props) {
127 return (
128 <DialogPrimitive.Title
129 className={cn("font-heading font-semibold text-xl leading-none", className)}
130 data-slot="dialog-title"
131 {...props}
132 />
133 );
134}
135
136function DialogDescription({ className, ...props }: DialogPrimitive.Description.Props) {
137 return (
138 <DialogPrimitive.Description
139 className={cn("text-muted-foreground text-sm", className)}
140 data-slot="dialog-description"
141 {...props}
142 />
143 );
144}
145
146function DialogPanel({
147 className,
148 scrollFade = true,
149 ...props
150}: React.ComponentProps<"div"> & { scrollFade?: boolean }) {
151 return (
152 <ScrollArea scrollFade={scrollFade}>
153 <div
154 className={cn(
155 "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",
156 className,
157 )}
158 data-slot="dialog-panel"
159 {...props}
160 />
161 </ScrollArea>
162 );
163}
164
165export {
166 DialogCreateHandle,
167 Dialog,
168 DialogTrigger,
169 DialogPortal,
170 DialogClose,
171 DialogBackdrop,
172 DialogBackdrop as DialogOverlay,
173 DialogPopup,
174 DialogPopup as DialogContent,
175 DialogHeader,
176 DialogFooter,
177 DialogTitle,
178 DialogDescription,
179 DialogPanel,
180 DialogViewport,
181};