Skip to content
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

Add dark mode toggle. #135

Closed
wants to merge 2 commits into from
Closed

Add dark mode toggle. #135

wants to merge 2 commits into from

Conversation

leerob
Copy link
Owner

@leerob leerob commented Dec 30, 2019

No description provided.

@vercel
Copy link

vercel bot commented Dec 30, 2019

This pull request is being automatically deployed with ZEIT Now (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://zeit.co/leerob/leerob/j1pjvokhd
✅ Preview: https://leerob-git-use-dark-mode-toggle.leerob1.now.sh

@quantizor
Copy link

Looked into this and it appears to be an issue with differing content during rehydration. useDarkMode(false)'s returned ref has a value of false on the server and true on the client.

This code fixed it:

const DarkMode = ({children}) => {
    const darkMode = useDarkMode(false);
    const [theme, setTheme] = useState(lightTheme);

    useEffect(() => {
        if (darkMode.value) setTheme(darkTheme);
    }, [darkMode, darkMode.value]);

    return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
};

Note that React will automatically fix up differing element markup, but not different HTML attribute markup.

@leerob
Copy link
Owner Author

leerob commented Jan 3, 2020

Huh, interesting. Thanks for looking into this @probablyup!

cc: @brianlovin

@brianlovin
Copy link
Contributor

That did fix it, but you want one small tweak:

React.useEffect(() => {
    setTheme(darkMode.value ? darkTheme : lightTheme);
  }, [darkMode, darkMode.value]);

Previously it would respond to changes from light -> dark, but not the reverse. This fixes it so that if your user is looking at your site and switches to light mode (manually or automatically) the site will use the correct theme.

@brianlovin
Copy link
Contributor

Unfortunately, this still causes a flash from light -> dark if your OS is set to dark mode. I think this still requires hiding the content with a visibility:hidden wrapping div to prevent a flash.

@quantizor
Copy link

quantizor commented Jan 6, 2020 via email

@leerob
Copy link
Owner Author

leerob commented Mar 26, 2020

While redesigning my site to use Chakra UI (which has use-dark-mode under the hood), I was still hitting the same issue.

Ultimately decided to just default to light mode always, but still have a toggle for dark if they want it. Good enough solution for now, since I don't really want to with CSS variables or global styles.

#159

@leerob leerob closed this Mar 26, 2020
@leerob leerob deleted the use-dark-mode-toggle branch March 26, 2020 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants