OpenCut icon indicating copy to clipboard operation
OpenCut copied to clipboard

Fix hydration error

Open SamSyntax opened this issue 1 year ago • 3 comments

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Fixes #629 #635 #621 This is a really tiny modification that fixes hydration error mentioned in #629, #635 and #621

CustomThemeProvider just wraps the ThemeProvider from next-themes and uses useState to set mounted state as false initially and then calls useEffect that runs only once on the initial load to set mounted to true. Since it's a client-side hook, it does not run on server so we receive a neutral html from server without theme mismatch. I believe this is a common pattern in working with next-themes, but if anyone knows a better way to handle that I would love to know!

This pr also adds the theme icon change on toggle.

Type of change

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [x] Code refactoring

How Has This Been Tested?

This change has been tested on on both Chromium browser and Zen browser which is using firefox engine under the hood.

Test Configuration:

  • Node version: v23.11.1
  • Browser (if applicable): Chromium, Zen
  • Operating System: Arch Linux

Screenshots (if applicable)

image

Checklist:

  • [x] My code follows the style guidelines of this project
  • [x] I have performed a self-review of my code
  • [x] I have added screenshots if ui has been changed
  • [ ] I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [x] My changes generate no new warnings
  • [ ] I have added tests that prove my fix is effective or that my feature works
  • [ ] New and existing unit tests pass locally with my changes
  • [ ] Any dependent changes have been merged and published in downstream modules

Summary by CodeRabbit

  • New Features

    • Adds a hydration-safe custom theme provider to improve theming reliability on initial load.
  • Bug Fixes

    • Prevents hydration mismatches by deferring theme application until after mount.
  • Style

    • Theme toggle now displays contextual icons (sun in dark mode, moon in light mode).

SamSyntax avatar Nov 01 '25 12:11 SamSyntax

Deploy request for appcut pending review.

Visit the deploys page to approve it

Name Link
Latest commit f8bfbbb335ec4b7fd699c2a6936eca794f13a42b

netlify avatar Nov 01 '25 12:11 netlify

@SamSyntax is attempting to deploy a commit to the OpenCut OSS Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Nov 01 '25 12:11 vercel[bot]

Walkthrough

Replaces the app ThemeProvider with a new hydration-safe CustomThemeProvider, adds the CustomThemeProvider implementation that defers applying ThemeProvider until client mount, and updates the theme toggle to render Sun or Moon based on current theme.

Changes

Cohort / File(s) Summary
Layout integration
apps/web/src/app/layout.tsx
Swapped import and JSX usage of ThemeProvider to CustomThemeProvider; props forwarded unchanged.
Theme provider implementation
apps/web/src/components/theme-provider.tsx
New CustomThemeProvider component and CustomThemeProviderProps interface; uses useEffect to set mounted state and delays rendering ThemeProvider until after mount to avoid hydration mismatches.
Theme toggle UI
apps/web/src/components/theme-toggle.tsx
Adjusted icon rendering: shows Sun when theme is "dark", otherwise Moon. Click/toggle behavior unchanged; component signature unchanged.

Sequence Diagram

sequenceDiagram
    participant Browser as Browser (SSR/CSR)
    participant Layout as layout.tsx
    participant Custom as CustomThemeProvider
    participant Theme as ThemeProvider

    Browser->>Layout: Receive server-rendered HTML
    Layout->>Custom: Render CustomThemeProvider (mounted=false)
    Custom-->>Browser: Return children only (no ThemeProvider) to match SSR

    Browser->>Custom: Client mount triggers useEffect
    Custom->>Custom: set mounted = true
    Custom->>Theme: Render ThemeProvider with provided props + children
    Theme-->>Browser: Theme context applied on client

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review useEffect mounting logic for edge cases and unnecessary re-renders.
  • Verify theme persistence and first-paint behavior (possible flicker) across browsers.
  • Confirm ThemeToggle icon logic aligns with accessibility labels and styling conventions.

Poem

🐰 I nibbled code where themes take flight,
Waiting to mount before the light.
A gentle wrap, no server fight,
Sun or Moon appears just right. ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix hydration error' is highly specific and directly related to the main change—adding a CustomThemeProvider to resolve hydration mismatches in Next.js theming.
Description check ✅ Passed The description follows the template structure with all major sections completed: issue references, detailed explanation, type of change selected, testing information provided, screenshots included, and comprehensive checklist.
Linked Issues check ✅ Passed The PR addresses the hydration error from #629 by implementing a deferred ThemeProvider pattern, directly solving the server/client mismatch problem reported across the linked issues.
Out of Scope Changes check ✅ Passed All changes are scope-focused: CustomThemeProvider wrapper to fix hydration errors, theme icon toggle change, and layout updates to use the new provider—all directly address the linked issues.
✨ Finishing touches
  • [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

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

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Nov 01 '25 12:11 coderabbitai[bot]