import { circleCheck, circleExclamation, circleInfo, cross, triangleExclamation, } from "@simplybusiness/icons"; import classNames from "classnames/dedupe"; import type { ReactNode } from "react"; import { toast as sonnerToast } from "sonner"; import { Icon } from "../Icon"; import { toastState } from "./state"; import type { ToastOptions, ToastVariant } from "./types"; import "./Toast.css"; const variantIcons: Record = { info: circleInfo, success: circleCheck, warning: triangleExclamation, error: circleExclamation, }; const variantColors: Record = { info: "var(--color-info)", success: "var(--color-valid)", warning: "var(--color-warning)", error: "var(--color-error)", }; const ToastIcon = ({ variant }: { variant: ToastVariant }) => ( ); const CloseIcon = () => ( ); type ToastContentProps = { toastId: string | number; variant: ToastVariant; title?: string; description?: ReactNode; action?: ToastOptions["action"]; cancel?: ToastOptions["cancel"]; showCloseButton?: boolean; }; const ToastContent = ({ toastId, variant, title, description, action, cancel, showCloseButton = toastState.showCloseButton, }: ToastContentProps) => (
{title &&
{title}
} {description && (
{description}
)}
{(action || cancel) && (
{cancel && ( )} {action && ( )}
)}
{showCloseButton && ( )}
); const createCustomToast = ( message: string, variant: ToastVariant, options?: ToastOptions, ) => sonnerToast.custom( id => ( ), { duration: options?.duration, onDismiss: options?.onDismiss, onAutoClose: options?.onAutoClose, }, ); export const toast = { /** Show an info toast */ info: (message: string, options?: ToastOptions) => createCustomToast(message, "info", options), /** Show a success toast */ success: (message: string, options?: ToastOptions) => createCustomToast(message, "success", options), /** Show a warning toast */ warning: (message: string, options?: ToastOptions) => createCustomToast(message, "warning", options), /** Show an error toast */ error: (message: string, options?: ToastOptions) => createCustomToast(message, "error", options), /** Dismiss a specific toast by ID or all toasts */ dismiss: (toastId?: string | number) => sonnerToast.dismiss(toastId), };