From 2bf1bd8e137be9a59ad9052bc087de724a7e9077 Mon Sep 17 00:00:00 2001 From: Peter Molnar Date: Thu, 15 Nov 2018 13:35:47 +0000 Subject: [PATCH] themeswitcher v1 --- templates/themeswitcher.js | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 templates/themeswitcher.js diff --git a/templates/themeswitcher.js b/templates/themeswitcher.js new file mode 100644 index 0000000..18291a3 --- /dev/null +++ b/templates/themeswitcher.js @@ -0,0 +1,47 @@ +var DEFAULT_THEME = 'dark'; +var ALT_THEME = 'light'; + +function setTheme(mode) { + var st = document.querySelector('#css_alt'); + var cb = document.querySelector('#contrast'); + if (mode == DEFAULT_THEME) { + st.setAttribute("media", "speech"); + cb.checked = true; + } + else { + st.setAttribute("media", "all"); + cb.checked = false; + } +} + +function toggleTheme(e) { + var mode = DEFAULT_THEME; + if (e.checked == false) { + mode = ALT_THEME; + } + setTheme(mode); + localStorage.setItem("theme", mode); + return true; +} + +function mqlTheme(e) { + console.log(e); + if (e.matches) { + setTheme(ALT_THEME); + } + else { + setTheme(DEFAULT_THEME); + } +} + +var theme = localStorage.getItem("theme"); +if (theme != null) { + setTheme(theme); +} +else { + var mql = window.matchMedia("(prefers-color-scheme: " + ALT_THEME + ")"); + if(mql.matches) { + setTheme(ALT_THEME); + } + mql.addListener(mqlTheme); +}