59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { Toaster as Sonner, type ToasterProps } from "sonner";
|
|
import {
|
|
IconCircleCheck,
|
|
IconInfoCircle,
|
|
IconAlertTriangle,
|
|
IconAlertOctagon,
|
|
IconLoader,
|
|
} from "@tabler/icons-react";
|
|
|
|
function readDocumentTheme(): ToasterProps["theme"] {
|
|
if (typeof document === "undefined") return "system";
|
|
return document.documentElement.classList.contains("dark") ? "dark" : "light";
|
|
}
|
|
|
|
const Toaster = ({ ...props }: ToasterProps) => {
|
|
const [theme, setTheme] = useState<ToasterProps["theme"]>(() => readDocumentTheme());
|
|
|
|
useEffect(() => {
|
|
const observer = new MutationObserver(() => setTheme(readDocumentTheme()));
|
|
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
|
|
// An ancestor can apply the theme between render and this subscription.
|
|
setTheme(readDocumentTheme());
|
|
return () => observer.disconnect();
|
|
}, []);
|
|
|
|
return (
|
|
<Sonner
|
|
theme={theme}
|
|
className="toaster group"
|
|
icons={{
|
|
success: <IconCircleCheck className="size-4" />,
|
|
info: <IconInfoCircle className="size-4" />,
|
|
warning: <IconAlertTriangle className="size-4" />,
|
|
error: <IconAlertOctagon className="size-4" />,
|
|
loading: <IconLoader className="size-4 animate-spin" />,
|
|
}}
|
|
style={
|
|
{
|
|
"--normal-bg": "var(--popover)",
|
|
"--normal-text": "var(--popover-foreground)",
|
|
"--normal-border": "var(--border)",
|
|
"--border-radius": "var(--radius)",
|
|
} as React.CSSProperties
|
|
}
|
|
toastOptions={{
|
|
classNames: {
|
|
toast: "cn-toast",
|
|
},
|
|
}}
|
|
{...props}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export { Toaster };
|