blob: c919aee4376e6aeff67367855505334b191ed6d4 [file] [log] [blame]
giod0026612025-05-08 13:00:36 +00001import * as React from "react";
2import * as ToastPrimitives from "@radix-ui/react-toast";
3import { cva, type VariantProps } from "class-variance-authority";
4import { cn } from "@/lib/utils";
5import { Cross2Icon } from "@radix-ui/react-icons";
gio5f2f1002025-03-20 18:38:48 +04006
giod0026612025-05-08 13:00:36 +00007const ToastProvider = ToastPrimitives.Provider;
gio5f2f1002025-03-20 18:38:48 +04008
9const ToastViewport = React.forwardRef<
giod0026612025-05-08 13:00:36 +000010 React.ElementRef<typeof ToastPrimitives.Viewport>,
11 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport>
gio5f2f1002025-03-20 18:38:48 +040012>(({ className, ...props }, ref) => (
giod0026612025-05-08 13:00:36 +000013 <ToastPrimitives.Viewport
14 ref={ref}
15 className={cn(
16 "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]",
17 className,
18 )}
19 {...props}
20 />
21));
22ToastViewport.displayName = ToastPrimitives.Viewport.displayName;
gio5f2f1002025-03-20 18:38:48 +040023
24const toastVariants = cva(
giod0026612025-05-08 13:00:36 +000025 "group pointer-events-auto relative flex w-full items-center justify-between space-x-2 overflow-hidden rounded-md border p-4 pr-6 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",
26 {
27 variants: {
28 variant: {
29 default: "border bg-background text-foreground",
30 destructive: "destructive group border-destructive bg-destructive text-destructive-foreground",
31 },
32 },
33 defaultVariants: {
34 variant: "default",
35 },
36 },
37);
gio5f2f1002025-03-20 18:38:48 +040038
39const Toast = React.forwardRef<
giod0026612025-05-08 13:00:36 +000040 React.ElementRef<typeof ToastPrimitives.Root>,
41 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> & VariantProps<typeof toastVariants>
gio5f2f1002025-03-20 18:38:48 +040042>(({ className, variant, ...props }, ref) => {
giod0026612025-05-08 13:00:36 +000043 return <ToastPrimitives.Root ref={ref} className={cn(toastVariants({ variant }), className)} {...props} />;
44});
45Toast.displayName = ToastPrimitives.Root.displayName;
gio5f2f1002025-03-20 18:38:48 +040046
47const ToastAction = React.forwardRef<
giod0026612025-05-08 13:00:36 +000048 React.ElementRef<typeof ToastPrimitives.Action>,
49 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action>
gio5f2f1002025-03-20 18:38:48 +040050>(({ className, ...props }, ref) => (
giod0026612025-05-08 13:00:36 +000051 <ToastPrimitives.Action
52 ref={ref}
53 className={cn(
54 "inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-transparent px-3 text-sm font-medium transition-colors hover:bg-secondary focus:outline-none focus:ring-1 focus:ring-ring 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",
55 className,
56 )}
57 {...props}
58 />
59));
60ToastAction.displayName = ToastPrimitives.Action.displayName;
gio5f2f1002025-03-20 18:38:48 +040061
62const ToastClose = React.forwardRef<
giod0026612025-05-08 13:00:36 +000063 React.ElementRef<typeof ToastPrimitives.Close>,
64 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close>
gio5f2f1002025-03-20 18:38:48 +040065>(({ className, ...props }, ref) => (
giod0026612025-05-08 13:00:36 +000066 <ToastPrimitives.Close
67 ref={ref}
68 className={cn(
69 "absolute right-1 top-1 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none focus:ring-1 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",
70 className,
71 )}
72 toast-close=""
73 {...props}
74 >
75 <Cross2Icon className="h-4 w-4" />
76 </ToastPrimitives.Close>
77));
78ToastClose.displayName = ToastPrimitives.Close.displayName;
gio5f2f1002025-03-20 18:38:48 +040079
80const ToastTitle = React.forwardRef<
giod0026612025-05-08 13:00:36 +000081 React.ElementRef<typeof ToastPrimitives.Title>,
82 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
gio5f2f1002025-03-20 18:38:48 +040083>(({ className, ...props }, ref) => (
giod0026612025-05-08 13:00:36 +000084 <ToastPrimitives.Title ref={ref} className={cn("text-sm font-semibold [&+div]:text-xs", className)} {...props} />
85));
86ToastTitle.displayName = ToastPrimitives.Title.displayName;
gio5f2f1002025-03-20 18:38:48 +040087
88const ToastDescription = React.forwardRef<
giod0026612025-05-08 13:00:36 +000089 React.ElementRef<typeof ToastPrimitives.Description>,
90 React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
gio5f2f1002025-03-20 18:38:48 +040091>(({ className, ...props }, ref) => (
giod0026612025-05-08 13:00:36 +000092 <ToastPrimitives.Description ref={ref} className={cn("text-sm opacity-90", className)} {...props} />
93));
94ToastDescription.displayName = ToastPrimitives.Description.displayName;
gio5f2f1002025-03-20 18:38:48 +040095
giod0026612025-05-08 13:00:36 +000096type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>;
gio5f2f1002025-03-20 18:38:48 +040097
giod0026612025-05-08 13:00:36 +000098type ToastActionElement = React.ReactElement<typeof ToastAction>;
gio5f2f1002025-03-20 18:38:48 +040099
100export {
giod0026612025-05-08 13:00:36 +0000101 type ToastProps,
102 type ToastActionElement,
103 ToastProvider,
104 ToastViewport,
105 Toast,
106 ToastTitle,
107 ToastDescription,
108 ToastClose,
109 ToastAction,
110};