Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

dialog.tsx

143 lines4.0 KB

1import * as React from "react";
2import { cn } from "@/lib/utils";
3import { Dialog as DialogPrimitive } from "radix-ui";
4​
5import { Button } from "@/components/ui/button";
6import { XIcon } from "lucide-react";
7​
8function Dialog({ ...props }: React.ComponentProps<typeof DialogPrimitive.Root>) {
9 return <DialogPrimitive.Root data-slot="dialog" {...props} />;
11​
12function DialogTrigger({ ...props }: React.ComponentProps<typeof DialogPrimitive.Trigger>) {
13 return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />;
15​
16function DialogPortal({ ...props }: React.ComponentProps<typeof DialogPrimitive.Portal>) {
17 return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />;
19​
20function DialogClose({ ...props }: React.ComponentProps<typeof DialogPrimitive.Close>) {
21 return <DialogPrimitive.Close data-slot="dialog-close" {...props} />;
23​
24function DialogOverlay({
25 className,
26 ...props
27}: React.ComponentProps<typeof DialogPrimitive.Overlay>) {
28 return (
29 <DialogPrimitive.Overlay
30 data-slot="dialog-overlay"
31 className={cn(
32 "fixed inset-0 isolate z-50 bg-black/10 duration-100 supports-backdrop-filter:backdrop-blur-xs data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0",
33 className,
34 )}
35 {...props}
36 />
37 );
39​
40function DialogContent({
41 className,
42 children,
43 showCloseButton = true,
44 ...props
45}: React.ComponentProps<typeof DialogPrimitive.Content> & {
46 showCloseButton?: boolean;
47}) {
48 return (
49 <DialogPortal>
50 <DialogOverlay />
51 <DialogPrimitive.Content
52 data-slot="dialog-content"
53 className={cn(
54 "fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 gap-4 rounded-xl bg-popover p-4 text-sm text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
55 className,
56 )}
57 {...props}
58 >
59 {children}
60 {showCloseButton && (
61 <DialogPrimitive.Close data-slot="dialog-close" asChild>
62 <Button variant="ghost" className="absolute top-2 right-2" size="icon-sm">
63 <XIcon />
64 <span className="sr-only">Close</span>
65 </Button>
66 </DialogPrimitive.Close>
67 )}
68 </DialogPrimitive.Content>
69 </DialogPortal>
70 );
72​
73function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
74 return (
75 <div data-slot="dialog-header" className={cn("flex flex-col gap-2", className)} {...props} />
76 );
78​
79function DialogFooter({
80 className,
81 showCloseButton = false,
82 children,
83 ...props
84}: React.ComponentProps<"div"> & {
85 showCloseButton?: boolean;
86}) {
87 return (
88 <div
89 data-slot="dialog-footer"
90 className={cn(
91 "-mx-4 -mb-4 flex flex-col-reverse gap-2 rounded-b-xl border-t bg-muted/50 p-4 sm:flex-row sm:justify-end",
92 className,
93 )}
94 {...props}
95 >
96 {children}
97 {showCloseButton && (
98 <DialogPrimitive.Close asChild>
99 <Button variant="outline">Close</Button>
100 </DialogPrimitive.Close>
101 )}
102 </div>
103 );
105​
106function DialogTitle({ className, ...props }: React.ComponentProps<typeof DialogPrimitive.Title>) {
107 return (
108 <DialogPrimitive.Title
109 data-slot="dialog-title"
110 className={cn("text-base leading-none font-medium", className)}
111 {...props}
112 />
113 );
115​
116function DialogDescription({
117 className,
118 ...props
119}: React.ComponentProps<typeof DialogPrimitive.Description>) {
120 return (
121 <DialogPrimitive.Description
122 data-slot="dialog-description"
123 className={cn(
124 "text-sm text-muted-foreground *:[a]:underline *:[a]:underline-offset-3 *:[a]:hover:text-foreground",
125 className,
126 )}
127 {...props}
128 />
129 );
131​
132export {
133 Dialog,
134 DialogClose,
135 DialogContent,
136 DialogDescription,
137 DialogFooter,
138 DialogHeader,
139 DialogOverlay,
140 DialogPortal,
141 DialogTitle,
142 DialogTrigger,
143};