Explorar o código

enableDarkMode bug fix

fix #24
zzossig %!s(int64=5) %!d(string=hai) anos
pai
achega
8952855ede
Modificáronse 1 ficheiros con 20 adicións e 11 borrados
  1. 20 11
      layouts/partials/head/scripts.html

+ 20 - 11
layouts/partials/head/scripts.html

@@ -219,27 +219,36 @@
     }) : null;
     // =============================================================
 
-
     
     // ======================= toggle theme =======================
+    {{ $enableDarkMode := ($.Param "enableDarkMode") }}
+    var enableDarkMode = JSON.parse({{ $enableDarkMode | jsonify }});
     var root = document.getElementById('root');
     var toggleToLightBtn = document.getElementById('toggleToLight');
     var toggleToDarkBtn = document.getElementById('toggleToDark');
 
-    toggleToDark.onclick = function(e) {
-      root.className = 'theme__dark';
-      localStorage.setItem('theme', 'dark');
-      toggleToLightBtn.className = 'navbar__icons--icon';
-      toggleToDarkBtn.className = 'hide';
-    }
-
-    toggleToLight.onclick = function (e) {
+    if (!enableDarkMode) {
       root.className = 'theme__light';
       localStorage.setItem('theme', 'light');
-      toggleToLightBtn.className = 'hide';
-      toggleToDarkBtn.className = 'navbar__icons--icon';
+    }
+    
+    if (toggleToDarkBtn) {
+      toggleToDarkBtn.onclick = function (e) {
+        root.className = 'theme__dark';
+        localStorage.setItem('theme', 'dark');
+        toggleToLightBtn.className = 'navbar__icons--icon';
+        toggleToDarkBtn.className = 'hide';
+      }
     }
 
+    if (toggleToLightBtn) {
+      toggleToLightBtn.onclick = function (e) {
+        root.className = 'theme__light';
+        localStorage.setItem('theme', 'light');
+        toggleToLightBtn.className = 'hide';
+        toggleToDarkBtn.className = 'navbar__icons--icon';
+      }
+    }
 
     // =================== section menu collapse ==================
     document.querySelectorAll('.menu__list').forEach(function(elem) {