Skip to content

ThemeToggle briefly shows the wrong icon on first paint because theme state initializes to "light" before the correcting effect runs #208

Closed

Description

@chonilius

src/context/ThemeContext.tsx#ThemeProvider initializes React state unconditionally:

const [theme, setTheme] = useState<Theme>("light");

The actual applied theme (dark or light) is decided synchronously before hydration by
the inline themeInitScript in layout.tsx, which adds the dark class straight to
<html>. ThemeProvider's useEffect then reads that class back to correct its own
theme state:

useEffect(() => {
  setTheme(document.documentElement.classList.contains("dark") ? "dark" : "light");
  ...
}, []);

Between first paint and this effect running, theme is "light" regardless of the
visitor's actual preference. src/components/ui/ThemeToggle.tsx renders its icon
directly from this state:

{theme === "dark" ? <Sun className="h-4 w-4" /> : <Moon className="h-4 w-4" />}

For a visitor whose page is actually rendering in dark mode (via a stored preference or
prefers-color-scheme: dark), the toggle button shows a Moon icon (meaning "click to
go dark") for one render/paint even though the page is already dark — the icon is
momentarily backwards relative to the real state, a small but visible flash on every
page load in dark mode.

Suggested fix: read the initial theme synchronously via a lazy useState initializer
(useState<Theme>(() => document.documentElement.classList.contains("dark") ? "dark" : "light"))
instead of hardcoding "light" and correcting it after the fact — safe here since
ThemeProvider is already a Client Component executing after the theme-init script
has run.

Activity

  1. added
    bug
    very hard
    help wanted
    enhancement
    on Aug 21, 2026
  2. added
    Stellar Wave
    on Aug 24, 2026
  3. codewithzubair07 commented on Aug 24, 2026

    @codewithzubair07

    @codewithzubair07 has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    i would like to work on this issue

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @codewithzubair07 to this issue.

  4. kelvin1738-sys commented on Aug 24, 2026

    @kelvin1738-sys

    @kelvin1738-sys has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Hello, This issue aligns well with my experience and technical skills. I'm confident I can resolve it effectively and would appreciate being assigned to it.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @kelvin1738-sys to this issue.

  5. onyekachi66 commented on Aug 24, 2026

    @onyekachi66

    @onyekachi66 has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    I would like to work on this issue having worked on similar issues in the past. Please assign

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @onyekachi66 to this issue.

  6. Genesis000001 commented on Aug 24, 2026

    @Genesis000001

    @Genesis000001 has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Hi, I’ve carefully gone through the issue details and fully understand the requirements. I’m confident I can deliver a clean, efficient solution that meets the acceptance criteria. Please feel free to assign it to me.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Genesis000001 to this issue.

  7. KingDavid9999 commented on Aug 24, 2026

    @KingDavid9999

    @KingDavid9999 has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Hi maintainer, I'd very much appreciate the opportunity to work on this issue.
    Having reviewed the acceptance criteria, I'm confident that I'd be able to have a PR up shortly after being assigned.
    Kindly assign this issue to me.
    Thank you!

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @KingDavid9999 to this issue.

  8. abdool002 commented on Aug 24, 2026

    @abdool002

    @abdool002 has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Hello Maintainer, I’ve reviewed the issue and understand the requirements. I’m confident I can handle it effectively. Kindly assign it to me.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @abdool002 to this issue.

  9. devNuel commented on Aug 24, 2026

    @devNuel

    @Yotwesten has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Hi Maintainer, I’m very interested in working on this issue. I have experience handling similar tasks and am confident I can contribute effectively. I’m ready to get started right away.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Yotwesten to this issue.

  10. johdanike commented on Aug 24, 2026

    @johdanike

    @johdanike has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    I can pick this up! I will fix the hydration flash in ThemeContext.tsx by using a lazy useState initializer to synchronously read the applied theme class from the document before the first paint. Having architected Next.js applications and managed complex React component lifecycles, I can seamlessly resolve this layout state mismatch. Let me know if I'm good to open a PR!

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @johdanike to this issue.

  11. richardtoms100 commented on Aug 24, 2026

    @richardtoms100
    Contributor

    @richardtoms100 has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    I'm ready to take on this issue now

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @richardtoms100 to this issue.

  12. olubukolatanko209-doc commented on Aug 24, 2026

    @olubukolatanko209-doc

    @olubukolatanko209-doc has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Good day maintainer, Please I would like to work on this issue.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @olubukolatanko209-doc to this issue.

  13. drips-wave commented on Aug 24, 2026

    @drips-wave

    Congratulations, @richardtoms100! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on August 31, 2026.

    🧑‍💻 @richardtoms100: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

    Warning

    When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

    🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

    🌊 Happy Wave 🌊

  14. grantfox-oss commented on Aug 25, 2026

    @grantfox-oss

    🎉 This issue has been marked as completed on GrantFox as part of the Third Campaign campaign!

    @richardtoms100's PR #322 was approved and merged by @chonilius.

    🏆 @richardtoms100: You earned 35 FoxPoints for this contribution! Your current tier: Explorer (156 total points). Track your full progress on GrantFox.

    👏 Great work, @richardtoms100! Keep contributing to MergeFi.

  15. drips-wave commented on Aug 25, 2026

    @drips-wave

    This issue has been completed by @richardtoms100 as part of the Stellar Wave Program's 8th Wave 🥳

    😎 @richardtoms100: You earned 200 Points for completing this issue! After the current Wave ends, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.

    🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

  16. removed
    Stellar Wave
    on Oct 15, 2026
  17. added
    Stellar Wave
    on Oct 15, 2026
  18. removed
    Stellar Wave
    on Oct 15, 2026
  19. added
    Stellar Wave
    on Oct 15, 2026
  20. removed
    Stellar Wave
    on Oct 15, 2026
  21. added
    Stellar Wave
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Stellar Wavebugenhancementhelp wantedvery hard

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions