You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// learn more at https://github.com/pacocoursey/next-themes#avoid-hydration-mismatch
81
+
if (!mode) {
107
82
returnnull;
108
83
}
109
84
@@ -123,10 +98,8 @@ function ModeSwitcher() {
123
98
}
124
99
```
125
100
126
-
</codeblock>
127
-
128
101
:::success
129
-
The mode will be `system` by default to follow the user's preference.
102
+
After React hydrates the tree, the mode is set to `system` to follow the user's preference.
130
103
:::
131
104
132
105
### Determining the system mode
@@ -190,7 +163,13 @@ Next, if you have a custom selector that is **not** `media`, add the `InitColorS
190
163
The `attribute` has to be the same as the one you set in the `colorSchemeSelector` property:
191
164
192
165
```js
193
-
<InitColorSchemeScript attribute=".mode-%s"/>
166
+
createTheme({
167
+
cssVariables: {
168
+
colorSchemeSelector:'class'
169
+
}
170
+
})
171
+
172
+
<InitColorSchemeScript attribute="class"/>
194
173
```
195
174
196
175
:::
@@ -204,17 +183,21 @@ import InitColorSchemeScript from '@mui/material/InitColorSchemeScript';
204
183
205
184
exportdefaultfunctionRootLayout(props) {
206
185
return (
207
-
<html lang="en">
186
+
<html lang="en" suppressHydrationWarning>
208
187
<body>
209
188
{/* must come before the <main> element */}
210
-
<InitColorSchemeScript attribute=".mode-%s"/>
211
-
<main>{props.children}</main>
189
+
<InitColorSchemeScript attribute="class"/>
190
+
<main>{children}</main>
212
191
</body>
213
192
</html>
214
193
);
215
194
}
216
195
```
217
196
197
+
:::warning
198
+
If you don't add `suppressHydrationWarning` to your `<html>` tag, you will see warnings about `"Extra attributes from the server"` because `InitColorSchemeScript` updates that element.
199
+
:::
200
+
218
201
### Next.js Pages Router
219
202
220
203
Add the following code to the custom [`pages/_document.js`](https://nextjs.org/docs/pages/building-your-application/routing/custom-document) file:
@@ -226,11 +209,11 @@ import InitColorSchemeScript from '@mui/material/InitColorSchemeScript';
226
209
exportdefaultclassMyDocumentextendsDocument {
227
210
render() {
228
211
return (
229
-
<Html data-color-scheme="light">
212
+
<Html>
230
213
<Head>...</Head>
231
214
<body>
232
215
{/* must come before the <Main> element */}
233
-
<InitColorSchemeScript attribute=".mode-%s"/>
216
+
<InitColorSchemeScript attribute="class"/>
234
217
<Main />
235
218
<NextScript />
236
219
</body>
@@ -249,7 +232,7 @@ import * as React from 'react';
@@ -289,12 +272,12 @@ In the example below, all the components inside the `div` will always be dark:
289
272
290
273
## Disabling CSS color scheme
291
274
292
-
By default, the `extendTheme` attach [CSS color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme) based on the palette mode. If you want to disable it, set `disableCssColorScheme` to `true`:
275
+
By default, `createTheme` attaches a [CSS `color-scheme` property](https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme) based on the palette mode.
276
+
You can disable this by setting `disableCssColorScheme` to `true`:
293
277
294
278
```js
295
-
extendTheme({
296
-
colorSchemes: { light:true, dark:true },
297
-
disableCssColorScheme:true,
279
+
createTheme({
280
+
cssVariables: { disableCssColorScheme:true },
298
281
});
299
282
```
300
283
@@ -312,8 +295,10 @@ The generated CSS will not include the `color-scheme` property:
312
295
313
296
## Instant transition between color schemes
314
297
315
-
To disable CSS transition when switching between modes, use`disableTransitionOnChange` prop:
298
+
To disable CSS transitions when switching between modes, apply the`disableTransitionOnChange` prop:
0 commit comments