daphnis/components/darkmode.tsx

47 lines
1004 B
TypeScript
Raw Permalink Normal View History

2024-07-01 18:19:20 +00:00
"use client";
import React, { useState, useEffect } from "react";
2024-07-01 18:19:20 +00:00
import { useTheme } from "next-themes";
import { Moon, Sun } from "lucide-react";
function DarkToggle() {
const [mounted, setMounted] = useState(false);
const [isDark, setIsDark] = useState(false);
const { theme, setTheme } = useTheme();
2024-07-01 18:19:20 +00:00
useEffect(() => {
setMounted(true);
setIsDark(theme === "dark");
}, [theme]);
2024-07-01 18:19:20 +00:00
if (!mounted) {
return null;
}
const toggleDarkMode = () => {
setIsDark(!isDark);
setTheme(theme === "light" ? "dark" : "light");
};
2024-07-01 18:19:20 +00:00
return (
<button
onClick={toggleDarkMode}
className="flex items-center focus:outline-none"
>
{isDark ? (
<div className="mr-2 text-2xl text-yellow-400">
<Moon />
</div>
) : (
<div className="mr-2 text-2xl text-gray-700">
<span className="text-yellow-400">
<Sun />
</span>
</div>
)}
</button>
2024-07-01 18:19:20 +00:00
);
}
export default DarkToggle;