Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flickering white on page load / reload #6431

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions torchci/components/NavBar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
width: 100%;
min-height: 60px;
align-items: center;
transition: background 0.3s ease, box-shadow 0.3s ease;
Copy link
Contributor

Choose a reason for hiding this comment

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

My .css foo is pretty stale, what browser will support those?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://caniuse.com/css-transitions -> all of them except for ie9 and opera mini

}

.navbar li {
Expand Down
13 changes: 13 additions & 0 deletions torchci/lib/DarkModeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ export function DarkModeProvider({ children }: { children: ReactNode }) {
document.documentElement.classList.add("dark-mode");
} else {
document.documentElement.classList.remove("dark-mode");

// Reset any inline styles that might have been set during initial load
document.querySelectorAll('[style*="background"]').forEach((el) => {
if ((el as HTMLElement).style.backgroundColor === "#1e1e1e") {
(el as HTMLElement).style.removeProperty("background-color");
}
});

// Remove any dark mode inline styles we added at page load
const styleEl = document.getElementById("dark-mode-init-styles");
if (styleEl) {
styleEl.remove();
}
}

// Save the preference to localStorage
Expand Down
54 changes: 54 additions & 0 deletions torchci/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Head, Html, Main, NextScript } from "next/document";

export default function Document() {
return (
<Html>
<Head>
{/* Preload script to prevent white flash in dark mode */}
<script
dangerouslySetInnerHTML={{
__html: `
(function() {
try {
var themeMode = localStorage.getItem('themeMode');
var systemDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
var isDarkMode =
themeMode === 'dark' ||
(themeMode === 'system' && systemDarkMode) ||
(!themeMode && systemDarkMode);

if (isDarkMode) {
document.documentElement.classList.add('dark-mode');
document.documentElement.style.backgroundColor = '#1e1e1e';

// Create and append a style element to handle navbar and other elements before React loads
var style = document.createElement('style');
style.id = 'dark-mode-init-styles';
style.textContent = \`
html, body { background-color: #1e1e1e !important; color: #e0e0e0 !important; }
div[class*="navbar"] {
background: linear-gradient(326deg, #2a2a2a, #2d2d2d) !important;
box-shadow: 0 -4px 20px 0px rgba(0, 0, 0, 0.4) !important;
}
/* Force all top-level elements to have dark background */
#__next, #__next > div {
background-color: #1e1e1e !important;
}
\`;
document.head.appendChild(style);
}
} catch (e) {
// If localStorage is not available, do nothing
}
})();
`,
}}
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
4 changes: 4 additions & 0 deletions torchci/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ html {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
background-color: var(--background-color);
color: var(--text-color);
transition: background-color 0.3s ease, color 0.3s ease;
}

h1 {
Expand Down Expand Up @@ -222,6 +225,7 @@ hr {
/* Component background elements */
.component-bg {
background-color: var(--dropdown-bg);
transition: background-color 0.3s ease;
}

.animate-on-click {
Expand Down