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

Follow system theme #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ lang: en-US # default lang
timezone: Asia/Jakarta # set your timezone
image: assets/img/ogp.png # This image used for Open Graph more info https://ogp.me/
repo: https://github.com/piharpi/jekyll-klise # site repo [optional]
mode: dark # default theme "dark" | "light"
mode: dark # default theme "dark" | "light" | "system" (follow system theme)

# Profile settings
author:
Expand Down
24 changes: 21 additions & 3 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Theme switch
const body = document.body;
const lamp = document.getElementById("mode");
const themeMode = localStorage.getItem("theme");
const systemDarkModePreference = window.matchMedia("(prefers-color-scheme: dark)");

const toggleTheme = (state) => {
if (state === "dark") {
Expand All @@ -15,9 +17,25 @@
}
};

lamp.addEventListener("click", () =>
toggleTheme(localStorage.getItem("theme"))
);
const systemThemeToToggle = (e) => {
if (e.matches) {
toggleTheme("light")
} else {
toggleTheme("dark")
}
}

if (themeMode !== "system") {
lamp.addEventListener("click", () =>
toggleTheme(localStorage.getItem("theme"))
);
} else {
lamp.remove();
systemThemeToToggle(systemDarkModePreference);
systemDarkModePreference.addEventListener("change", e => {
systemThemeToToggle(e);
});
}

// Blur the content when the menu is open
const cbox = document.getElementById("menu-trigger");
Expand Down