diff options
author | Giovanni Condello <giovanni.condello@coderit.it> | 2022-08-04 19:44:05 +0200 |
---|---|---|
committer | Simone Bortolin <simonebortolin@users.noreply.github.com> | 2022-12-19 22:50:13 +0100 |
commit | 73669e296b14d0e3b610b0553585981be59911ed (patch) | |
tree | 0be5e009a6787bc110352ecb1eef2ced7b9bb1e3 /assets/js | |
parent | AFM PLOAM logging (diff) | |
parent | update script (diff) | |
download | hack-gpon.github.io-73669e296b14d0e3b610b0553585981be59911ed.tar hack-gpon.github.io-73669e296b14d0e3b610b0553585981be59911ed.tar.gz hack-gpon.github.io-73669e296b14d0e3b610b0553585981be59911ed.tar.bz2 hack-gpon.github.io-73669e296b14d0e3b610b0553585981be59911ed.tar.lz hack-gpon.github.io-73669e296b14d0e3b610b0553585981be59911ed.tar.xz hack-gpon.github.io-73669e296b14d0e3b610b0553585981be59911ed.tar.zst hack-gpon.github.io-73669e296b14d0e3b610b0553585981be59911ed.zip |
Diffstat (limited to 'assets/js')
-rw-r--r-- | assets/js/theme-switch.js | 52 |
1 files changed, 32 insertions, 20 deletions
diff --git a/assets/js/theme-switch.js b/assets/js/theme-switch.js index 02b0ce9..c3f3949 100644 --- a/assets/js/theme-switch.js +++ b/assets/js/theme-switch.js @@ -1,21 +1,33 @@ -
-if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
- jtd.setTheme('dark');
-} else {
- jtd.setTheme('light');
-}
-
-window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
- const newColorScheme = event.matches ? "dark" : "light";
- jtd.setTheme(newColorScheme);
-});
-
-
-const toggleDarkMode = document.querySelector('.js-toggle-dark-mode');
-jtd.addEvent(toggleDarkMode, 'click', function(){
- if (jtd.getTheme() === 'dark') {
- jtd.setTheme('light');
- toggleDarkMode.textContent = 'Preview dark color scheme';
- } else { jtd.setTheme('dark');
- toggleDarkMode.textContent = 'Return to the light side'; }
+if(localStorage.getItem("color-scheme") === null) { + const newColorScheme = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? "dark" : "light"; + const newTextScheme = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? "Light mode" : "Dark mode"; + jtd.setTheme(newColorScheme); + toggleDarkMode.textContent = newTextScheme; + localStorage.setItem("color-scheme",newColorScheme); + localStorage.setItem("text-scheme",newTextScheme); +} else { + jtd.setTheme(localStorage.getItem("color-scheme")); + toggleDarkMode.textContent = localStorage.getItem("text-scheme"); +} + +window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { + if(localStorage.getItem("color-scheme") === null) { + const newColorScheme = event.matches ? "dark" : "light"; + const newTextScheme = event.matches ? "Light mode" : "Dark mode"; + jtd.setTheme(newColorScheme); + toggleDarkMode.textContent = newTextScheme; + localStorage.setItem("color-scheme",newColorScheme); + localStorage.setItem("text-scheme",newTextScheme); + } +}); + + +const toggleDarkMode = document.querySelector('.js-toggle-dark-mode'); +jtd.addEvent(toggleDarkMode, 'click', function(){ + const newColorScheme = jtd.getTheme() === 'dark' ? "dark" : "light"; + const newTextScheme = jtd.getTheme() === 'dark' ? "Light mode" : "Dark mode"; + jtd.setTheme(newColorScheme); + toggleDarkMode.textContent = newTextScheme; + localStorage.setItem("color-scheme",newColorScheme); + localStorage.setItem("text-scheme",newTextScheme); });
\ No newline at end of file |