mirror of
https://github.com/hex248/tsos.git
synced 2026-02-08 02:33:03 +00:00
theme toggle
This commit is contained in:
35
src/components/theme-toggle.tsx
Normal file
35
src/components/theme-toggle.tsx
Normal 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;
|
||||
Reference in New Issue
Block a user