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

Feature/automatic dark mode #76

Merged

Conversation

edsonboldrini
Copy link
Contributor

@edsonboldrini edsonboldrini commented Feb 7, 2024

Closes #73

It uses the prefers-color-scheme config to determine what will be the first theme to save in the localStorage when there is no config. After saving it for the first time, it will always use the localStorage setting.

Needed to delete the previous package-lock because it doesn't work in my machine, too many postCSS errors. After deleting and reinstalling it worked.

Sorry for formatting the HTML.

Don't know why/if the script code at index.html which handles thheme is required, anyway, didn't remove it even not necessary in my dev environment. This one:

  <script>
    const localStorageTheme = localStorage.getItem("theme")
    const isBrowserDark = window && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
    const isDarkMode = !!localStorageTheme ? localStorageTheme === 'dark' : isBrowserDark

    if (isDarkMode) {
      document.documentElement.classList.add("dark");
      localStorage.setItem("theme", "dark");
      return;
    }
    document.documentElement.classList.remove("dark");
    localStorage.setItem("theme", "light");
    return;
  </script>

Copy link

vercel bot commented Feb 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
phived ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 7, 2024 11:06am

Copy link
Owner

@LukeberryPi LukeberryPi left a comment

Choose a reason for hiding this comment

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

lgtm

@LukeberryPi LukeberryPi merged commit af2ed57 into LukeberryPi:main Feb 7, 2024
2 checks passed
@LukeberryPi
Copy link
Owner

script code is to avoid FOUC (flash of unstyled content), so i never mess with it.

your PR is great and solves the issue, merged!

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.

QOL: Use darkMode: media for automatic theme detection
2 participants