From 9e0aac33f4400c1b12188ce6e05c049b3154e5be Mon Sep 17 00:00:00 2001
From: Alexander Bilz <mail@alexbilz.com>
Date: Sun, 18 Apr 2021 12:58:28 +0000
Subject: [PATCH] Merge remote-tracking branch 'origin/master' into darkmode
---
assets/js/anatole-theme-switcher.js | 36 +++++++++++++++++++++++++++++++-----
1 files changed, 31 insertions(+), 5 deletions(-)
diff --git a/assets/js/anatole-theme-switcher.js b/assets/js/anatole-theme-switcher.js
index f7a584f..c31df10 100644
--- a/assets/js/anatole-theme-switcher.js
+++ b/assets/js/anatole-theme-switcher.js
@@ -8,26 +8,51 @@
localStorage.setItem('theme', style);
}
+// Check if a theme was set manually
+function getMode() {
+ return localStorage.getItem('mode') ? localStorage.getItem('mode') : null;
+}
+
+function setMode(mode) {
+ localStorage.setItem('mode', mode);
+}
+
function init() {
// initialize default value
const theme = getTheme();
+ const mode = getMode();
// check if a preferred color theme is set for users that have never been to our site
const userPrefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
+ const userPrefersLight = window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches;
if (theme === null) {
if (userPrefersDark) {
setTheme('dark');
+ setMode('auto');
+ } else if (userPrefersLight){
+ setTheme('light');
+ setMode('auto');
} else if (!document.documentElement.getAttribute('data-theme')) {
setTheme('light');
} else {
setTheme(document.documentElement.getAttribute('data-theme'));
}
} else {
- // load a stored theme
- if (theme === 'light') {
- document.documentElement.setAttribute('data-theme', 'light');
- } else {
- document.documentElement.setAttribute('data-theme', 'dark');
+
+ if (mode === 'auto') {
+ if (userPrefersDark) {
+ document.documentElement.setAttribute('data-theme', 'dark');
+ } else if (userPrefersLight){
+ document.documentElement.setAttribute('data-theme', 'light');
+ }
+ }
+ else {
+ // load a user set theme theme
+ if (theme === 'light') {
+ document.documentElement.setAttribute('data-theme', 'light');
+ } else {
+ document.documentElement.setAttribute('data-theme', 'dark');
+ }
}
}
}
@@ -41,6 +66,7 @@
} else {
setTheme('light');
}
+ setMode('user');
}
// Manual Switch
--
Gitblit v1.10.0