Dark mode flashes in Next.js even with noflash.js
See original GitHub issueThe dark mode on my Next.js app still flashes even with adding noflash.js. I have made a dark mode component like so:
import useDarkMode from "use-dark-mode";
import { FiMoon } from "react-icons/fi";
const DarkModeToggle = () => {
const darkMode = useDarkMode(false);
return (
<div className="custom-control custom-switch">
<input
type="checkbox"
className="custom-control-input"
id="darkmodeSwitch"
onChange={darkMode.toggle}
checked={darkMode.value}
onClick={darkMode.disable}
/>
<label className="custom-control-label" htmlFor="darkmodeSwitch">
<FiMoon />
</label>
</div>
);
};
export default DarkModeToggle;
Added noflash.js
to the public folder.
Then on _document.js
import Document, { Html, Head, Main, NextScript } from "next/document";
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx);
return { ...initialProps };
}
render() {
return (
<Html lang="en">
<Head />
<body>
<script src="noflash.js" />
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;
In my SCSS folder I created a simple _dark-mode.scss
and added styles for dark mode in there e.g.:
body.light-mode {
transition: background-color 0.3s ease;
}
body.dark-mode {
background-color: #121212;
color: #f5f5f5;
transition: background-color 0.3s ease;
img {
opacity: 0.8;
}
}
As you can see it flashes and there’s also the toggle/switch that is not persistent.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9
Top Results From Across the Web
How to fix dark mode background color flicker in NextJS?
If I ship the HTML with class="dark" , the same problem occurs but in reverse: then light mode users will experience a flash...
Read more >Fixing the dark mode flash issue on server rendered websites
Bringing a proper solution to dark mode flashing without an ugly hack.
Read more >Implement Dark/Light mode: How to fix the flicker of incorrect ...
This article assumes that the reader has basic knowledge of React Context and Next.js . I have tried to link to the docs...
Read more >How to get a flickerless persistent dark mode in your Next.js app
And if you still do that, then you will end up facing the dreaded Dark Mode Flicker. Meaning your page will always flicker...
Read more >Dark Mode in Next.js and Tailwind CSS - YouTube
Adding dark mode toggle in next. js and tailwind css with next-themes package.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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.
Looks like a nice solution, thanks for sharing!