-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 fix: properly initialise
currentTheme
The `currentTheme` variable is now properly initialized when setting the initial theme, which fixes the "extra click" issue observed when system preference is `dark` and there's no saved theme in localStorage. Also minifies the themeSwitcher. Fixes #94
- Loading branch information
Showing
2 changed files
with
16 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,37 @@ | ||
// Get the theme switcher button element. | ||
const themeSwitcher = document.querySelector(".theme-switcher"); | ||
|
||
// Retrieve theme from localStorage. | ||
let currentTheme = localStorage.getItem("theme"); | ||
// Retrieve theme from either the localStorage or the data-theme attribute on the document element. | ||
let currentTheme = localStorage.getItem("theme") || document.documentElement.getAttribute('data-theme'); | ||
|
||
// Function to switch between dark and light themes. | ||
function setTheme(theme) { | ||
// Function to set theme | ||
function setTheme(theme, saveToLocalStorage = false) { | ||
document.documentElement.setAttribute("data-theme", theme); | ||
localStorage.setItem("theme", theme); | ||
currentTheme = theme; | ||
|
||
if (saveToLocalStorage) { | ||
localStorage.setItem("theme", theme); | ||
} | ||
|
||
// Dispatch a custom event for comment systems. | ||
// Dispatch a custom event for utterances and giscus. | ||
const event = new CustomEvent("themeChanged", { | ||
detail: { theme: theme } | ||
}); | ||
window.dispatchEvent(event); | ||
} | ||
|
||
// Function to switch between dark and light themes. | ||
function switchTheme() { | ||
// Set the new theme based on the current theme. | ||
const newTheme = currentTheme === "dark" ? "light" : "dark"; | ||
setTheme(newTheme); | ||
setTheme(newTheme, true); // Save the theme to localStorage when the user changes it. | ||
} | ||
|
||
// Initialize the theme switcher button. | ||
themeSwitcher.addEventListener("click", switchTheme, false); | ||
|
||
// Update the theme based on system preference if the user hasn't manually changed the theme. | ||
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", e => { | ||
if (!localStorage.getItem("theme")) { | ||
const newTheme = e.matches ? "dark" : "light"; | ||
setTheme(newTheme); | ||
} | ||
const newTheme = e.matches ? "dark" : "light"; | ||
setTheme(newTheme); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.