mirror of
https://github.com/vercel/ai-elements.git
synced 2026-09-14 19:48:26 +08:00
36 lines
861 B
TypeScript
36 lines
861 B
TypeScript
"use client";
|
|
|
|
import { Button } from "@repo/shadcn-ui/components/ui/button";
|
|
import { MoonIcon, SunIcon } from "lucide-react";
|
|
import { useTheme } from "next-themes";
|
|
import { useEffect, useState } from "react";
|
|
|
|
export const ThemeToggle = () => {
|
|
const { resolvedTheme, setTheme } = useTheme();
|
|
const [mounted, setMounted] = useState(false);
|
|
|
|
useEffect(() => {
|
|
setMounted(true);
|
|
}, []);
|
|
|
|
const handleClick = () => {
|
|
setTheme(resolvedTheme === "dark" ? "light" : "dark");
|
|
};
|
|
|
|
if (!mounted) {
|
|
return (
|
|
<Button size="icon-sm" type="button" variant="ghost">
|
|
<div className="size-4" />
|
|
</Button>
|
|
);
|
|
}
|
|
|
|
const Icon = resolvedTheme === "dark" ? MoonIcon : SunIcon;
|
|
|
|
return (
|
|
<Button onClick={handleClick} size="icon-sm" type="button" variant="ghost">
|
|
<Icon className="size-4" />
|
|
</Button>
|
|
);
|
|
};
|