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

@@ -1,8 +1,11 @@
import ThemeToggle from "./components/theme-toggle";
function Index() {
return (
<div className="flex flex-col h-[100vh] items-center">
<header className="w-full flex items-center justify-center border-b h-12">
<header className="w-full flex items-center justify-between border-b h-12 p-2">
<h1 className="text-3xl font-bold">The Shape of Sound</h1>
<ThemeToggle />
</header>
<main className="w-full flex-1 p-4 flex items-center justify-center">
<div className="w-64 h-64 bg-orange-600 rounded-full" />

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;