🐛 Stop site appearance taking priority over user

Fixes #102
pull/105/head
James Panther 2022-02-05 19:19:47 +11:00
parent 91b5b52c21
commit 09d2e410c8
No known key found for this signature in database
GPG Key ID: D36F789E45745D17
3 changed files with 24 additions and 15 deletions

View File

@ -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)) - 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 ## [2.0.1] - 2022-02-03
### Fixed ### Fixed
@ -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 - ⚠️ All theme images are now Hugo assets
- ⚠️ Overhauled `figure` shortcode which now resizes images - ⚠️ Overhauled `figure` shortcode which now resizes images
- Upgrade to Tailwind v3.0.18 - Upgrade to Tailwind v3.0.18
- Inline Javascript moved to external files - Inline JavaScript moved to external files
- Improved JSON-LD structured data - Improved JSON-LD structured data
- Breadcrumbs now fallback to section name when `title` is not provided - Breadcrumbs now fallback to section name when `title` is not provided
- Minor style and layout improvements - Minor style and layout improvements

View File

@ -1,11 +1,14 @@
const browserIsDark = const browserIsDark =
window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches; window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
const sitePreference = document.documentElement.getAttribute("data-default-appearance");
const userPreference = localStorage.getItem("appearance"); const userPreference = localStorage.getItem("appearance");
const switcher = document.getElementById("appearance-switcher"); const switcher = document.getElementById("appearance-switcher");
if ( if (
(browserIsDark && userPreference === null) || (browserIsDark && userPreference === null) ||
(browserIsDark && userPreference === "dark") || (browserIsDark && userPreference === "dark") ||
(sitePreference === "dark" && userPreference === null) ||
(sitePreference === "dark" && userPreference === "dark") ||
userPreference === "dark" userPreference === "dark"
) { ) {
document.documentElement.classList.add("dark"); document.documentElement.classList.add("dark");
@ -22,15 +25,17 @@ if (document.documentElement.getAttribute("data-auto-appearance") === "true") {
} }
window.addEventListener("DOMContentLoaded", (event) => { window.addEventListener("DOMContentLoaded", (event) => {
switcher.addEventListener("click", () => { if (switcher) {
document.documentElement.classList.toggle("dark"); switcher.addEventListener("click", () => {
localStorage.setItem( document.documentElement.classList.toggle("dark");
"appearance", localStorage.setItem(
document.documentElement.classList.contains("dark") ? "dark" : "light" "appearance",
); document.documentElement.classList.contains("dark") ? "dark" : "light"
}); );
switcher.addEventListener("contextmenu", (event) => { });
event.preventDefault(); switcher.addEventListener("contextmenu", (event) => {
localStorage.removeItem("appearance"); event.preventDefault();
}); localStorage.removeItem("appearance");
});
}
}); });

View File

@ -8,9 +8,8 @@
{{- else -}} {{- else -}}
ltr ltr
{{- end }}" {{- end }}"
class="scroll-smooth {{ if eq (.Site.Params.defaultAppearance | default "light") "dark" -}} class="scroll-smooth"
dark data-default-appearance="{{ .Site.Params.defaultAppearance | default "light" }}"
{{- end }}"
data-auto-appearance="{{ .Site.Params.autoSwitchAppearance | default "true" }}" data-auto-appearance="{{ .Site.Params.autoSwitchAppearance | default "true" }}"
> >
{{- partial "head.html" . -}} {{- partial "head.html" . -}}