Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

dialog.tsx

202 lines5.1 KB

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