Refactor theme handling in login page to prioritize saved preferences from localStorage before falling back to system color scheme

This commit is contained in:
Théophile Diot 2024-11-08 12:35:08 +01:00
parent 175cc70b70
commit 9c9ce5a55d
No known key found for this signature in database
GPG key ID: FA995104A0BA376A

View file

@ -1,11 +1,16 @@
$(document).ready(() => {
// If no saved preference, use the system's preferred color scheme
const systemPrefersDark = window.matchMedia(
"(prefers-color-scheme: dark)",
).matches;
savedTheme = systemPrefersDark ? "dark" : "light";
// Check if there's a saved theme preference in localStorage
let savedTheme = localStorage.getItem("theme");
// Apply the preferred theme
if (!savedTheme) {
// If no saved preference, use the system's preferred color scheme
const systemPrefersDark = window.matchMedia(
"(prefers-color-scheme: dark)",
).matches;
savedTheme = systemPrefersDark ? "dark" : "light";
}
// Apply the saved or system-preferred theme
applyTheme(savedTheme);
// Toggle theme on change