49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import { useTheme } from "next-themes";
|
|
import { MoonIcon, SunIcon } from "lucide-react";
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
export function ThemeToggle() {
|
|
const { resolvedTheme, setTheme } = useTheme();
|
|
const mounted = React.useSyncExternalStore(
|
|
() => () => {},
|
|
() => true,
|
|
() => false
|
|
);
|
|
|
|
if (!mounted) {
|
|
return (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
aria-label="Toggle theme"
|
|
className="text-muted-foreground"
|
|
>
|
|
<span className="size-4" />
|
|
</Button>
|
|
);
|
|
}
|
|
|
|
const isDark = resolvedTheme === "dark";
|
|
|
|
return (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
onClick={() => setTheme(isDark ? "light" : "dark")}
|
|
aria-label="Toggle color theme"
|
|
title={isDark ? "切换为浅色模式" : "切换为深色模式"}
|
|
className="text-muted-foreground hover:text-foreground transition-colors"
|
|
>
|
|
{isDark ? (
|
|
<SunIcon data-icon="inline-start" />
|
|
) : (
|
|
<MoonIcon data-icon="inline-start" />
|
|
)}
|
|
</Button>
|
|
);
|
|
}
|
|
|