]> danix's work - danix.xyz-2.git/commitdiff
feat: create theme toggle with localStorage persistence
authorDanilo M. <redacted>
Wed, 15 Apr 2026 13:36:17 +0000 (15:36 +0200)
committerDanilo M. <redacted>
Wed, 15 Apr 2026 13:36:17 +0000 (15:36 +0200)
themes/danix-xyz-hacker/assets/js/theme-toggle.js [new file with mode: 0644]

diff --git a/themes/danix-xyz-hacker/assets/js/theme-toggle.js b/themes/danix-xyz-hacker/assets/js/theme-toggle.js
new file mode 100644 (file)
index 0000000..97ed1ce
--- /dev/null
@@ -0,0 +1,30 @@
+document.addEventListener('DOMContentLoaded', function() {
+  const themeToggle = document.getElementById('theme-toggle');
+
+  if (!themeToggle) {
+    return;
+  }
+
+  themeToggle.addEventListener('click', function(e) {
+    e.preventDefault();
+
+    // Get current theme from html element
+    const htmlElement = document.documentElement;
+    const isDark = htmlElement.classList.contains('theme-dark');
+    const newTheme = isDark ? 'light' : 'dark';
+
+    // Remove both theme classes
+    htmlElement.classList.remove('theme-light', 'theme-dark');
+
+    // Add the new theme class
+    htmlElement.classList.add(`theme-${newTheme}`);
+
+    // Persist to localStorage
+    localStorage.setItem('theme', newTheme);
+
+    // Update Feather Icons if available
+    if (window.feather) {
+      window.feather.replace();
+    }
+  });
+});