fix: Theme toggle icon syncs correctly with stored theme on page reload#25
Conversation
WalkthroughToggle component rendering was adjusted: the unmounted render uses Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🔇 Additional comments (3)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
components/toggle-theme.tsx (1)
32-54: Optional: Consider accessibility attributes on unmounted placeholder.The unmounted placeholder includes
role="button"andaria-label="Toggle theme"but has no click handler. While this state is very brief (only during hydration), you might consider:
- Removing
role="button"andaria-labelfrom the placeholder, or- Adding
aria-hidden="true"to indicate it's not interactiveThis is a minor refinement since the placeholder appears for only milliseconds during hydration, and the lack of
tabIndexalready prevents keyboard interaction.Example if you want to refine:
<div className="flex items-center space-x-4" - aria-label="Toggle theme" - role="button" + aria-hidden="true" >
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
components/toggle-theme.tsx(1 hunks)
🔇 Additional comments (1)
components/toggle-theme.tsx (1)
42-42: Perfect fix for the theme icon sync issue.Changing the Sun icon to
opacity-0in the unmounted placeholder ensures both icons are hidden during hydration. This prevents the incorrect icon flash when users reload the page with a stored theme preference. After mounting, the correct icon appears immediately based onresolvedTheme.The solution is minimal, maintains DOM structure consistency for hydration, and aligns with next-themes SSR best practices.
🐛 Problem
compressed_Screen.Recording.2025-12-13.223816.mp4
Fixes #5
When users activate light mode and reload the page:
🔍 Root Cause
The placeholder component (shown during hydration) had a hardcoded Sun icon with
opacity-100that didn't reflect the stored theme preference in localStorage.✨ Solution
next-themesSSR/hydration strategy📁 Changes
opacity-100toopacity-0✅ Testing
Tested on Chrome, Firefox, and Safari:
📸 Screenshots
Before Fix
Light mode persisted, but toggle showed incorrect moon icon after reload.
After Fix - Light Mode
Sun icon correctly persists after reloading in light mode.
After Fix - Dark Mode
Moon icon correctly persists after reloading in dark mode.
Type: Bug Fix




Impact: User Experience
Stability: High - Minimal change, well-tested
Breaking Changes: None
Dependencies: No new dependencies added
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.