Files
hapi/website/src/components/ModeToggle.tsx
T
weishu 77a2eee236 site: Add official website source
- Set VitePress base to '/docs/' for serving documentation at /docs/ route
- Update favicon path in head config to '/docs/favicon.ico'
- Add build:site script to build website and docs, then merge outputs
2026-01-01 12:48:45 +08:00

32 lines
1.1 KiB
TypeScript

import { Moon, Sun } from "lucide-react"
import { Button } from "@/components/ui/button"
import { useTheme } from "@/contexts/ThemeContext"
export function ModeToggle() {
const { theme, setTheme } = useTheme()
const toggleTheme = () => {
// 如果当前是 system,则根据系统偏好决定切换到哪个模式
if (theme === "system") {
const systemIsDark = window.matchMedia("(prefers-color-scheme: dark)").matches
setTheme(systemIsDark ? "light" : "dark")
} else {
setTheme(theme === "dark" ? "light" : "dark")
}
}
return (
<Button
variant="outline"
size="icon"
onClick={toggleTheme}
className="border-2 border-border shadow-hard hover:translate-y-0.5 hover:shadow-none transition-all"
title="Toggle theme"
>
<Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
<Moon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
<span className="sr-only">Toggle theme</span>
</Button>
)
}