| import { Moon, Sun } from "lucide-react"; | |
| import { Button } from "./ui/button"; | |
| import { useEffect, useState } from "react"; | |
| export function ThemeToggle() { | |
| const [theme, setTheme] = useState("light"); | |
| useEffect(() => { | |
| const isDark = document.documentElement.classList.contains("dark"); | |
| setTheme(isDark ? "dark" : "light"); | |
| }, []); | |
| function toggleTheme() { | |
| const newTheme = theme === "light" ? "dark" : "light"; | |
| setTheme(newTheme); | |
| document.documentElement.classList.toggle("dark"); | |
| } | |
| return ( | |
| <Button | |
| variant="ghost" | |
| size="icon" | |
| onClick={toggleTheme} | |
| className="fixed top-4 right-4" | |
| > | |
| {theme === "light" ? ( | |
| <Moon className="h-5 w-5" /> | |
| ) : ( | |
| <Sun className="h-5 w-5" /> | |
| )} | |
| </Button> | |
| ); | |
| } | |