|
| 1 | +import * as ToastPrimitives from "@radix-ui/react-toast"; |
| 2 | +import { cva, type VariantProps } from "class-variance-authority"; |
| 3 | +import * as React from "react"; |
| 4 | +import { cn } from "../../utilities"; |
| 5 | + |
| 6 | +const ToastProvider = ToastPrimitives.Provider; |
| 7 | + |
| 8 | +const ToastViewport = React.forwardRef< |
| 9 | + React.ElementRef<typeof ToastPrimitives.Viewport>, |
| 10 | + React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport> |
| 11 | +>(({ className, ...props }, ref) => ( |
| 12 | + <ToastPrimitives.Viewport |
| 13 | + ref={ref} |
| 14 | + className={cn( |
| 15 | + "fixed top-0 z-[9999] flex max-h-screen w-full flex-col-reverse gap-1 p-4 sm:right-0 sm:top-7 sm:flex-col md:max-w-[480px]", |
| 16 | + className |
| 17 | + )} |
| 18 | + {...props} |
| 19 | + /> |
| 20 | +)); |
| 21 | +ToastViewport.displayName = ToastPrimitives.Viewport.displayName; |
| 22 | + |
| 23 | +export const toastVariants = cva( |
| 24 | + "group pointer-events-auto pl-4 pr-6 py-3 relative flex w-full items-center justify-between overflow-hidden border 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", |
| 25 | + { |
| 26 | + variants: { |
| 27 | + rounded: { |
| 28 | + true: "rounded-md", |
| 29 | + false: "" |
| 30 | + }, |
| 31 | + status: { |
| 32 | + default: "", |
| 33 | + error: "", |
| 34 | + success: "", |
| 35 | + warning: "" |
| 36 | + }, |
| 37 | + emphasis: { |
| 38 | + subtle: "border", |
| 39 | + bold: "" |
| 40 | + } |
| 41 | + }, |
| 42 | + compoundVariants: [ |
| 43 | + { |
| 44 | + emphasis: "bold", |
| 45 | + status: "default", |
| 46 | + className: "white-close bg-theme-surface-strong text-theme-text-negative" |
| 47 | + }, |
| 48 | + { |
| 49 | + emphasis: "subtle", |
| 50 | + status: "default", |
| 51 | + class: "bg-primary-25 border-primary-400 text-theme-text-primary" |
| 52 | + }, |
| 53 | + { |
| 54 | + emphasis: "bold", |
| 55 | + status: "error", |
| 56 | + className: "white-close bg-error-600 text-theme-text-negative" |
| 57 | + }, |
| 58 | + { |
| 59 | + emphasis: "subtle", |
| 60 | + status: "error", |
| 61 | + className: "bg-error-50 border-error-300 text-error-900" |
| 62 | + }, |
| 63 | + { |
| 64 | + emphasis: "bold", |
| 65 | + status: "success", |
| 66 | + className: "white-close bg-success-700 text-theme-text-negative" |
| 67 | + }, |
| 68 | + { |
| 69 | + emphasis: "subtle", |
| 70 | + status: "success", |
| 71 | + className: "bg-success-50 border-success-300 text-success-900" |
| 72 | + }, |
| 73 | + { |
| 74 | + emphasis: "bold", |
| 75 | + status: "warning", |
| 76 | + className: "bg-warning-400" |
| 77 | + }, |
| 78 | + { |
| 79 | + emphasis: "subtle", |
| 80 | + status: "warning", |
| 81 | + className: "bg-warning-50 border-warning-300 text-warning-900" |
| 82 | + } |
| 83 | + ], |
| 84 | + defaultVariants: { |
| 85 | + status: "default" |
| 86 | + } |
| 87 | + } |
| 88 | +); |
| 89 | + |
| 90 | +const Toast = React.forwardRef< |
| 91 | + React.ElementRef<typeof ToastPrimitives.Root>, |
| 92 | + React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> & VariantProps<typeof toastVariants> |
| 93 | +>(({ className, status, rounded, emphasis, ...props }, ref) => { |
| 94 | + return ( |
| 95 | + <ToastPrimitives.Root |
| 96 | + ref={ref} |
| 97 | + className={cn(toastVariants({ status, rounded, emphasis }), className)} |
| 98 | + {...props} |
| 99 | + /> |
| 100 | + ); |
| 101 | +}); |
| 102 | +Toast.displayName = ToastPrimitives.Root.displayName; |
| 103 | + |
| 104 | +const ToastAction = React.forwardRef< |
| 105 | + React.ElementRef<typeof ToastPrimitives.Action>, |
| 106 | + React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action> |
| 107 | +>(({ className, ...props }, ref) => ( |
| 108 | + <ToastPrimitives.Action |
| 109 | + ref={ref} |
| 110 | + className={cn( |
| 111 | + "text-sm ring-offset-background hover:bg-secondary focus:ring-ring inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-transparent px-3 font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:pointer-events-none disabled:opacity-50", |
| 112 | + className |
| 113 | + )} |
| 114 | + {...props} |
| 115 | + /> |
| 116 | +)); |
| 117 | +ToastAction.displayName = ToastPrimitives.Action.displayName; |
| 118 | + |
| 119 | +const ToastClose = React.forwardRef< |
| 120 | + React.ElementRef<typeof ToastPrimitives.Close>, |
| 121 | + React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close> |
| 122 | +>(({ className, ...props }, ref) => ( |
| 123 | + <ToastPrimitives.Close |
| 124 | + ref={ref} |
| 125 | + className={cn( |
| 126 | + "absolute right-2 top-2 rounded-md p-0.5 group-[.white-close]:text-theme-text-negative", |
| 127 | + className |
| 128 | + )} |
| 129 | + toast-close="" |
| 130 | + {...props} |
| 131 | + > |
| 132 | + <svg |
| 133 | + className="h-4 w-4 group-[.white-close]:fill-white" |
| 134 | + viewBox="0 0 24 24" |
| 135 | + fill="black" |
| 136 | + xmlns="http://www.w3.org/2000/svg" |
| 137 | + > |
| 138 | + <path |
| 139 | + fillRule="evenodd" |
| 140 | + clipRule="evenodd" |
| 141 | + d="M5.29289 5.29289C5.68342 4.90237 6.31658 4.90237 6.70711 5.29289L12 10.5858L17.2929 5.29289C17.6834 4.90237 18.3166 4.90237 18.7071 5.29289C19.0976 5.68342 19.0976 6.31658 18.7071 6.70711L13.4142 12L18.7071 17.2929C19.0976 17.6834 19.0976 18.3166 18.7071 18.7071C18.3166 19.0976 17.6834 19.0976 17.2929 18.7071L12 13.4142L6.70711 18.7071C6.31658 19.0976 5.68342 19.0976 5.29289 18.7071C4.90237 18.3166 4.90237 17.6834 5.29289 17.2929L10.5858 12L5.29289 6.70711C4.90237 6.31658 4.90237 5.68342 5.29289 5.29289Z" |
| 142 | + /> |
| 143 | + </svg> |
| 144 | + </ToastPrimitives.Close> |
| 145 | +)); |
| 146 | +ToastClose.displayName = ToastPrimitives.Close.displayName; |
| 147 | + |
| 148 | +const ToastTitle = React.forwardRef< |
| 149 | + React.ElementRef<typeof ToastPrimitives.Title>, |
| 150 | + React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title> |
| 151 | +>(({ className, ...props }, ref) => ( |
| 152 | + <ToastPrimitives.Title ref={ref} className={cn("text-sm font-semibold", className)} {...props} /> |
| 153 | +)); |
| 154 | +ToastTitle.displayName = ToastPrimitives.Title.displayName; |
| 155 | + |
| 156 | +const ToastDescription = React.forwardRef< |
| 157 | + React.ElementRef<typeof ToastPrimitives.Description>, |
| 158 | + React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description> |
| 159 | +>(({ className, ...props }, ref) => ( |
| 160 | + <ToastPrimitives.Description |
| 161 | + ref={ref} |
| 162 | + className={cn("text-sm opacity-90", className)} |
| 163 | + {...props} |
| 164 | + /> |
| 165 | +)); |
| 166 | +ToastDescription.displayName = ToastPrimitives.Description.displayName; |
| 167 | + |
| 168 | +type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>; |
| 169 | + |
| 170 | +type ToastActionElement = React.ReactElement<typeof ToastAction>; |
| 171 | + |
| 172 | +export { |
| 173 | + Toast, |
| 174 | + ToastAction, |
| 175 | + ToastClose, |
| 176 | + ToastDescription, |
| 177 | + ToastProvider, |
| 178 | + ToastTitle, |
| 179 | + ToastViewport, |
| 180 | + type ToastActionElement, |
| 181 | + type ToastProps |
| 182 | +}; |
0 commit comments