Skip to content
Open
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
30 changes: 20 additions & 10 deletions apps/web/src/components/theme-toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
"use client";

import { Button } from "./ui/button";
import { Sun, Moon } from "lucide-react";
import { useTheme } from "next-themes";
import { useEffect, useState } from "react";

Choose a reason for hiding this comment

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


interface ThemeToggleProps {
className?: string;
}

export function ThemeToggle({ className }: ThemeToggleProps) {
const { theme, setTheme } = useTheme();
const [isClient, setIsClient] = useState(false);

useEffect(() => {
setIsClient(true)
}, []) // this will see that the client side is rendered or not.

// if the client side is rendered then only render the button
return (
<Button
size="icon"
variant="text"
className="h-7"
onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
>
<Sun className="!size-[1.1rem]" />
<span className="sr-only">{theme === "dark" ? "Light" : "Dark"}</span>
</Button>
<>
{
isClient && <Button
size="icon"
variant="text"
className="h-7"
onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
>
{theme === "dark" ? <Sun className="!size-[1.1rem]" /> : <Moon className="!size-[1.1rem]" />}
{/* <span className="sr-only">{theme === "dark" ? "Light" : "Dark"}</span> */}

Choose a reason for hiding this comment

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

i forgot to add it we can uncomment and add it it will not cause any error actually i studied from stackoverflow useTheme from next themes is causing hydration warning we can use suppressHydrationWarning or use with useEffect and useState which will both result in same things. so we can uncomment the span

</Button>
}
</>
);
}