104 lines
2.4 KiB
TypeScript
104 lines
2.4 KiB
TypeScript
import { type ExternalToast, toast as sonnerToast, Toaster } from "sonner"
|
|
|
|
import {
|
|
CheckCircleIcon,
|
|
CloseLargeIcon,
|
|
CrossCircle,
|
|
InfoCircleIcon,
|
|
WarningTriangle,
|
|
} from "@/components/Icons"
|
|
|
|
import Button from "../Button"
|
|
import Body from "../Text/Body"
|
|
import { toastVariants } from "./variants"
|
|
|
|
import styles from "./toasts.module.css"
|
|
|
|
import type { ToastsProps } from "./toasts"
|
|
|
|
export function ToastHandler() {
|
|
return <Toaster position="bottom-right" duration={5000} />
|
|
}
|
|
|
|
function getIcon(variant: ToastsProps["variant"]) {
|
|
switch (variant) {
|
|
case "error":
|
|
return CrossCircle
|
|
case "info":
|
|
return InfoCircleIcon
|
|
case "success":
|
|
return CheckCircleIcon
|
|
case "warning":
|
|
return WarningTriangle
|
|
}
|
|
}
|
|
|
|
export function Toast({ children, message, onClose, variant }: ToastsProps) {
|
|
const className = toastVariants({ variant })
|
|
const Icon = getIcon(variant)
|
|
return (
|
|
<div className={className}>
|
|
<div className={styles.iconContainer}>
|
|
{Icon && <Icon color="white" height={24} width={24} />}
|
|
</div>
|
|
{message ? (
|
|
<Body className={styles.message}>{message}</Body>
|
|
) : (
|
|
<div className={styles.content}>{children}</div>
|
|
)}
|
|
{onClose ? (
|
|
<Button onClick={onClose} variant="icon" intent="tertiary">
|
|
<CloseLargeIcon />
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export const toast = {
|
|
success: (message: React.ReactNode, options?: ExternalToast) =>
|
|
sonnerToast.custom(
|
|
(t) => (
|
|
<Toast
|
|
variant="success"
|
|
message={message}
|
|
onClose={() => sonnerToast.dismiss(t)}
|
|
/>
|
|
),
|
|
options
|
|
),
|
|
info: (message: React.ReactNode, options?: ExternalToast) =>
|
|
sonnerToast.custom(
|
|
(t) => (
|
|
<Toast
|
|
variant="info"
|
|
message={message}
|
|
onClose={() => sonnerToast.dismiss(t)}
|
|
/>
|
|
),
|
|
options
|
|
),
|
|
error: (message: React.ReactNode, options?: ExternalToast) =>
|
|
sonnerToast.custom(
|
|
(t) => (
|
|
<Toast
|
|
variant="error"
|
|
message={message}
|
|
onClose={() => sonnerToast.dismiss(t)}
|
|
/>
|
|
),
|
|
options
|
|
),
|
|
warning: (message: React.ReactNode, options?: ExternalToast) =>
|
|
sonnerToast.custom(
|
|
(t) => (
|
|
<Toast
|
|
variant="warning"
|
|
message={message}
|
|
onClose={() => sonnerToast.dismiss(t)}
|
|
/>
|
|
),
|
|
options
|
|
),
|
|
}
|