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

Remove react hook and use javascript to detect theme state #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

msoham123
Copy link
Member

This fixes the initial flash that can be seen when opening the page and using a system default of dark mode.

In the future, I'd like to add local storage based dark mode handling, so this opens that up as well.


const IndexPage = () => {

configureDarkMode();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just thinking not tried but is it working when we go to another page? It looks like it is working on index page but what happens if we go to download page? Is it still works because it looks like you are just adding dark class on index page

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I didn't even think about that. It seems to maintain state when navigating to the download page, which makes sense, but then upon reload defaults to light. I added the method to all pages and now it works fine, no flash and respects system theme, for all of them (even upon changing in system settings and navigating to the page.)

import "react";

const colorSchemeMedia = "(prefers-color-scheme: dark)";
const isBrowser = () => typeof window !== "undefined"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract this into some utility file and also use it in CustomLink/Button.

const isBrowser = () => typeof window !== "undefined"

const configureDarkMode = () => {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space.

Comment on lines 21 to 22

// TODO: Add a switch that respects local storage and allows user to choose
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If something is fully functional in itself then there's no need to add TODOs.


const IndexPage = () => {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space.

const Layout = ({ children, title }) => {
const isDark = useDarkModeState();

const Layout = ({ children, title}) => {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space.

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.

3 participants