Fix theme transition flash on page navigation
- Add immediate theme application in <head> section for all pages - Prevents light mode flash when navigating in dark mode - Script executes before body renders to apply saved theme - Fixes flash when going from privacy policy back to home - Maintains consistent theme experience across all pages - Supports auto theme detection based on system preference
This commit is contained in:
parent
6347c1de40
commit
d20ce03264
3 changed files with 41 additions and 77 deletions
|
@ -7,6 +7,18 @@
|
|||
<link rel="icon" type="image/svg+xml" href="https://iceymi.b-cdn.net/favicon.svg">
|
||||
<link rel="icon" type="image/x-icon" href="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAzMiAzMiIgd2lkdGg9IjMyIiBoZWlnaHQ9IjMyIj4KICA8ZGVmcz4KICAgIDxzdHlsZT4KICAgICAgLnNub3dmbGFrZSB7IGZpbGw6ICMyMTk2RjM7IH0KICAgICAgLmNlbnRlciB7IGZpbGw6ICMxOTc2RDI7IH0KICAgIDwvc3R5bGU+CiAgPC9kZWZzPgogIAogIDxnIGNsYXNzPSJzbm93Zmxha2UiPgogICAgPHJlY3QgeD0iMTUiIHk9IjIiIHdpZHRoPSIyIiBoZWlnaHQ9IjI4IiAvPgogICAgPHJlY3QgeD0iMiIgeT0iMTUiIHdpZHRoPSIyOCIgaGVpZ2h0PSIyIiAvPgogICAgPHJlY3QgeD0iMTUiIHk9IjIiIHdpZHRoPSIyIiBoZWlnaHQ9IjI4IiB0cmFuc2Zvcm09InJvdGF0ZSg0NSAxNiAxNikiIC8+CiAgICA8cmVjdCB4PSIxNSIgeT0iMiIgd2lkdGg9IjIiIGhlaWdodD0iMjgiIHRyYW5zZm9ybT0icm90YXRlKC00NSAxNiAxNikiIC8+CiAgICA8cG9seWdvbiBwb2ludHM9IjE2LDIgMTQsNiAxOCw2IiAvPgogICAgPHBvbHlnb24gcG9pbnRzPSIxNiwzMCAxNCwyNiAxOCwyNiIgLz4KICAgIDxwb2x5Z29uIHBvaW50cz0iMiwxNiA2LDE0IDYsMTgiIC8+CiAgICA8cG9seWdvbiBwb2ludHM9IjMwLDE2IDI2LDE0IDI2LDE4IiAvPgogICAgPHBvbHlnb24gcG9pbnRzPSI2LjMsNi4zIDguNiw0IDkuOSw3LjciIHRyYW5zZm9ybT0icm90YXRlKDQ1IDE2IDE2KSIgLz4KICAgIDxwb2x5Z29uIHBvaW50cz0iMjUuNywyNS43IDIzLjQsMjggMjIuMSwyNC4zIiB0cmFuc2Zvcm09InJvdGF0ZSg0NSAxNiAxNikiIC8+CiAgICA8cG9seWdvbiBwb2ludHM9IjYuMywyNS43IDguNiwyOCA5LjksMjQuMyIgdHJhbnNmb3JtPSJyb3RhdGUoLTQ1IDE2IDE2KSIgLz4KICAgIDxwb2x5Z29uIHBvaW50cz0iMjUuNyw2LjMgMjMuNCw0IDIyLjEsNy43IiB0cmFuc2Zvcm09InJvdGF0ZSgtNDUgMTYgMTYpIiAvPgogIDwvZz4KICA8Y2lyY2xlIGN4PSIxNiIgY3k9IjE2IiByPSIzIiBjbGFzcz0iY2VudGVyIiAvPgo8L3N2Zz4K">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<script>
|
||||
// Apply theme immediately to prevent flash
|
||||
(function() {
|
||||
const savedTheme = localStorage.getItem('theme') || 'auto';
|
||||
if (savedTheme === 'auto') {
|
||||
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
document.documentElement.setAttribute('data-theme', prefersDark ? 'dark' : 'light');
|
||||
} else {
|
||||
document.documentElement.setAttribute('data-theme', savedTheme);
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="privacy-container">
|
||||
|
@ -110,84 +122,9 @@
|
|||
</div>
|
||||
</footer>
|
||||
|
||||
<!-- Load shared theme utility -->
|
||||
<script src="/theme-utils.js"></script>
|
||||
<script>
|
||||
// Theme toggle functionality (shared with main page)
|
||||
function initializeTheme() {
|
||||
const themeToggle = document.getElementById('theme-toggle');
|
||||
const themeIcon = document.querySelector('.theme-icon');
|
||||
|
||||
if (!themeToggle || !themeIcon) {
|
||||
console.warn('Theme toggle elements not found');
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for saved theme preference or default to auto (follows system)
|
||||
const savedTheme = localStorage.getItem('theme') || 'auto';
|
||||
applyTheme(savedTheme);
|
||||
|
||||
// Update icon based on current theme
|
||||
updateThemeIcon(savedTheme, themeIcon);
|
||||
|
||||
// Add click listener for cycling through themes
|
||||
themeToggle.addEventListener('click', () => {
|
||||
const currentTheme = localStorage.getItem('theme') || 'auto';
|
||||
let newTheme;
|
||||
|
||||
// Cycle: auto → light → dark → auto
|
||||
switch(currentTheme) {
|
||||
case 'auto':
|
||||
newTheme = 'light';
|
||||
break;
|
||||
case 'light':
|
||||
newTheme = 'dark';
|
||||
break;
|
||||
case 'dark':
|
||||
newTheme = 'auto';
|
||||
break;
|
||||
default:
|
||||
newTheme = 'auto';
|
||||
}
|
||||
|
||||
localStorage.setItem('theme', newTheme);
|
||||
applyTheme(newTheme);
|
||||
updateThemeIcon(newTheme, themeIcon);
|
||||
|
||||
console.log(`Theme switched to: ${newTheme}`);
|
||||
});
|
||||
|
||||
// Listen for system theme changes when in auto mode
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
|
||||
const currentTheme = localStorage.getItem('theme') || 'auto';
|
||||
if (currentTheme === 'auto') {
|
||||
applyTheme('auto');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function applyTheme(theme) {
|
||||
if (theme === 'auto') {
|
||||
// Detect system preference and apply appropriate theme
|
||||
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
document.documentElement.setAttribute('data-theme', prefersDark ? 'dark' : 'light');
|
||||
} else {
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
}
|
||||
}
|
||||
|
||||
function updateThemeIcon(theme, iconElement) {
|
||||
switch(theme) {
|
||||
case 'auto':
|
||||
iconElement.textContent = '🌍'; // Globe (auto)
|
||||
break;
|
||||
case 'light':
|
||||
iconElement.textContent = '☀️'; // Sun (light)
|
||||
break;
|
||||
case 'dark':
|
||||
iconElement.textContent = '🌙'; // Moon (dark)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize theme when page loads
|
||||
document.addEventListener('DOMContentLoaded', initializeTheme);
|
||||
</script>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue