Skip to content

Commit 2bbcd2c

Browse files
authored
merge: fix: read system preference to prevent light theme flash on initial l… (#7820)
## Description Fixes #7802 Previously, the inline blocking script in `layout.tsx` defaulted to rendering the dark theme if no theme was saved in `localStorage`. However, if a user's system preference was set to light mode, or vice versa, this caused a jarring flash of the wrong theme before client-side hydration corrected it. This PR updates the `<head>` injection script in `layout.tsx` to properly read `window.matchMedia('(prefers-color-scheme: dark)')` during its initial synchronous execution. This ensures that the application immediately renders the correct theme matching the user's operating system preferences on their very first visit, completely eliminating the white/dark flash. ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have started the repo. - [x] I have made sure that i have only one commit to merge in this PR. - [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents e48131f + 9793624 commit 2bbcd2c

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

app/layout.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@ export default function RootLayout({ children }: { children: React.ReactNode })
9191
__html: `
9292
try {
9393
const storedTheme = window.localStorage.getItem('theme');
94-
if (storedTheme === 'light') {
95-
document.documentElement.classList.remove('dark');
96-
document.documentElement.style.colorScheme = 'light';
97-
} else {
94+
const isSystemDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
95+
if (storedTheme === 'dark' || (!storedTheme && isSystemDark)) {
9896
document.documentElement.classList.add('dark');
9997
document.documentElement.style.colorScheme = 'dark';
98+
} else {
99+
document.documentElement.classList.remove('dark');
100+
document.documentElement.style.colorScheme = 'light';
100101
}
101102
} catch (_) {}
102103
`,

0 commit comments

Comments
 (0)