Skip to content

Commit

Permalink
feat: store dark mode preference
Browse files Browse the repository at this point in the history
  • Loading branch information
atsuyaw committed Nov 11, 2023
1 parent 47fd080 commit 96d2780
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions v4/assets/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,20 @@ $(document).ready(function () {
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)");

// Detect preference in last visit
if (darkStore === "dark") {
setThemeMode("dark");
} else if (darkStore === "light") {
setThemeMode("light");
} else if (prefersDark.matches) {
setThemeMode("dark");
}

lightDarkToggle.addEventListener("click", () => {
if (lightDarkToggle.className === "fa fa-sun") {
if (lightDarkToggle.className === "fa fa-moon") {
setThemeMode("light");
} else {
setThemeMode("dark");
Expand All @@ -113,21 +123,13 @@ $(document).ready(function () {
if (mode === "dark") {
lightThemeCss.disabled = true;
darkThemeCss.disabled = false;
lightDarkToggle.className = "fa fa-sun";
lightDarkToggle.className = "fa fa-moon";
localStorage.setItem("dark-store", mode);
} else {
lightThemeCss.disabled = false;
darkThemeCss.disabled = true;
lightDarkToggle.className = "fa fa-moon";
lightDarkToggle.className = "fa fa-sun";
localStorage.setItem("dark-store", mode);
}
}

const themeModeListener = (event) => {
if (event.matches) {
setThemeMode("dark");
} else {
setThemeMode("light");
}
};

themeModeListener(prefersDark);
});

0 comments on commit 96d2780

Please sign in to comment.