Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/web/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ThemeProvider } from "next-themes";
import { Analytics } from "@vercel/analytics/react";
import Script from "next/script";
import "./globals.css";
Expand All @@ -10,6 +9,7 @@ import { baseMetaData } from "./metadata";
import { defaultFont } from "../lib/font-config";
import { BotIdClient } from "botid/client";
import { env } from "@/env";
import CustomThemeProvider from "@/components/theme-provider";

export const metadata = baseMetaData;

Expand All @@ -35,7 +35,7 @@ export default function RootLayout({
<BotIdClient protect={protectedRoutes} />
</head>
<body className={`${defaultFont.className} font-sans antialiased`}>
<ThemeProvider attribute="class" defaultTheme="dark">
<CustomThemeProvider attribute="class" defaultTheme="dark">
<TooltipProvider>
<StorageProvider>
<ScenesMigrator>{children}</ScenesMigrator>
Expand All @@ -55,7 +55,7 @@ export default function RootLayout({
data-track-sessions={false}
/>
</TooltipProvider>
</ThemeProvider>
</CustomThemeProvider>
</body>
</html>
);
Expand Down
21 changes: 21 additions & 0 deletions apps/web/src/components/theme-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use client"

import { ThemeProvider, ThemeProviderProps } from "next-themes";
import { useEffect, useState } from "react"

interface CustomThemeProviderProps extends ThemeProviderProps {
children: React.ReactNode;
}

export default function CustomThemeProvider({ children, ...props }: CustomThemeProviderProps) {
const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
}, []);

if (!mounted) {
return <>{children}</>;
}
Comment on lines +10 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Missing ThemeProvider context causes runtime errors for child components.

Returning <>{children}</> without the ThemeProvider wrapper when !mounted (lines 16-18) breaks the theme context. Components like Toaster (which calls useTheme() as shown in the relevant snippet from sonner.tsx) will throw an error during the initial render: "useTheme must be used within a ThemeProvider".

The fix for hydration errors should keep the ThemeProvider wrapped at all times. The suppressHydrationWarning attribute on the <html> tag (already present in layout.tsx line 33) handles theme-related hydration mismatches.

Apply this diff to fix the context issue:

 export default function CustomThemeProvider({ children, ...props }: CustomThemeProviderProps) {
   const [mounted, setMounted] = useState(false);
   useEffect(() => {
     setMounted(true);
   }, []);

-  if (!mounted) {
-    return <>{children}</>;
-  }
-
-  return <ThemeProvider {...props}>{children}</ThemeProvider >
+  return (
+    <ThemeProvider {...props}>
+      {mounted ? children : null}
+    </ThemeProvider>
+  );
 }

Alternatively, if you want to avoid the layout shift from returning null, simply always render children within ThemeProvider and rely on suppressHydrationWarning:

 export default function CustomThemeProvider({ children, ...props }: CustomThemeProviderProps) {
-  const [mounted, setMounted] = useState(false);
-  useEffect(() => {
-    setMounted(true);
-  }, []);
-
-  if (!mounted) {
-    return <>{children}</>;
-  }
-
-  return <ThemeProvider {...props}>{children}</ThemeProvider >
+  return <ThemeProvider {...props}>{children}</ThemeProvider>;
 }

The second approach is simpler and leverages Next.js's built-in hydration handling via suppressHydrationWarning.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In apps/web/src/components/theme-provider.tsx around lines 10 to 18, the
component currently returns <>children</> when !mounted which removes the
ThemeProvider and causes runtime errors (e.g., "useTheme must be used within a
ThemeProvider"); remove the early return and always render the ThemeProvider
wrapper around children (i.e., render ThemeProvider regardless of mounted state)
to preserve theme context during initial render and rely on the existing
suppressHydrationWarning in layout.tsx to handle hydration mismatches.


return <ThemeProvider {...props}>{children}</ThemeProvider >
}
4 changes: 3 additions & 1 deletion apps/web/src/components/theme-toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function ThemeToggle({ className }: ThemeToggleProps) {
className="h-7"
onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
>
<Sun className="!size-[1.1rem]" />
{theme === "dark" ? (
<Sun className="!size-[1.1rem]" />
) : (<Moon className="!size[1.1rem]" />)}
<span className="sr-only">{theme === "dark" ? "Light" : "Dark"}</span>
</Button>
);
Expand Down
Loading