next-themes icon indicating copy to clipboard operation
next-themes copied to clipboard

Still flashing in Nextjs 13, Tailwind, DaisyUI

Open sivert-io opened this issue 1 year ago • 9 comments

Hey, my site is flashing when refreshing.

I know this is a duplicate issue but I will still post the issue as its an important issue to fix. The readme states the flashing is fixed, but in my case (and in many issues posted) it is not.

My website is: not relevant My repo is: not relevant

I am using DaisyUI to add styling.

I am implementing ThemeProvider in _app.tsx like so:

<ThemeProvider value={{ light: 'cmyk', dark: 'dracula' }}>
  ...
  <Component {...pageProps} key={router.route} />
</ThemeProvider>

Body and HTML are not styled manually, they receive their styling from Tailwind css.

What am I doing wrong?

I appreciate any help 😄

sivert-io avatar Mar 03 '23 12:03 sivert-io

I've performed some more tests, it seems the issue only persists when a theme has been set in the localStorage. If you delete the localStorage entry, the site doesn't flash anymore.

sivert-io avatar Mar 03 '23 14:03 sivert-io

@SivertGullbergHansen i have same issue, can u show me where did you execute deletion of localeStorage? Thank you in advance

ImraKocis avatar Apr 18 '23 10:04 ImraKocis

@SivertGullbergHansen i have same issue, can u show me where did you execute deletion of localeStorage? Thank you in advance

Sorry if it was not clear, I meant when I enter developer tools in my browser and manually delete the theme entry in LocalStorage, it prevents the site from flashing. So I'm not doing anything with the code 😕 Its not a solution, just an observation 😊

sivert-io avatar Apr 18 '23 10:04 sivert-io

I don't know it's solved or no, but I have the same issue. I am just trying to implement a dark mode to a starter project. But it flashes on refresh. I am Next.js 13.4.9

sellimenes avatar Jul 09 '23 20:07 sellimenes

same situation for me

Jean-Juel avatar Jul 10 '23 15:07 Jean-Juel

Still flashing on next 13.4.12 (appdir) with next-themes 0.2.1 + tailwindcss 3.3.3

stepbrobd avatar Jul 22 '23 05:07 stepbrobd

For those who are still having the flashing problem, check out https://github.com/pacocoursey/next-themes/pull/156, disabling Cloudflare Rocket Loader solved the problem for me.

stepbrobd avatar Sep 09 '23 15:09 stepbrobd

In our case (Next + DaisyUI custom theme) the same problem was occurring a workaround that fixed this was using "light" nd "dark" as custom daisUI themes names instead of using custom names:

Before :

// tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  // ...
  darkTheme: "scaffoldETHDark",
  // DaisyUI theme colors
  daisyui: {
    themes: [{ scaffoldETH: {...} }, { scaffoldETHDark: {...} }],
  },
};

After :

// tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  // ...
  darkTheme: "dark",
  // DaisyUI theme colors
  daisyui: {
    themes: [{ light: {...} }, { dark: {...} }],
  },
};

And after this removing the custom value prop passed to <ThemeProvider>

So my wild guess is this problem occurs if you pass value prop to <ThemeProvider>

technophile-04 avatar Feb 09 '24 13:02 technophile-04

@technophile-04 Thx i solved this problem.

I avoided direct matching with ThemeProvider like value={{ light: 'cmyk', dark: 'dracula' }}

and separate hook like useDarkMode and matched the theme from there.

jwu-ice avatar Apr 22 '24 04:04 jwu-ice