Fix theme persistence bug

This commit is contained in:
2026-02-15 18:46:41 -05:00
parent 816438430e
commit fb9f52cfc6
2 changed files with 27 additions and 11 deletions

View File

@@ -36,14 +36,32 @@
<!-- Theme detection script - runs before Vue loads to prevent flash of unstyled content --> <!-- Theme detection script - runs before Vue loads to prevent flash of unstyled content -->
<script> <script>
(function() { (function() {
// Check for saved theme preference or system preference // Check for saved theme preference only
const savedTheme = localStorage.getItem('theme'); const savedTheme = localStorage.getItem('theme');
if (!savedTheme) {
return; // Let Vue handle default theme
}
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches; const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const theme = savedTheme || (prefersDark ? 'dark' : 'light'); let theme = savedTheme;
// Handle 'auto' theme preference
if (theme === 'auto') {
theme = prefersDark ? 'dark' : 'light';
}
// Apply theme to html element // Apply theme to html element
document.documentElement.setAttribute('data-bs-theme', theme); document.documentElement.setAttribute('data-bs-theme', theme);
document.documentElement.setAttribute('data-theme', theme); document.documentElement.setAttribute('data-theme', theme);
// Apply theme classes
if (theme === 'dark') {
document.documentElement.classList.add('dark-theme');
document.documentElement.classList.remove('light-theme');
} else {
document.documentElement.classList.add('light-theme');
document.documentElement.classList.remove('dark-theme');
}
})(); })();
</script> </script>
</head> </head>

View File

@@ -18,7 +18,7 @@ export default {
sortMethod: 'score', sortMethod: 'score',
topics: [], topics: [],
isMobile: false, isMobile: false,
currentTheme: 'auto', currentTheme: typeof localStorage !== 'undefined' ? (localStorage.getItem('theme') || 'auto') : 'auto',
mediaQueryListener: null, mediaQueryListener: null,
vuetifyTheme: null, vuetifyTheme: null,
darkModeQuery: null, darkModeQuery: null,
@@ -31,11 +31,9 @@ export default {
this.fetchDeals(); this.fetchDeals();
// Initialize sort method from local storage // Initialize sort method from local storage
this.initializeSortMethod(); this.initializeSortMethod();
// Initialize theme on next tick // Initialize theme immediately to prevent flash
this.$nextTick(() => { this.initializeTheme();
this.initializeTheme(); this.setupThemeListener();
this.setupThemeListener();
});
}, },
beforeUnmount() { beforeUnmount() {
window.removeEventListener("keydown", this.handleKeyDown); window.removeEventListener("keydown", this.handleKeyDown);
@@ -50,11 +48,11 @@ export default {
const savedTheme = localStorage.getItem('theme'); const savedTheme = localStorage.getItem('theme');
if (!savedTheme) { if (!savedTheme) {
this.currentTheme = 'auto'; this.currentTheme = 'auto';
this.applyTheme('auto'); this.applyTheme('auto', true); // skipSave=true to avoid redundant write
} else { } else {
this.currentTheme = savedTheme; this.currentTheme = savedTheme;
// Apply saved theme // Apply saved theme (skipSave=true since it's already saved)
this.applyTheme(savedTheme); this.applyTheme(savedTheme, true);
} }
}, },
setupThemeListener() { setupThemeListener() {