-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dark mode flashes in Next.js even with noflash.js #83
Comments
Hey there,
You might try the dark mode solution used in
https://jamstackthemes.dev/theme/nextjs-tailwind-starter-blog/ (i.e.
next-themes with dark mode as a theme). I was able to get the behavior I
wanted (autodetection of system theme, local storage of preference, etc.)
in this project https://github.com/DoctorDerek/steven-terner.github.io
https://steventerner.com using a useEffect() handler.
|
Hey, |
@djD-REK Thanks again for your recommendation, it works great with no flash! Do you have any idea how to apply a toggle to an input like:
The example of next-themes uses 2 buttons to do the switch. |
Hey @markdost yeah I got the behavior you want using a useState and useEffect combination if you check out the demo I sent you. It's a little convoluted because of the animation involved, but it should be pretty clear. Let me know if it's not 😄 |
@DoctorDerek Yes I got it working now in the end by doing:
|
Looks like a nice solution, thanks for sharing!
|
The problem with useEffect is that you lose SSR, causing SEO problems |
Hmm, that's a good point. I thought it's just a thematic thing, and SEO
only has to do with the content, so using useEffect after Next hydrates to
update the dark mode wouldn't affect it. And you're right, you can't have
pure server side rendering with it.
|
@okalil I see, so it would cause problems when doing SSR. Keep in mind I switched from this library to next-themes, which might still cause the same problem if doing SSR. The example I gave at #83 (comment) is with next-themes. |
The dark mode on my Next.js app still flashes even with adding noflash.js. I have made a dark mode component like so:
Added
noflash.js
to the public folder.Then on
_document.js
In my SCSS folder I created a simple
_dark-mode.scss
and added styles for dark mode in there e.g.:As you can see it flashes and there's also the toggle/switch that is not persistent.
The text was updated successfully, but these errors were encountered: