How to fix flash of theme-switching on page-reload? (Mantine v7.1.2 - NextJS)

562 views Asked by At

I'm using Mantine v7.1.2 with NextJS.I've already added this code to my _app.tsx, but the flash of dark-theme on page-refresh still persists. How do I fix this? Thank you so much in advance

<MantineProvider theme={defaultTheme} defaultColorScheme='light'>

4

There are 4 answers

0
mertk On

Mantine team provides templates for popular environments on their document. There are not enough details in your question but I can suggest you to check out the templates and compare with your project.

Templates listed here.

Next app route and Next page route, both of them available.

1
David Boskovic On

Was surprised to see this question downvoted. Mantine does indeed do a flash of default browser color-scheme after it injects the css but before the javascript adds the data-mantine-color-scheme="value" attribute to the html tag.

The workaround I used was to set data-mantine-color-scheme="light" (or dark or auto) manually on the <html ... tag to avoid the flash.

1
bdrangova On

If you are using the AppRouter adding defaultColorScheme to the <ColorSchemeScript /> works.

Like this:

<ColorSchemeScript defaultColorScheme="dark" />
0
Kishore Sundharam On

I faced the similar bug while developing my project. You have to set the colorsSchemeScript theme to "light", if you are using light theme. Just adding "light" to MantineProvider's defaultColorScheme will still flicker. It worked for me.

<Head>
   <ColorSchemeScript defaultColorScheme="light" />
</Head>