sonner.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { useEffect, useState } from "react"
  2. import { Toaster as Sonner } from "sonner"
  3. type ToasterProps = React.ComponentProps<typeof Sonner>
  4. const Toaster = ({ ...props }: ToasterProps) => {
  5. const [theme, setTheme] = useState<"light" | "dark">("light")
  6. const [isStandalone, setIsStandalone] = useState(false)
  7. useEffect(() => {
  8. // Check initial theme
  9. const isDark = document.documentElement.classList.contains("dark")
  10. setTheme(isDark ? "dark" : "light")
  11. // Check if running as PWA (standalone mode)
  12. const standalone = window.matchMedia('(display-mode: standalone)').matches ||
  13. (window.navigator as unknown as { standalone?: boolean }).standalone === true
  14. setIsStandalone(standalone)
  15. // Watch for theme changes
  16. const observer = new MutationObserver((mutations) => {
  17. mutations.forEach((mutation) => {
  18. if (mutation.attributeName === "class") {
  19. const isDark = document.documentElement.classList.contains("dark")
  20. setTheme(isDark ? "dark" : "light")
  21. }
  22. })
  23. })
  24. observer.observe(document.documentElement, { attributes: true })
  25. return () => observer.disconnect()
  26. }, [])
  27. // Use larger offset for PWA to account for Dynamic Island/notch (59px typical + 16px padding)
  28. const offset = isStandalone ? 75 : 16
  29. return (
  30. <Sonner
  31. theme={theme}
  32. className="toaster group"
  33. offset={offset}
  34. toastOptions={{
  35. classNames: {
  36. toast:
  37. "group toast group-[.toaster]:bg-background group-[.toaster]:text-foreground group-[.toaster]:border-border group-[.toaster]:shadow-lg",
  38. description: "group-[.toast]:text-muted-foreground",
  39. actionButton:
  40. "group-[.toast]:bg-primary group-[.toast]:text-primary-foreground",
  41. cancelButton:
  42. "group-[.toast]:bg-muted group-[.toast]:text-muted-foreground",
  43. },
  44. }}
  45. {...props}
  46. />
  47. )
  48. }
  49. export { Toaster }