summary refs log tree commit diff stats
path: root/config
diff options
context:
space:
mode:
authordrtheuns <randall@theuns.org>2021-06-07 18:40:09 +0200
committerGitHub <noreply@github.com>2021-06-07 18:40:09 +0200
commit51ab7ccec1ffa21cef67f1a75dea889653751cfc (patch)
tree01682df2b1b150b0592a112cbf6a5c3d6b058c5c /config
parent21f3b8539a2e440f7cb69502721b6c56070e122f (diff)
downloadNim-51ab7ccec1ffa21cef67f1a75dea889653751cfc.tar.gz
Fix JS error on index page and detect dark mode (#18191)
* Fix JS error on index page and detect dark mode

The theindex.html page doesn't have a dark mode switch so the main
function will error because `toggleSwitch` is not defined. Checks have
been added to prevent this from happening.

Also add automatic detection of system settings for dark-mode. This
could also be done with pure css, but then the dark mode variable
declarations would have to be duplicated to work with the switch so I
went with this approach.

* Fix nimdoc tests

* Fix rst2html tests
Diffstat (limited to 'config')
-rw-r--r--config/nimdoc.cfg13
1 files changed, 9 insertions, 4 deletions
diff --git a/config/nimdoc.cfg b/config/nimdoc.cfg
index ed1b346a2..cdf49197d 100644
--- a/config/nimdoc.cfg
+++ b/config/nimdoc.cfg
@@ -252,7 +252,6 @@ function main() {
     }
   }
 
-  const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');
   function switchTheme(e) {
       if (e.target.checked) {
           document.documentElement.setAttribute('data-theme', 'dark');
@@ -263,13 +262,19 @@ function main() {
       }
   }
 
-  toggleSwitch.addEventListener('change', switchTheme, false);
+  const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');
+  if (toggleSwitch !== null) {
+    toggleSwitch.addEventListener('change', switchTheme, false);
+  }
 
-  const currentTheme = localStorage.getItem('theme') ? localStorage.getItem('theme') : null;
+  var currentTheme = localStorage.getItem('theme');
+  if (!currentTheme && window.matchMedia('(prefers-color-scheme: dark)').matches) {
+    currentTheme = 'dark';
+  }
   if (currentTheme) {
     document.documentElement.setAttribute('data-theme', currentTheme);
 
-    if (currentTheme === 'dark') {
+    if (currentTheme === 'dark' && toggleSwitch !== null) {
       toggleSwitch.checked = true;
     }
   }