An opinionated toast component for React.
Implement system theme (#97)
Emil Kowalskif58554ba1cb7
diff --git a/README.md b/README.mdindex f8cce86..95b4eb1 100644--- a/README.md+++ b/README.md@@ -139,7 +139,7 @@ toast.custom((t) => (You can change the theme using the `theme` prop. Default theme is light.```jsx-<Toaster theme="dark" />+<Toaster theme="system" />```### Positiondiff --git a/src/index.tsx b/src/index.tsxindex 8ec51a9..dc05660 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -125,14 +125,14 @@ const Toast = (props: ToastProps) => {setInitialHeight(newHeight);- setHeights(heights => {+ setHeights((heights) => {const alreadyExists = heights.find((height) => height.toastId === toast.id);if (!alreadyExists) {- return [{ toastId: toast.id, height: newHeight }, ...heights]+ return [{ toastId: toast.id, height: newHeight }, ...heights];} else {- return heights.map((height) => (height.toastId === toast.id ? { ...height, height: newHeight } : height))+ return heights.map((height) => (height.toastId === toast.id ? { ...height, height: newHeight } : height));}- })+ });}, [mounted, toast.title, toast.description, setHeights, toast.id]);const deleteToast = React.useCallback(() => {@@ -382,7 +382,15 @@ const Toaster = (props: ToasterProps) => {const [heights, setHeights] = React.useState<HeightT[]>([]);const [expanded, setExpanded] = React.useState(false);const [interacting, setInteracting] = React.useState(false);+ const [actualTheme, setActualTheme] = React.useState(+ typeof window !== 'undefined'+ ? window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches+ ? 'dark'+ : 'light'+ : 'light',+ );const [y, x] = position.split('-');+const listRef = React.useRef<HTMLOListElement>(null);const hotkeyLabel = hotkey.join('+').replace(/Key/g, '').replace(/Digit/g, '');@@ -420,6 +428,18 @@ const Toaster = (props: ToasterProps) => {});}, []);+ React.useEffect(() => {+ if (theme !== 'system' || typeof window === 'undefined') return;++ window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', ({ matches }) => {+ if (matches) {+ setActualTheme('dark');+ } else {+ setActualTheme('light');+ }+ });+ }, [theme]);+React.useEffect(() => {// Ensure expanded is always false when no toasts are present / only one leftif (toasts.length <= 1) {@@ -458,7 +478,7 @@ const Toaster = (props: ToasterProps) => {ref={listRef}className={className}data-sonner-toaster- data-theme={theme}+ data-theme={actualTheme}data-rich-colors={richColors}data-y-position={y}data-x-position={x}diff --git a/src/types.ts b/src/types.tsindex 036eb38..fd36f35 100644--- a/src/types.ts+++ b/src/types.ts@@ -51,7 +51,7 @@ interface ToastOptions {export interface ToasterProps {invert?: boolean;- theme?: 'light' | 'dark';+ theme?: 'light' | 'dark' | 'system';position?: Position;hotkey?: string[];richColors?: boolean;diff --git a/website/app/page.tsx b/website/app/page.tsxindex 1fbfe23..c77164b 100644--- a/website/app/page.tsx+++ b/website/app/page.tsx@@ -20,7 +20,7 @@ export default function Home() {return (<>- <Toaster richColors={richColors} closeButton={closeButton} expand={expand} position={position} />+ <Toaster theme="light" richColors={richColors} closeButton={closeButton} expand={expand} position={position} /><main className="container"><Hero /><div className="content">