Skip to content

autodetect darkmode#241

Open
rajivsinclair wants to merge 1 commit intomainfrom
solver/improve-theme-toggle-system-sync
Open

autodetect darkmode#241
rajivsinclair wants to merge 1 commit intomainfrom
solver/improve-theme-toggle-system-sync

Conversation

@rajivsinclair
Copy link
Contributor

@rajivsinclair rajivsinclair commented Mar 11, 2025

Added automatic system theme detection

  • Detects initial system color scheme preference
  • Automatically syncs with system theme changes
  • Preserves user's manual theme choice if previously set

Simplified theme toggle interaction

  • Replaced dropdown menu with a single-click toggle button
  • Maintained existing animations and visual feedback
  • Kept tooltip for accessibility

Summary by CodeRabbit

  • New Features
    • The theme toggle has been streamlined with a single button for fast switching between light and dark modes.
    • Theme settings now automatically load saved preferences and adjust in real time to changes in system settings.

@ellipsis-dev
Copy link

ellipsis-dev bot commented Mar 11, 2025

Your free trial has expired. To keep using Ellipsis, sign up at https://app.ellipsis.dev or contact us.

@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2025

Walkthrough

This pull request simplifies the theme toggle functionality. It replaces the previous dropdown menu in the mode toggle component with a single button to switch between light and dark themes. In addition, the theme provider now initializes the theme based on a stored value or the system's color preference and listens for changes in the system theme, updating the state accordingly.

Changes

File Change Summary
src/components/ui/mode-toggle.tsx Removed the DropdownMenu structure and its associated elements; replaced with a button that toggles the theme directly on click, retaining icon display.
src/providers/theme.tsx Modified theme state initialization to check localStorage and system preferences; added an effect hook to listen and respond to system theme changes.

Sequence Diagram(s)

Manual Theme Toggle Flow

sequenceDiagram
  participant User
  participant ModeToggle
  participant ThemeProvider
  User->>ModeToggle: Clicks toggle button
  ModeToggle->>ThemeProvider: Invoke toggleTheme()
  ThemeProvider->>ThemeProvider: Update theme state
  ThemeProvider->>User: UI updates with new theme
Loading

System Theme Change Listener Flow

sequenceDiagram
  participant System
  participant ThemeProvider
  System->>ThemeProvider: System changes theme preference
  ThemeProvider->>ThemeProvider: useEffect updates theme state
  ThemeProvider->>User: UI updates with new theme
Loading

Possibly related PRs

  • [f] 150 - Remove icon UI #115: Adjusts theme toggling by removing a button in header components, aligning with the broader changes to theme toggling across the application.

Suggested reviewers

  • nlgthuan

Poem

I hopped through code with twitching nose,
Simplifying toggles as the theme now flows,
From dropdown clutter to a button so neat,
With system sync, my code's complete!
Celebrate with hops and a curious beat! 🐰✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @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 (2)
src/components/ui/mode-toggle.tsx (1)

15-20: Use an accessible label for better screen reader support
It's recommended to add an aria-label or other descriptive attribute to the button for improved accessibility, ensuring that screen readers communicate the toggle functionality.

  <Button 
    variant='ghost' 
    size='icon' 
    className='h-8 w-8 p-0'
-   onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}
+   aria-label="Toggle theme"
+   onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}
  >
src/providers/theme.tsx (1)

28-28: Consider using optional chaining for matchMedia
Guarding against undefined or unavailable window.matchMedia in certain environments prevents runtime errors.

- if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
+ if (window.matchMedia?.('(prefers-color-scheme: dark)').matches) {
    return 'dark'
  }
🧰 Tools
🪛 Biome (1.9.4)

[error] 28-28: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4d833bb and 0437496.

📒 Files selected for processing (2)
  • src/components/ui/mode-toggle.tsx (1 hunks)
  • src/providers/theme.tsx (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/providers/theme.tsx

[error] 28-28: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (2)
src/components/ui/mode-toggle.tsx (1)

21-25: The dynamic icon usage looks good
Conditionally rendering the sun or moon icon based on the current theme is clear and intuitive.

src/providers/theme.tsx (1)

34-42: Good use of system theme event listener
Listening for changes in the system color scheme ensures a seamless real-time theme update experience.

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.

1 participant