mirror of
https://github.com/Xevion/banner.git
synced 2026-01-31 08:23:35 -06:00
37 lines
941 B
TypeScript
37 lines
941 B
TypeScript
import { Button } from "@radix-ui/themes";
|
|
import { Monitor, Moon, Sun } from "lucide-react";
|
|
import { useTheme } from "next-themes";
|
|
import { useMemo } from "react";
|
|
|
|
export function ThemeToggle() {
|
|
const { theme, setTheme } = useTheme();
|
|
|
|
const nextTheme = useMemo(() => {
|
|
switch (theme) {
|
|
case "light":
|
|
return "dark";
|
|
case "dark":
|
|
return "system";
|
|
case "system":
|
|
return "light";
|
|
default:
|
|
console.error(`Invalid theme: ${theme}`);
|
|
return "system";
|
|
}
|
|
}, [theme]);
|
|
|
|
const icon = useMemo(() => {
|
|
if (nextTheme === "system") {
|
|
return <Monitor size={18} />;
|
|
}
|
|
return nextTheme === "dark" ? <Moon size={18} /> : <Sun size={18} />;
|
|
}, [nextTheme]);
|
|
|
|
return (
|
|
<Button variant="ghost" size="3" onClick={() => setTheme(nextTheme)} className="theme-toggle">
|
|
{icon}
|
|
<span className="sr-only">Toggle theme</span>
|
|
</Button>
|
|
);
|
|
}
|