alert-dialog.tsx raw
1 import * as React from 'react'
2 import * as AlertDialogPrimitive from '@radix-ui/react-alert-dialog'
3
4 import { cn } from '@/lib/utils'
5 import { buttonVariants } from '@/components/ui/button'
6 import { VariantProps } from 'class-variance-authority'
7
8 const AlertDialog = AlertDialogPrimitive.Root
9
10 const AlertDialogTrigger = AlertDialogPrimitive.Trigger
11
12 const AlertDialogPortal = AlertDialogPrimitive.Portal
13
14 const AlertDialogOverlay = React.forwardRef<
15 React.ElementRef<typeof AlertDialogPrimitive.Overlay>,
16 React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Overlay>
17 >(({ className, ...props }, ref) => (
18 <AlertDialogPrimitive.Overlay
19 className={cn(
20 'fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
21 className
22 )}
23 {...props}
24 ref={ref}
25 />
26 ))
27 AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName
28
29 const AlertDialogContent = React.forwardRef<
30 React.ElementRef<typeof AlertDialogPrimitive.Content>,
31 React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content>
32 >(({ className, ...props }, ref) => (
33 <AlertDialogPortal>
34 <AlertDialogOverlay />
35 <AlertDialogPrimitive.Content
36 ref={ref}
37 className={cn(
38 'fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg',
39 className
40 )}
41 {...props}
42 />
43 </AlertDialogPortal>
44 ))
45 AlertDialogContent.displayName = AlertDialogPrimitive.Content.displayName
46
47 const AlertDialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
48 <div className={cn('flex flex-col space-y-2 text-center sm:text-left', className)} {...props} />
49 )
50 AlertDialogHeader.displayName = 'AlertDialogHeader'
51
52 const AlertDialogFooter = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
53 <div
54 className={cn('flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2', className)}
55 {...props}
56 />
57 )
58 AlertDialogFooter.displayName = 'AlertDialogFooter'
59
60 const AlertDialogTitle = React.forwardRef<
61 React.ElementRef<typeof AlertDialogPrimitive.Title>,
62 React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Title>
63 >(({ className, ...props }, ref) => (
64 <AlertDialogPrimitive.Title
65 ref={ref}
66 className={cn('text-lg font-semibold', className)}
67 {...props}
68 />
69 ))
70 AlertDialogTitle.displayName = AlertDialogPrimitive.Title.displayName
71
72 const AlertDialogDescription = React.forwardRef<
73 React.ElementRef<typeof AlertDialogPrimitive.Description>,
74 React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Description>
75 >(({ className, ...props }, ref) => (
76 <AlertDialogPrimitive.Description
77 ref={ref}
78 className={cn('text-sm text-muted-foreground', className)}
79 {...props}
80 />
81 ))
82 AlertDialogDescription.displayName = AlertDialogPrimitive.Description.displayName
83
84 const AlertDialogAction = React.forwardRef<
85 React.ElementRef<typeof AlertDialogPrimitive.Action>,
86 React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Action> &
87 VariantProps<typeof buttonVariants>
88 >(({ className, variant, size, ...props }, ref) => (
89 <AlertDialogPrimitive.Action
90 ref={ref}
91 className={cn(buttonVariants({ variant, size, className }))}
92 {...props}
93 />
94 ))
95 AlertDialogAction.displayName = AlertDialogPrimitive.Action.displayName
96
97 const AlertDialogCancel = React.forwardRef<
98 React.ElementRef<typeof AlertDialogPrimitive.Cancel>,
99 React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Cancel>
100 >(({ className, ...props }, ref) => (
101 <AlertDialogPrimitive.Cancel
102 ref={ref}
103 className={cn(buttonVariants({ variant: 'outline' }), 'mt-2 sm:mt-0', className)}
104 {...props}
105 />
106 ))
107 AlertDialogCancel.displayName = AlertDialogPrimitive.Cancel.displayName
108
109 export {
110 AlertDialog,
111 AlertDialogPortal,
112 AlertDialogOverlay,
113 AlertDialogTrigger,
114 AlertDialogContent,
115 AlertDialogHeader,
116 AlertDialogFooter,
117 AlertDialogTitle,
118 AlertDialogDescription,
119 AlertDialogAction,
120 AlertDialogCancel
121 }
122