summaryrefslogtreecommitdiffstats
path: root/docs/theme/ThemeSwitch.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-09-03 07:47:33 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-09-03 07:47:33 +0000
commit39bf3544d4064a528a85c21a9069a9cae364a6c1 (patch)
treeaa318bfe1e7816ba9492aff0cdd2454d278f5836 /docs/theme/ThemeSwitch.js
parentAdding upstream version 0.45+dfsg. (diff)
downloadjinjax-39bf3544d4064a528a85c21a9069a9cae364a6c1.tar.xz
jinjax-39bf3544d4064a528a85c21a9069a9cae364a6c1.zip
Adding upstream version 0.46.upstream/0.46
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'docs/theme/ThemeSwitch.js')
-rw-r--r--docs/theme/ThemeSwitch.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/docs/theme/ThemeSwitch.js b/docs/theme/ThemeSwitch.js
new file mode 100644
index 0000000..c8b2847
--- /dev/null
+++ b/docs/theme/ThemeSwitch.js
@@ -0,0 +1,41 @@
+import { on } from "./jxui.js";
+
+const SEL_TARGET = ".cd-theme-switch";
+const STORAGE_KEY = "theme";
+
+const DARK = "dark";
+const LIGHT = "light";
+const theme = {value: getColorPreference()};
+
+reflectPreference();
+on("click", SEL_TARGET, onClick);
+// sync with system changes
+window
+ .matchMedia("(prefers-color-scheme: dark)")
+ .addEventListener("change", ({matches:isDark}) => {
+ theme.value = isDark ? DARK : LIGHT
+ setPreference()
+ });
+
+function onClick (event, target) {
+ if (target.matches("[disabled]")) return;
+ theme.value = theme.value === LIGHT ? DARK : LIGHT;
+ setPreference();
+}
+function setPreference () {
+ localStorage.setItem(STORAGE_KEY, theme.value);
+ reflectPreference();
+}
+function reflectPreference () {
+ const value = getColorPreference ();
+ if (value === DARK) {
+ document.documentElement.classList.add(DARK);
+ document.documentElement.classList.remove(LIGHT);
+ } else {
+ document.documentElement.classList.add(LIGHT);
+ document.documentElement.classList.remove(DARK);
+ }
+}
+function getColorPreference () {
+ return localStorage.getItem(STORAGE_KEY);
+} \ No newline at end of file