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

Prevent dark mode flash on page refresh #182

Merged
merged 1 commit into from
Jan 2, 2025
Merged

Prevent dark mode flash on page refresh #182

merged 1 commit into from
Jan 2, 2025

Conversation

chacha912
Copy link
Contributor

@chacha912 chacha912 commented Jan 2, 2025

What this PR does / why we need it?

This PR fixes two theme-related issues:

  1. Fixed 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.
before after
dashboard-fouc dashboard-fix
  1. Fixed system theme preference not being applied.
    Previously, when using system preference settings, the app didn't respond to dark/light mode changes. Now the theme updates properly when system preferences change.
before after
dashboard-system dashboard-theme

Any background context you want to provide?

What are the relevant tickets?

Fixes #

Checklist

  • Added relevant tests or not required
  • Didn't break anything

Summary by CodeRabbit

  • New Features

    • Enhanced theme management with support for system and manual dark/light mode preferences
    • Added dynamic theme switching based on user or system settings
  • Style

    • Updated default HTML background color to improve visual consistency
  • Chores

    • Improved theme detection and application logic across the application

@chacha912 chacha912 requested a review from hackerwins January 2, 2025 07:52
Copy link

coderabbitai bot commented Jan 2, 2025

Walkthrough

The pull request introduces enhanced theme management functionality across the application. A new script in index.html checks and applies user theme preferences, while App.tsx implements a more dynamic theme switching mechanism. The changes include a new applyTheme function, system theme detection, and an additional background color style in the base SCSS file to support the dark/light mode transitions.

Changes

File Change Summary
index.html Added theme preference detection script in <head> section
src/App.tsx Introduced applyTheme function for theme management, added system theme change listener
src/assets/styles/base/_common.scss Added default background color to html selector

Sequence Diagram

sequenceDiagram
    participant User
    participant Browser
    participant App
    participant SystemPreference

    User->>Browser: Load Page
    Browser->>App: Initialize Theme
    App->>SystemPreference: Check System Theme
    SystemPreference-->>App: Return Theme Preference
    App->>Browser: Apply Theme (Light/Dark)
    Browser->>User: Render Page with Selected Theme
Loading

Poem

🌓 Themes dance, light and dark entwine,
A rabbit's code makes colors align!
System whispers, preferences gleam,
Switching softly like a moonlit dream 🐰
Elegance coded with pixel-pure art!


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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/App.tsx (2)

40-48: Centralize theme styling in one place
Introducing applyTheme unifies theme application logic, improving maintainability. However, consider validating the input (e.g., handling unexpected theme values) to guard against potential inconsistencies.


56-69: Account for system preference across browsers
Implementing system theme detection and dynamic updates is a great enhancement. Consider a fallback for browsers that do not support the 'change' event on matchMedia (particularly older ones) to ensure broad compatibility.

index.html (1)

34-44: Optimize the inline theme script
The initial script ensures a consistent theme is applied before the page renders. Verify that localStorage is always accessible in the environment. For improved performance, consider inlining only minimal logic and deferring or bundling more complex checks if needed.

src/assets/styles/base/_common.scss (1)

132-135: Extend background color for dark mode
Setting a default background color on <html> helps prevent a flash. However, you might also define a dark-mode-specific background (e.g., via .darkmode html { background-color: ... }) for a smoother transition in dark mode.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b16ce07 and 65ce8cd.

📒 Files selected for processing (3)
  • index.html (1 hunks)
  • src/App.tsx (2 hunks)
  • src/assets/styles/base/_common.scss (1 hunks)
🔇 Additional comments (2)
src/App.tsx (2)

18-18: Confirm styling dependencies
Importing style.scss here seems appropriate for ensuring global styles are loaded. No issues found.


53-53: Synchronized darkMode usage
This line succinctly toggles between dark and light modes. It aligns well with the rest of the theme logic. No issues found.

Copy link
Member

@hackerwins hackerwins left a 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.

@hackerwins hackerwins merged commit b44d5b1 into main Jan 2, 2025
2 checks passed
@hackerwins hackerwins deleted the darkmode branch January 2, 2025 08:37
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.

2 participants