theme toggle

This commit is contained in:
Oliver Bryan
2026-01-07 12:32:16 +00:00
parent 019b8545f1
commit 5d2ee6cc13
4 changed files with 42 additions and 4 deletions

View File

@@ -0,0 +1,35 @@
import { Moon, Sun } from "lucide-react";
import { useEffect, useState } from "react";
function ThemeToggle() {
const [theme, setTheme] = useState<string | null>();
useEffect(() => {
const savedTheme = localStorage.getItem("theme");
if (savedTheme) {
setTheme(savedTheme);
}
}, []);
function updateTheme(newTheme: string) {
setTheme(newTheme);
localStorage.setItem("theme", newTheme);
document.documentElement.classList.toggle("dark");
}
return (
<button
type="button"
className="rounded cursor-pointer"
onClick={() => {
if (!theme || theme === "light") updateTheme("dark");
else updateTheme("light");
}}
>
{theme === "dark" ? <Sun /> : <Moon />}
</button>
);
}
export default ThemeToggle;