| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 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 { cn } from "@/lib/utils"; |
| 5 | import { Cross2Icon } from "@radix-ui/react-icons"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 6 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 7 | const ToastProvider = ToastPrimitives.Provider; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 8 | |
| 9 | const ToastViewport = React.forwardRef< |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 10 | React.ElementRef<typeof ToastPrimitives.Viewport>, |
| 11 | React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport> |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 12 | >(({ className, ...props }, ref) => ( |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 13 | <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 | )); |
| 22 | ToastViewport.displayName = ToastPrimitives.Viewport.displayName; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 23 | |
| 24 | const toastVariants = cva( |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 25 | "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 | ); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 38 | |
| 39 | const Toast = React.forwardRef< |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 40 | React.ElementRef<typeof ToastPrimitives.Root>, |
| 41 | React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> & VariantProps<typeof toastVariants> |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 42 | >(({ className, variant, ...props }, ref) => { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 43 | return <ToastPrimitives.Root ref={ref} className={cn(toastVariants({ variant }), className)} {...props} />; |
| 44 | }); |
| 45 | Toast.displayName = ToastPrimitives.Root.displayName; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 46 | |
| 47 | const ToastAction = React.forwardRef< |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 48 | React.ElementRef<typeof ToastPrimitives.Action>, |
| 49 | React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action> |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 50 | >(({ className, ...props }, ref) => ( |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 51 | <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 | )); |
| 60 | ToastAction.displayName = ToastPrimitives.Action.displayName; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 61 | |
| 62 | const ToastClose = React.forwardRef< |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 63 | React.ElementRef<typeof ToastPrimitives.Close>, |
| 64 | React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close> |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 65 | >(({ className, ...props }, ref) => ( |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 66 | <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 | )); |
| 78 | ToastClose.displayName = ToastPrimitives.Close.displayName; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 79 | |
| 80 | const ToastTitle = React.forwardRef< |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 81 | React.ElementRef<typeof ToastPrimitives.Title>, |
| 82 | React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title> |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 83 | >(({ className, ...props }, ref) => ( |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 84 | <ToastPrimitives.Title ref={ref} className={cn("text-sm font-semibold [&+div]:text-xs", className)} {...props} /> |
| 85 | )); |
| 86 | ToastTitle.displayName = ToastPrimitives.Title.displayName; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 87 | |
| 88 | const ToastDescription = React.forwardRef< |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 89 | React.ElementRef<typeof ToastPrimitives.Description>, |
| 90 | React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description> |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 91 | >(({ className, ...props }, ref) => ( |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 92 | <ToastPrimitives.Description ref={ref} className={cn("text-sm opacity-90", className)} {...props} /> |
| 93 | )); |
| 94 | ToastDescription.displayName = ToastPrimitives.Description.displayName; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 95 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 96 | type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 97 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 98 | type ToastActionElement = React.ReactElement<typeof ToastAction>; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 99 | |
| 100 | export { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 101 | type ToastProps, |
| 102 | type ToastActionElement, |
| 103 | ToastProvider, |
| 104 | ToastViewport, |
| 105 | Toast, |
| 106 | ToastTitle, |
| 107 | ToastDescription, |
| 108 | ToastClose, |
| 109 | ToastAction, |
| 110 | }; |