Skip to content

Fix dark mode white flash on page refresh#3482

Merged
springfall2008 merged 2 commits intospringfall2008:mainfrom
warksit:fix-dark-mode-flash
Mar 5, 2026
Merged

Fix dark mode white flash on page refresh#3482
springfall2008 merged 2 commits intospringfall2008:mainfrom
warksit:fix-dark-mode-flash

Conversation

@warksit
Copy link
Contributor

@warksit warksit commented Mar 4, 2026

Summary

  • Adds an inline <script> at the very start of <head> (before the <style> block) that applies the dark-mode class to <html> from localStorage
  • Adds html.dark-mode body CSS rules so the body inherits the dark background immediately when created, without waiting for window.onload
  • Eliminates the flash of white background that occurs every time the page refreshes in dark mode
  • The existing window.onload handler still runs to update the logo

Test plan

  • Enable dark mode in Predbat web UI
  • Refresh the page — background should stay dark with no white flash
  • Toggle dark mode off and verify light mode still works
  • Verify logo updates correctly for both modes

🤖 Generated with Claude Code

@warksit warksit force-pushed the fix-dark-mode-flash branch 2 times, most recently from eb718c9 to ed7670a Compare March 4, 2026 12:55
Apply dark-mode class to <html> immediately via inline script in <head>,
before the body renders. The existing window.onload handler still runs
to apply body classes and update the logo.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent the dark-mode “white flash” on page refresh in Predbat’s web UI by applying dark mode as early as possible during initial page load.

Changes:

  • Adds an inline <script> early in <head> to apply the dark-mode class to <html> based on localStorage.
  • Adds html.dark-mode body CSS rules so the <body> gets dark background/text before the existing window.onload handler runs.

Comment on lines +7070 to +7073
if (localStorage.getItem('darkMode') === 'true') {
document.documentElement.classList.add('dark-mode');
document.documentElement.style.backgroundColor = '#121212';
document.documentElement.style.color = '#e0e0e0';
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This early script only adds dark-mode to <html>, but most dark-mode styling in this header (e.g., .menu-bar, buttons, tables) is keyed off body.dark-mode and is still applied via applyDarkMode() on window.onload. That means some UI elements can still render in light theme briefly on refresh (even if the page background is dark). Consider applying the body class earlier (e.g., DOMContentLoaded instead of window.onload, or extending CSS selectors to html.dark-mode … equivalents) so all dark-mode styles take effect immediately.

Suggested change
if (localStorage.getItem('darkMode') === 'true') {
document.documentElement.classList.add('dark-mode');
document.documentElement.style.backgroundColor = '#121212';
document.documentElement.style.color = '#e0e0e0';
if (localStorage.getItem('darkMode') === 'true') {
// Apply dark mode to the root element as early as possible
document.documentElement.classList.add('dark-mode');
document.documentElement.style.backgroundColor = '#121212';
document.documentElement.style.color = '#e0e0e0';
// As soon as the DOM is ready, mirror the class on <body> so that
// all CSS rules keyed off body.dark-mode apply without waiting for window.onload
document.addEventListener('DOMContentLoaded', function () {
if (document.body) {
document.body.classList.add('dark-mode');
}
});

Copilot uses AI. Check for mistakes.
Comment on lines +7072 to +7073
document.documentElement.style.backgroundColor = '#121212';
document.documentElement.style.color = '#e0e0e0';
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inline script sets document.documentElement.style.backgroundColor/style.color, which duplicates the dark-mode colors already defined in the stylesheet below (e.g., html.dark-mode … and html.dark-mode body …). Keeping both inline styles and CSS makes future theme tweaks easy to miss/inconsistent; consider only adding the dark-mode class here and letting CSS control colors (or centralize the colors via CSS variables).

Suggested change
document.documentElement.style.backgroundColor = '#121212';
document.documentElement.style.color = '#e0e0e0';

Copilot uses AI. Check for mistakes.
Comment on lines +7085 to +7087
html.dark-mode, html.dark-mode body {
border-color: #121212;
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The html.dark-mode, html.dark-mode body { border-color: #121212; } rule appears redundant with the later existing rule that sets border: 2px solid #121212 for html.dark-mode / body.dark-mode. Consider removing this earlier rule to avoid duplicate/competing sources for the same border styling.

Suggested change
html.dark-mode, html.dark-mode body {
border-color: #121212;
}

Copilot uses AI. Check for mistakes.
Addresses Copilot review: inline styles duplicated CSS rules and most
dark-mode selectors target body.dark-mode. Now the early script only sets
the class on <html>, and adds body.dark-mode at DOMContentLoaded so all
existing body.dark-mode CSS selectors take effect before window.onload.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@springfall2008 springfall2008 merged commit 119a4b8 into springfall2008:main Mar 5, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants