From a060469adc46b2af68263904dfab4936ba6f00d2 Mon Sep 17 00:00:00 2001 From: jessikitty Date: Thu, 21 May 2026 10:13:23 +1000 Subject: [PATCH] feat: Theme toggle with localStorage persistence --- wwwroot/js/admin.js | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/wwwroot/js/admin.js b/wwwroot/js/admin.js index 33ffbb7..636be0e 100644 --- a/wwwroot/js/admin.js +++ b/wwwroot/js/admin.js @@ -1,7 +1,46 @@ // === Scratching Post Admin JS === + +// === Theme Toggle === +(function () { + var saved = localStorage.getItem('sb-theme') || 'dark'; + document.documentElement.setAttribute('data-theme', saved); + + document.addEventListener('DOMContentLoaded', function () { + updateToggleIcon(saved); + + var btn = document.getElementById('themeToggle'); + if (btn) { + btn.addEventListener('click', function () { + var current = document.documentElement.getAttribute('data-theme') || 'dark'; + var next = current === 'dark' ? 'light' : 'dark'; + document.documentElement.setAttribute('data-theme', next); + localStorage.setItem('sb-theme', next); + updateToggleIcon(next); + }); + } + }); + + function updateToggleIcon(theme) { + var btn = document.getElementById('themeToggle'); + if (!btn) return; + btn.innerHTML = theme === 'dark' + ? '' + : ''; + btn.title = theme === 'dark' ? 'Switch to Light Mode' : 'Switch to Dark Mode'; + } +})(); + +// Auto-dismiss alerts after 5 seconds document.querySelectorAll('.alert-dismissible').forEach(function (alert) { - setTimeout(function () { var bsAlert = bootstrap.Alert.getOrCreateInstance(alert); bsAlert.close(); }, 5000); + setTimeout(function () { + var bsAlert = bootstrap.Alert.getOrCreateInstance(alert); + bsAlert.close(); + }, 5000); }); + +// Confirm dangerous actions document.querySelectorAll('[data-confirm]').forEach(function (el) { - el.addEventListener('click', function (e) { if (!confirm(this.dataset.confirm)) e.preventDefault(); }); + el.addEventListener('click', function (e) { + if (!confirm(this.dataset.confirm)) e.preventDefault(); + }); });