fix detection of prefers colors

This commit is contained in:
AngelJumbo
2024-06-07 20:17:34 -05:00
parent 4d63eea921
commit 7c7c9f9fc8
2 changed files with 7 additions and 18 deletions
+1 -16
View File
@@ -36,7 +36,6 @@ function activeSection(section) {
function hideAll() {
document.querySelectorAll(".section").forEach((s) => {
s.classList.remove("selected");
// s.style.display = "none";
});
}
const HIDDEN_CLASS = "hidden";
@@ -50,26 +49,12 @@ function hideThemeSwitcher(currentTheme) {
document.getElementById("dark-icon")?.classList.remove(HIDDEN_CLASS);
}
}
function getCurrentTheme() {
const fromProperty = document.documentElement.style.getPropertyValue("color-scheme");
if (fromProperty) return fromProperty;
const prefersTheme = window.matchMedia("(prefers-color-scheme: dark)") ? "dark" : "light";
return prefersTheme;
}
function setTheme(newTheme) {
document.documentElement.style.setProperty("color-scheme", newTheme);
hideThemeSwitcher(newTheme);
}
function switchTheme() {
let currentTheme = getCurrentTheme();
console.debug("Start theme switching to " + currentTheme + " theme");
setTheme(currentTheme == "dark" ? "light" : "dark");
setTheme(document.documentElement.style.getPropertyValue("color-scheme")=="dark"?"light":"dark");
}
function initColorTheme() {
const currentTheme = getCurrentTheme();
console.debug("Current theme is " + currentTheme + "");
setTheme(currentTheme);
}
+6 -2
View File
@@ -113,14 +113,18 @@ done
echo "</main>" >> ./index.html
echo "<script>
window.onload = () => {" >> ./index.html
echo "hideAll();" >> ./index.html
echo "initColorTheme();" >> ./index.html
#echo "hideAll();" >> ./index.html
for section in "${sections[@]}"; do
echo " loadPage('$section', 1);" >> ./index.html
done
echo "
activeSection('${sections[0]}');
if (window.matchMedia('(prefers-color-scheme: dark)').matches){
setTheme('dark');
}else{
setTheme('light');
}
}
</script>" >> ./index.html