-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
This pull request is being automatically deployed with ZEIT Now (learn more). 🔍 Inspect: https://zeit.co/leerob/leerob/j1pjvokhd |
Looked into this and it appears to be an issue with differing content during rehydration. 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. |
Huh, interesting. Thanks for looking into this @probablyup! cc: @brianlovin |
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. |
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 |
Yeah. I think for this use case css variables and the dark mode media query
is probably the better solution since it takes effect prior to React mount
…On Mon, Jan 6, 2020 at 1:04 AM Brian Lovin ***@***.***> wrote:
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.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#135?email_source=notifications&email_token=AAELFVRSX7SK544PAS7OBA3Q4LCWVA5CNFSM4KBMR5RKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIEQJEY#issuecomment-571016339>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAELFVVW4IZJO22IC6ATNC3Q4LCWVANCNFSM4KBMR5RA>
.
|
While redesigning my site to use Chakra UI (which has 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. |
No description provided.