Branchwell

Become a Pro / website · becomeapro.ai / src / components / ui / toast.tsx

Raw 4.8 KB on 5069836c1e4c4e369ebe86b13dc7d299c5bed8c0
1import * as React from "react"
2import * as ToastPrimitives from "@radix-ui/react-toast"
3import { cva, type VariantProps } from "class-variance-authority"
4import { X } from "lucide-react"
5
6import { cn } from "@/lib/utils"
7
8const ToastProvider = ToastPrimitives.Provider
9
10const ToastViewport = React.forwardRef<
11 React.ElementRef<typeof ToastPrimitives.Viewport>,
12 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport>
13>(({ className, ...props }, ref) => (
14 <ToastPrimitives.Viewport
15 ref={ref}
16 className={cn(
17 "fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:bottom-0 sm:right-0 sm:top-auto sm:flex-col md:max-w-[420px]",
18 className
19 )}
20 {...props}
21 />
22))
23ToastViewport.displayName = ToastPrimitives.Viewport.displayName
24
25const toastVariants = cva(
26 "group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-6 pr-8 shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full data-[state=open]:sm:slide-in-from-bottom-full",
27 {
28 variants: {
29 variant: {
30 default: "border bg-zinc-800 text-white border-zinc-700",
31 destructive:
32 "destructive group border-red-900 bg-red-800 text-white",
33 success: "border-green-900 bg-green-800 text-white",
34 },
35 },
36 defaultVariants: {
37 variant: "default",
38 },
39 }
40)
41
42const Toast = React.forwardRef<
43 React.ElementRef<typeof ToastPrimitives.Root>,
44 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> &
45 VariantProps<typeof toastVariants>
46>(({ className, variant, ...props }, ref) => {
47 return (
48 <ToastPrimitives.Root
49 ref={ref}
50 className={cn(toastVariants({ variant }), className)}
51 {...props}
52 />
53 )
54})
55Toast.displayName = ToastPrimitives.Root.displayName
56
57const ToastAction = React.forwardRef<
58 React.ElementRef<typeof ToastPrimitives.Action>,
59 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action>
60>(({ className, ...props }, ref) => (
61 <ToastPrimitives.Action
62 ref={ref}
63 className={cn(
64 "inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-transparent px-3 text-sm font-medium ring-offset-background transition-colors hover:bg-secondary focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 group-[.destructive]:border-muted/40 group-[.destructive]:hover:border-destructive/30 group-[.destructive]:hover:bg-destructive group-[.destructive]:hover:text-destructive-foreground group-[.destructive]:focus:ring-destructive",
65 className
66 )}
67 {...props}
68 />
69))
70ToastAction.displayName = ToastPrimitives.Action.displayName
71
72const ToastClose = React.forwardRef<
73 React.ElementRef<typeof ToastPrimitives.Close>,
74 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close>
75>(({ className, ...props }, ref) => (
76 <ToastPrimitives.Close
77 ref={ref}
78 className={cn(
79 "absolute right-2 top-2 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none focus:ring-2 group-hover:opacity-100 group-[.destructive]:text-red-300 group-[.destructive]:hover:text-red-50 group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600",
80 className
81 )}
82 toast-close=""
83 {...props}
84 >
85 <X className="h-4 w-4" />
86 </ToastPrimitives.Close>
87))
88ToastClose.displayName = ToastPrimitives.Close.displayName
89
90const ToastTitle = React.forwardRef<
91 React.ElementRef<typeof ToastPrimitives.Title>,
92 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
93>(({ className, ...props }, ref) => (
94 <ToastPrimitives.Title
95 ref={ref}
96 className={cn("text-sm font-semibold", className)}
97 {...props}
98 />
99))
100ToastTitle.displayName = ToastPrimitives.Title.displayName
101
102const ToastDescription = React.forwardRef<
103 React.ElementRef<typeof ToastPrimitives.Description>,
104 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
105>(({ className, ...props }, ref) => (
106 <ToastPrimitives.Description
107 ref={ref}
108 className={cn("text-sm opacity-90", className)}
109 {...props}
110 />
111))
112ToastDescription.displayName = ToastPrimitives.Description.displayName
113
114type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>
115
116type ToastActionElement = React.ReactElement<typeof ToastAction>
117
118export {
119 type ToastProps,
120 type ToastActionElement,
121 ToastProvider,
122 ToastViewport,
123 Toast,
124 ToastTitle,
125 ToastDescription,
126 ToastClose,
127 ToastAction,
128}