e4ca70d758
The ThemeToggle component was causing hydration errors because the server and client rendered different icons (Sun/Moon) based on the theme state. Changes: - Render a placeholder button until component is mounted - Only render the actual theme icon after client-side hydration - This ensures server and client HTML match during initial render Fixes console error: 'Expected server HTML to contain a matching <circle> in <svg>'
43 lines
965 B
TypeScript
43 lines
965 B
TypeScript
"use client";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import { Moon, Sun } from "lucide-react";
|
|
import { useTheme } from "next-themes";
|
|
import { useEffect, useState } from "react";
|
|
|
|
export default function ThemeToggle() {
|
|
const { resolvedTheme, setTheme } = useTheme();
|
|
const [mounted, setMounted] = useState(false);
|
|
|
|
useEffect(() => setMounted(true), []);
|
|
|
|
if (!mounted) {
|
|
return (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
aria-label="Toggle theme"
|
|
disabled
|
|
>
|
|
<span className="h-5 w-5" />
|
|
</Button>
|
|
);
|
|
}
|
|
|
|
const isDark = resolvedTheme === "dark";
|
|
const toggle = () => setTheme(isDark ? "light" : "dark");
|
|
|
|
return (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
aria-label="Toggle theme"
|
|
onClick={toggle}
|
|
>
|
|
{isDark ? <Sun className="h-5 w-5" /> : <Moon className="h-5 w-5" />}
|
|
<span className="sr-only">Toggle theme</span>
|
|
</Button>
|
|
);
|
|
}
|
|
|