1
1
---
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"
5
5
---
6
6
7
7
> 💡 If you need to activate the dark mode directly, you can use the ` theme ` property: [ ` show ` ] ( /toaster#theme )
@@ -14,9 +14,9 @@ category: 'Customization'
14
14
- Snippet from [ ` shadcn/ui ` Dark Mode documentation] ( https://ui.shadcn.com/docs/dark-mode/vite#dark-mode ) .
15
15
16
16
``` tsx
17
- import { createContext , useContext , useEffect , useState } from ' react' ;
17
+ import { createContext , useContext , useEffect , useState } from " react" ;
18
18
19
- export type Theme = ' dark' | ' light' | ' system' ;
19
+ export type Theme = " dark" | " light" | " system" ;
20
20
21
21
type ThemeProviderProps = {
22
22
children: React .ReactNode ;
@@ -30,16 +30,16 @@ type ThemeProviderState = {
30
30
};
31
31
32
32
const initialState: ThemeProviderState = {
33
- theme: ' system' ,
33
+ theme: " system" ,
34
34
setTheme : () => null ,
35
35
};
36
36
37
37
const ThemeProviderContext = createContext <ThemeProviderState >(initialState );
38
38
39
39
export function ThemeProvider({
40
40
children ,
41
- defaultTheme = ' system' ,
42
- storageKey = ' my-ui-theme' ,
41
+ defaultTheme = " system" ,
42
+ storageKey = " my-ui-theme" ,
43
43
... props
44
44
}: ThemeProviderProps ) {
45
45
const [theme, setTheme] = useState <Theme >(
@@ -49,13 +49,13 @@ export function ThemeProvider({
49
49
useEffect (() => {
50
50
const root = window .document .documentElement ;
51
51
52
- root .classList .remove (' light' , ' dark' );
52
+ root .classList .remove (" light" , " dark" );
53
53
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)" )
56
56
.matches
57
- ? ' dark'
58
- : ' light' ;
57
+ ? " dark"
58
+ : " light" ;
59
59
60
60
root .classList .add (systemTheme );
61
61
root .style .colorScheme = systemTheme ;
@@ -85,7 +85,7 @@ export const useTheme = () => {
85
85
const context = useContext (ThemeProviderContext );
86
86
87
87
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" );
89
89
90
90
return context ;
91
91
};
@@ -101,10 +101,10 @@ export const useTheme = () => {
101
101
``` tsx
102
102
// 📄 components/ToasterProvider.tsx
103
103
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" ;
106
106
107
- import { useTheme } from ' ./themeProvider' ;
107
+ import { useTheme } from " ./themeProvider" ;
108
108
109
109
const ToasterProvider = (props : ToasterProperties ) => {
110
110
const { theme } = useTheme ();
@@ -116,12 +116,12 @@ export default ToasterProvider;
116
116
117
117
2 . Import the ` ToasterProvider ` component in the ` main.tsx ` file :
118
118
119
- ``` tsx
119
+ ``` tsx {3, 9}
120
120
// 📄 main.tsx
121
121
122
- import ToasterProvider from ' ./providers/toasterProvider' ;
122
+ import ToasterProvider from " ./providers/toasterProvider" ;
123
123
124
- ReactDOM .createRoot (document .getElementById (' root' ) as HTMLElement ).render (
124
+ ReactDOM .createRoot (document .getElementById (" root" ) as HTMLElement ).render (
125
125
<React.StrictMode >
126
126
<ThemeProvider defaultTheme = " system" storageKey = " my-ui-theme" >
127
127
<App />
@@ -143,31 +143,31 @@ ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
143
143
``` tsx
144
144
// 📄 components/ToasterNextTheme.tsx
145
145
146
- ' use client' ;
146
+ " use client" ;
147
147
148
148
import {
149
149
Toaster ,
150
150
type ToastTheme ,
151
151
type ToasterProperties ,
152
- } from ' @pheralb/toast' ;
152
+ } from " @pheralb/toast" ;
153
153
154
- import { useTheme } from ' next-themes' ;
154
+ import { useTheme } from " next-themes" ;
155
155
156
156
const ToasterNextTheme = (props : ToasterProperties ) => {
157
157
const { theme } = useTheme ();
158
- return (
159
- <Toaster toastFont = " font-sans" theme = { theme as ToastTheme } { ... props } />
160
- );
158
+ return <Toaster theme = { theme as ToastTheme } { ... props } />;
161
159
};
162
160
163
161
export default ToasterNextTheme ;
164
162
```
165
163
166
164
2 . Import the ` ToasterNextTheme ` component in the ` layout/app.tsx ` file :
167
165
168
- ``` tsx
166
+ ``` tsx {3, 20}
169
167
// 📄 layout/app.tsx
170
168
169
+ import ToasterNextTheme from " @/components/ToasterNextTheme" ;
170
+
171
171
export default function RootLayout({
172
172
children ,
173
173
}: Readonly <{
@@ -197,7 +197,7 @@ export default function RootLayout({
197
197
198
198
Import the ` useTheme ` hook from ` remix-themes ` and change the theme value :
199
199
200
- ``` tsx
200
+ ``` tsx {4, 19}
201
201
// 📄 app/root.tsx
202
202
203
203
import clsx from ' clsx' ;
0 commit comments