diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dbc9c5c..dbbc8504 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Changed - Upgrade to Tailwind v3.0.12 +- Inline Javascript moved to external files - Author images are now Hugo assets - Required Hugo version is now 0.87.0 or later - Minor style and layout improvements diff --git a/assets/js/dark.js b/assets/js/dark.js new file mode 100644 index 00000000..8f07fd32 --- /dev/null +++ b/assets/js/dark.js @@ -0,0 +1,54 @@ +function loadPreferredAppearance() { + if ( + localStorage.preferredAppearance === "dark" || + (!("preferredAppearance" in localStorage) && + window.matchMedia("(prefers-color-scheme: dark)").matches) + ) { + document.documentElement.classList.add("dark"); + } else { + document.documentElement.classList.remove("dark"); + } +} + +function setPreferredAppearance(scheme) { + if (scheme == "default") { + localStorage.removeItem("preferredAppearance"); + } else { + localStorage.preferredAppearance = scheme; + } + loadPreferredAppearance(); +} + +window.addEventListener("DOMContentLoaded", (event) => { + // Hook up toggle events + darkToggle = document.getElementById("dark-toggle"); + if (darkToggle) { + darkToggle.addEventListener("click", function (e) { + e.preventDefault(); + setPreferredAppearance("dark"); + }); + darkToggle.addEventListener("contextmenu", function (e) { + e.preventDefault(); + setPreferredAppearance("default"); + }); + } + lightToggle = document.getElementById("light-toggle"); + if (lightToggle) { + lightToggle.addEventListener("click", function (e) { + e.preventDefault(); + setPreferredAppearance("light"); + }); + lightToggle.addEventListener("contextmenu", function (e) { + e.preventDefault(); + setPreferredAppearance("default"); + }); + } + + // Load user's preset preference (if any) + loadPreferredAppearance(); + + // Allow browser media overrides + window + .matchMedia("(prefers-color-scheme: dark)") + .addEventListener("change", loadPreferredAppearance); +}); diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html index 256b42d8..924354bc 100644 --- a/layouts/partials/footer.html +++ b/layouts/partials/footer.html @@ -43,8 +43,6 @@