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

dialog.tsx

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