main
tsx 111 lines 4.69 KB
Raw
1 import * as React from "react";
2 import * as ToastPrimitives from "@radix-ui/react-toast";
3 import { cva, type VariantProps } from "class-variance-authority";
4 import { X } from "lucide-react";
5
6 import { cn } from "@/lib/utils";
7
8 const ToastProvider = ToastPrimitives.Provider;
9
10 const 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 ));
23 ToastViewport.displayName = ToastPrimitives.Viewport.displayName;
24
25 const 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-background text-foreground",
31 destructive: "destructive group border-destructive bg-destructive text-destructive-foreground",
32 },
33 },
34 defaultVariants: {
35 variant: "default",
36 },
37 },
38 );
39
40 const Toast = React.forwardRef<
41 React.ElementRef<typeof ToastPrimitives.Root>,
42 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> & VariantProps<typeof toastVariants>
43 >(({ className, variant, ...props }, ref) => {
44 return <ToastPrimitives.Root ref={ref} className={cn(toastVariants({ variant }), className)} {...props} />;
45 });
46 Toast.displayName = ToastPrimitives.Root.displayName;
47
48 const ToastAction = React.forwardRef<
49 React.ElementRef<typeof ToastPrimitives.Action>,
50 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action>
51 >(({ className, ...props }, ref) => (
52 <ToastPrimitives.Action
53 ref={ref}
54 className={cn(
55 "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 group-[.destructive]:border-muted/40 hover:bg-secondary group-[.destructive]:hover:border-destructive/30 group-[.destructive]:hover:bg-destructive group-[.destructive]:hover:text-destructive-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 group-[.destructive]:focus:ring-destructive disabled:pointer-events-none disabled:opacity-50",
56 className,
57 )}
58 {...props}
59 />
60 ));
61 ToastAction.displayName = ToastPrimitives.Action.displayName;
62
63 const ToastClose = React.forwardRef<
64 React.ElementRef<typeof ToastPrimitives.Close>,
65 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close>
66 >(({ className, ...props }, ref) => (
67 <ToastPrimitives.Close
68 ref={ref}
69 className={cn(
70 "absolute right-2 top-2 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity group-hover:opacity-100 group-[.destructive]:text-red-300 hover:text-foreground group-[.destructive]:hover:text-red-50 focus:opacity-100 focus:outline-none focus:ring-2 group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600",
71 className,
72 )}
73 toast-close=""
74 {...props}
75 >
76 <X className="h-4 w-4" />
77 </ToastPrimitives.Close>
78 ));
79 ToastClose.displayName = ToastPrimitives.Close.displayName;
80
81 const ToastTitle = React.forwardRef<
82 React.ElementRef<typeof ToastPrimitives.Title>,
83 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
84 >(({ className, ...props }, ref) => (
85 <ToastPrimitives.Title ref={ref} className={cn("text-sm font-semibold", className)} {...props} />
86 ));
87 ToastTitle.displayName = ToastPrimitives.Title.displayName;
88
89 const ToastDescription = React.forwardRef<
90 React.ElementRef<typeof ToastPrimitives.Description>,
91 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
92 >(({ className, ...props }, ref) => (
93 <ToastPrimitives.Description ref={ref} className={cn("text-sm opacity-90", className)} {...props} />
94 ));
95 ToastDescription.displayName = ToastPrimitives.Description.displayName;
96
97 type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>;
98
99 type ToastActionElement = React.ReactElement<typeof ToastAction>;
100
101 export {
102 type ToastProps,
103 type ToastActionElement,
104 ToastProvider,
105 ToastViewport,
106 Toast,
107 ToastTitle,
108 ToastDescription,
109 ToastClose,
110 ToastAction,
111 };