Skip to content

Commit

Permalink
🐛 Stop site appearance taking priority over user
Browse files Browse the repository at this point in the history
Fixes #102
  • Loading branch information
jpanther committed Feb 5, 2022
1 parent 91b5b52 commit 09d2e41
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- Updated French translation ([#100](https://github.com/jpanther/congo/pull/100))

### Fixed

- User's appearance preference is lost on page load when default appearance is dark ([#102](https://github.com/jpanther/congo/issues/102))
- JavaScript warning when appearance switcher is disabled

## [2.0.1] - 2022-02-03

### Fixed
Expand Down Expand Up @@ -42,7 +47,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- ⚠️ All theme images are now Hugo assets
- ⚠️ Overhauled `figure` shortcode which now resizes images
- Upgrade to Tailwind v3.0.18
- Inline Javascript moved to external files
- Inline JavaScript moved to external files
- Improved JSON-LD structured data
- Breadcrumbs now fallback to section name when `title` is not provided
- Minor style and layout improvements
Expand Down
27 changes: 16 additions & 11 deletions assets/js/appearance.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const browserIsDark =
window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
const sitePreference = document.documentElement.getAttribute("data-default-appearance");
const userPreference = localStorage.getItem("appearance");
const switcher = document.getElementById("appearance-switcher");

if (
(browserIsDark && userPreference === null) ||
(browserIsDark && userPreference === "dark") ||
(sitePreference === "dark" && userPreference === null) ||
(sitePreference === "dark" && userPreference === "dark") ||
userPreference === "dark"
) {
document.documentElement.classList.add("dark");
Expand All @@ -22,15 +25,17 @@ if (document.documentElement.getAttribute("data-auto-appearance") === "true") {
}

window.addEventListener("DOMContentLoaded", (event) => {
switcher.addEventListener("click", () => {
document.documentElement.classList.toggle("dark");
localStorage.setItem(
"appearance",
document.documentElement.classList.contains("dark") ? "dark" : "light"
);
});
switcher.addEventListener("contextmenu", (event) => {
event.preventDefault();
localStorage.removeItem("appearance");
});
if (switcher) {
switcher.addEventListener("click", () => {
document.documentElement.classList.toggle("dark");
localStorage.setItem(
"appearance",
document.documentElement.classList.contains("dark") ? "dark" : "light"
);
});
switcher.addEventListener("contextmenu", (event) => {
event.preventDefault();
localStorage.removeItem("appearance");
});
}
});
5 changes: 2 additions & 3 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
{{- else -}}
ltr
{{- end }}"
class="scroll-smooth {{ if eq (.Site.Params.defaultAppearance | default "light") "dark" -}}
dark
{{- end }}"
class="scroll-smooth"
data-default-appearance="{{ .Site.Params.defaultAppearance | default "light" }}"
data-auto-appearance="{{ .Site.Params.autoSwitchAppearance | default "true" }}"
>
{{- partial "head.html" . -}}
Expand Down

0 comments on commit 09d2e41

Please sign in to comment.