question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dark mode flashes in Next.js even with noflash.js

See original GitHub issue

The 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;
  }
}

dark_mode_flash As you can see it flashes and there’s also the toggle/switch that is not persistent.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
djD-REKcommented, Jul 1, 2021

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.

1reaction
djD-REKcommented, Jul 2, 2021

Looks like a nice solution, thanks for sharing!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found