Skip to content

Commit

Permalink
perf: inhibit flickering during rendering on dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
atsuyaw committed Nov 14, 2023
1 parent 66a6d7c commit cfd0fe8
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions v4/layouts/partials/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
{{ else }}
<header>
{{ end }}
<script>
const lightDarkToggle = document.getElementById("light-dark-toggle");
const lightThemeCss = document.getElementById("light-theme-css");
const darkThemeCss = document.getElementById("dark-theme-css");
const darkStore = localStorage.getItem("dark-store");
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");

if (darkStore === "dark") {
setThemeMode("dark");
} else if (darkStore === "light") {
setThemeMode("light");
} else if (prefersDark.matches) {
setThemeMode("dark");
}

function setThemeMode(mode) {
if (mode === "dark") {
lightThemeCss.disabled = true;
darkThemeCss.disabled = false;
lightDarkToggle.className = "fa fa-moon";
localStorage.setItem("dark-store", mode);
} else {
lightThemeCss.disabled = false;
darkThemeCss.disabled = true;
lightDarkToggle.className = "fa fa-sun";
localStorage.setItem("dark-store", mode);
}
}
</script>
<a id="back-to-top-button">
<i class="fas fa-angle-up"></i>
</a>
Expand Down

0 comments on commit cfd0fe8

Please sign in to comment.