From a05a5b2704eec66e84c4fc3045127e354cf38e1a Mon Sep 17 00:00:00 2001 From: Ole Mussmann Date: Thu, 20 Feb 2025 20:59:17 +0100 Subject: move inline JS to its own file --- static/js/theme-switch.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 static/js/theme-switch.js (limited to 'static/js/theme-switch.js') diff --git a/static/js/theme-switch.js b/static/js/theme-switch.js new file mode 100644 index 0000000..a617fda --- /dev/null +++ b/static/js/theme-switch.js @@ -0,0 +1,28 @@ +function isAuto() { + return document.body.classList.contains("auto"); +} + +function setTheme() { + if (!isAuto()) { + return + } + + document.body.classList.remove("auto"); + let cls = "light"; + if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { + cls = "dark"; + } + + document.body.classList.add(cls); +} + +function invertBody() { + document.body.classList.toggle("dark"); + document.body.classList.toggle("light"); +} + +if (isAuto()) { + window.matchMedia('(prefers-color-scheme: dark)').addListener(invertBody); +} + +setTheme(); -- cgit v1.2.3