Files
Warracker/frontend/theme-loader.js
sassanix 4b377f4259 This update improves filter syncing, OIDC settings, and mobile UX, with fixes for archived items, menus, and the login page.
* Major usability and reliability improvements across frontend and backend
* Filters (Status, Tag, Vendor, Type, Search, Sort) now persist across views and sync via API for cross-device consistency
* Archived warranties correctly appear under “All” without affecting other filters
* Enhanced OIDC system with admin group support, secure secret handling, and improved attribute synchronization
* New responsive mobile hamburger menu and tablet-specific UI enhancements
* Fixed archived item styling, menu initialization, and login page layout issues
* Includes minor UX refinements, PyJWT compatibility updates, and deprecation clean-ups
2025-10-06 20:31:04 -03:00

49 lines
2.0 KiB
JavaScript

// theme-loader.js
(function() {
try {
// Function to apply the theme directly to the root element
function applyTheme(isDark) {
const theme = isDark ? 'dark' : 'light';
document.documentElement.setAttribute('data-theme', theme);
// No need for console log in production loader script
// console.log(`Theme applied on load: ${theme}`);
}
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
return null;
}
// Default to light theme initially
let isDarkMode = false;
// Priority: cookie 'theme' -> localStorage 'darkMode' -> system preference
const themeCookie = getCookie('theme');
const savedTheme = localStorage.getItem('darkMode');
if (themeCookie === 'dark' || themeCookie === 'light') {
isDarkMode = themeCookie === 'dark';
} else if (savedTheme !== null) {
// Use the saved theme preference
isDarkMode = savedTheme === 'true';
} else {
// Fallback to system preference if no theme saved in localStorage
const prefersDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
// console.log(`No saved theme found, falling back to system preference: ${prefersDarkMode ? 'dark' : 'light'}`);
isDarkMode = prefersDarkMode;
// Optionally, save the detected system preference as the initial setting for future loads
// localStorage.setItem('darkMode', isDarkMode); // Consider if this is desired behavior
}
// Apply the determined theme
applyTheme(isDarkMode);
} catch (e) {
console.error("Error applying theme from theme-loader.js:", e);
// Fallback to light theme in case of error
document.documentElement.setAttribute('data-theme', 'light');
}
})();