From e342680351215fd570361a0815723bffc8a67023 Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Tue, 23 Jan 2024 09:18:31 +0000 Subject: [PATCH] User Theme saved to local storage The theme will no longer reset to the default on page change or reload. --- apps/static/js/base.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/apps/static/js/base.js b/apps/static/js/base.js index 39a1faf..67ae214 100644 --- a/apps/static/js/base.js +++ b/apps/static/js/base.js @@ -1,15 +1,22 @@ -$("#themeToggle").on("click", function() { - const currentTheme = $("body").attr("data-bs-theme"); +$(document).ready(function() { + // Activate all tooltips + $('[data-bs-toggle="tooltip"]').tooltip(); - if (currentTheme == "light") { - $("body").attr("data-bs-theme", "dark"); + // Apply the user preferred theme + const theme = localStorage.getItem("theme"); + if (theme == "light" || theme == "dark") { + $("body").attr("data-bs-theme", theme); } else { $("body").attr("data-bs-theme", "light"); } }); -$(document).ready(function() { - // Activate all tooltips - $('[data-bs-toggle="tooltip"]').tooltip(); +$("#themeToggle").on("click", function() { + var theme = $("body").attr("data-bs-theme"); + + theme = theme == "light" ? "dark" : "light"; + + localStorage.setItem("theme", theme) + $("body").attr("data-bs-theme", theme); }); \ No newline at end of file