Fix: The light/dark mode toggle is behaving strangely when the page is reloaded#21
Fix: The light/dark mode toggle is behaving strangely when the page is reloaded#21rishabh30Gupta wants to merge 1 commit intoStabilityNexus:mainfrom
Conversation
- Fixed hydration issue where toggle showed wrong icon after reload - Component now waits for resolvedTheme to be available before rendering - Prevents flash of incorrect icon during next-themes initialization - Resolves StabilityNexus#5
WalkthroughThe toggle-theme component's hydration guard is updated to defer rendering until both the component is mounted and Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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)
34-54: Optional: Consider adjusting placeholder semantics for screen readers.The placeholder has
role="button"andaria-labelbut no click handler, which could be briefly confusing for assistive technology. Since this state is very short-lived, the impact is minimal.If you'd like to improve it, consider either:
- Removing
role="button"from the placeholder, or- Adding
aria-busy="true"to signal loading state<div className="flex items-center space-x-4" aria-label="Toggle theme" - role="button" + aria-busy="true" >
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
components/toggle-theme.tsx(3 hunks)
🔇 Additional comments (1)
components/toggle-theme.tsx (1)
32-33: LGTM! Core fix correctly addresses the hydration timing issue.The updated guard ensures the interactive toggle waits for
resolvedThemeto be available from localStorage, preventing the wrong icon from displaying after reload. The added comment clearly explains the intention.Minor note: The placeholder (lines 40-47) always shows the sun icon, so dark-mode users may see a brief light-mode flash during hydration. This is likely acceptable given how brief the delay typically is with next-themes.
Description
Fixes the theme toggle displaying the wrong icon after page reload.
Problem
When the page was reloaded while in light mode, the toggle would incorrectly show a moon icon instead of the expected sun icon, even though the page correctly remained in light mode.
Root Cause
The issue was a hydration timing problem with
next-themes. The component would render beforeresolvedThemewas available from localStorage, causing a brief moment where the wrong icon was displayed.Solution
Modified components/toggle-theme.tsx to wait for both
mountedstate andresolvedThemeto be available before rendering the interactive toggle. This ensures the correct icon is always displayed after hydration.Changes
if (!mounted)toif (!mounted || !resolvedTheme)Testing
Tested light mode reload - toggle correctly shows sun icon
Tested dark mode reload - toggle correctly shows moon icon
Theme persistence works correctly across reloads
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.