-
Notifications
You must be signed in to change notification settings - Fork 16
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
Prevent dark mode flash on page refresh #182
Conversation
Warning Rate limit exceeded@chacha912 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 28 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
WalkthroughThe pull request introduces improvements to theme handling across multiple files. A new Changes
Sequence DiagramsequenceDiagram
participant User
participant Browser
participant LocalStorage
participant Document
User->>Browser: Load Page
Browser->>LocalStorage: Check Theme Preference
LocalStorage-->>Browser: Return Theme
Browser->>Document: Apply Theme
alt Dark Theme
Document->Document: Add 'darkmode' class
Document->Document: Set color scheme to dark
else Light Theme
Document->Document: Remove 'darkmode' class
Document->Document: Set color scheme to light
end
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
hooks/useTheme.ts (1)
4-12
: Consider fallback checks for SSR environments.
In Next.js,window
is only available on the client side; callingwindow.document.documentElement
can cause issues if this code runs on the server. Though your usage might strictly be in effect after mounting, adding an optional fallback check can make the code more robust if future refactors introduce server-side rendering concerns.const applyTheme = (theme: 'light' | 'dark') => { - if (theme === 'light') { - window.document.documentElement.classList.remove('darkmode'); - window.document.documentElement.style.colorScheme = 'light'; - } else { - window.document.documentElement.classList.add('darkmode'); - window.document.documentElement.style.colorScheme = 'dark'; - } + if (typeof window !== 'undefined') { + if (theme === 'light') { + window.document.documentElement.classList.remove('darkmode'); + window.document.documentElement.style.colorScheme = 'light'; + } else { + window.document.documentElement.classList.add('darkmode'); + window.document.documentElement.style.colorScheme = 'dark'; + } + } };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
hooks/useTheme.ts
(1 hunks)pages/_app.tsx
(1 hunks)styles/base/_common.scss
(1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
pages/_app.tsx
[error] 41-41: Avoid passing content using the dangerouslySetInnerHTML prop.
Setting content using code can expose users to cross-site scripting (XSS) attacks
(lint/security/noDangerouslySetInnerHtml)
🔇 Additional comments (3)
hooks/useTheme.ts (2)
19-19
: Good use of theapplyTheme
function for system theme changes.
This centralized approach ensures consistent look and feel when system preferences change.
23-30
: Centralized theme application is a clean approach.
UsingapplyTheme
for all theme choices keeps your code DRY and maintainable, reducing the risk of divergence in how themes are applied.styles/base/_common.scss (1)
132-134
: Approved: Setting a default HTML background helps prevent white flashes.
This ensures users see a consistent background color before the theme styles fully load. Great job aligning this change with dark-mode enhancements in the codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution.
What this PR does / why we need it?
This PR fixes the white flash that appears when refreshing the page in dark mode.
To solve this, we now apply dark mode styles earlier in the page load process (before First Paint), so users won't see any unwanted white flashes when refreshing.
Any background context you want to provide?
What are the relevant tickets?
Fixes #
Checklist
Summary by CodeRabbit
New Features
Style Changes
Refactor
applyTheme
function