11---
2- title : ' Dark Mode'
3- description : ' This page shows examples of how to use next-themes ( Next.js) or remix-themes (Remix) libraries '
4- category : ' Customization'
2+ title : " Dark Mode"
3+ description : " This page shows examples of how to set up the dark mode theme for the Toaster component. Using Vite, Next.js (with next-themes ) or Remix (with remix-themes). "
4+ category : " Customization"
55---
66
77> 💡 If you need to activate the dark mode directly, you can use the ` theme ` property: [ ` show ` ] ( /toaster#theme )
@@ -14,9 +14,9 @@ category: 'Customization'
1414- Snippet from [ ` shadcn/ui ` Dark Mode documentation] ( https://ui.shadcn.com/docs/dark-mode/vite#dark-mode ) .
1515
1616``` tsx
17- import { createContext , useContext , useEffect , useState } from ' react' ;
17+ import { createContext , useContext , useEffect , useState } from " react" ;
1818
19- export type Theme = ' dark' | ' light' | ' system' ;
19+ export type Theme = " dark" | " light" | " system" ;
2020
2121type ThemeProviderProps = {
2222 children: React .ReactNode ;
@@ -30,16 +30,16 @@ type ThemeProviderState = {
3030};
3131
3232const initialState: ThemeProviderState = {
33- theme: ' system' ,
33+ theme: " system" ,
3434 setTheme : () => null ,
3535};
3636
3737const ThemeProviderContext = createContext <ThemeProviderState >(initialState );
3838
3939export function ThemeProvider({
4040 children ,
41- defaultTheme = ' system' ,
42- storageKey = ' my-ui-theme' ,
41+ defaultTheme = " system" ,
42+ storageKey = " my-ui-theme" ,
4343 ... props
4444}: ThemeProviderProps ) {
4545 const [theme, setTheme] = useState <Theme >(
@@ -49,13 +49,13 @@ export function ThemeProvider({
4949 useEffect (() => {
5050 const root = window .document .documentElement ;
5151
52- root .classList .remove (' light' , ' dark' );
52+ root .classList .remove (" light" , " dark" );
5353
54- if (theme === ' system' ) {
55- const systemTheme = window .matchMedia (' (prefers-color-scheme: dark)' )
54+ if (theme === " system" ) {
55+ const systemTheme = window .matchMedia (" (prefers-color-scheme: dark)" )
5656 .matches
57- ? ' dark'
58- : ' light' ;
57+ ? " dark"
58+ : " light" ;
5959
6060 root .classList .add (systemTheme );
6161 root .style .colorScheme = systemTheme ;
@@ -85,7 +85,7 @@ export const useTheme = () => {
8585 const context = useContext (ThemeProviderContext );
8686
8787 if (context === undefined )
88- throw new Error (' useTheme must be used within a ThemeProvider' );
88+ throw new Error (" useTheme must be used within a ThemeProvider" );
8989
9090 return context ;
9191};
@@ -101,10 +101,10 @@ export const useTheme = () => {
101101``` tsx
102102// 📄 components/ToasterProvider.tsx
103103
104- import type { ToasterProperties } from ' @pheralb/toast' ;
105- import { Toaster } from ' @pheralb/toast' ;
104+ import type { ToasterProperties } from " @pheralb/toast" ;
105+ import { Toaster } from " @pheralb/toast" ;
106106
107- import { useTheme } from ' ./themeProvider' ;
107+ import { useTheme } from " ./themeProvider" ;
108108
109109const ToasterProvider = (props : ToasterProperties ) => {
110110 const { theme } = useTheme ();
@@ -116,12 +116,12 @@ export default ToasterProvider;
116116
1171172 . Import the ` ToasterProvider ` component in the ` main.tsx ` file :
118118
119- ``` tsx
119+ ``` tsx {3, 9}
120120// 📄 main.tsx
121121
122- import ToasterProvider from ' ./providers/toasterProvider' ;
122+ import ToasterProvider from " ./providers/toasterProvider" ;
123123
124- ReactDOM .createRoot (document .getElementById (' root' ) as HTMLElement ).render (
124+ ReactDOM .createRoot (document .getElementById (" root" ) as HTMLElement ).render (
125125 <React.StrictMode >
126126 <ThemeProvider defaultTheme = " system" storageKey = " my-ui-theme" >
127127 <App />
@@ -143,31 +143,31 @@ ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
143143``` tsx
144144// 📄 components/ToasterNextTheme.tsx
145145
146- ' use client' ;
146+ " use client" ;
147147
148148import {
149149 Toaster ,
150150 type ToastTheme ,
151151 type ToasterProperties ,
152- } from ' @pheralb/toast' ;
152+ } from " @pheralb/toast" ;
153153
154- import { useTheme } from ' next-themes' ;
154+ import { useTheme } from " next-themes" ;
155155
156156const ToasterNextTheme = (props : ToasterProperties ) => {
157157 const { theme } = useTheme ();
158- return (
159- <Toaster toastFont = " font-sans" theme = { theme as ToastTheme } { ... props } />
160- );
158+ return <Toaster theme = { theme as ToastTheme } { ... props } />;
161159};
162160
163161export default ToasterNextTheme ;
164162```
165163
1661642 . Import the ` ToasterNextTheme ` component in the ` layout/app.tsx ` file :
167165
168- ``` tsx
166+ ``` tsx {3, 20}
169167// 📄 layout/app.tsx
170168
169+ import ToasterNextTheme from " @/components/ToasterNextTheme" ;
170+
171171export default function RootLayout({
172172 children ,
173173}: Readonly <{
@@ -197,7 +197,7 @@ export default function RootLayout({
197197
198198Import the ` useTheme ` hook from ` remix-themes ` and change the theme value :
199199
200- ``` tsx
200+ ``` tsx {4, 19}
201201// 📄 app/root.tsx
202202
203203import clsx from ' clsx' ;
0 commit comments