2024-07-01 18:19:20 +00:00
|
|
|
"use client";
|
|
|
|
|
2024-07-25 03:25:01 +00:00
|
|
|
import React, { useState, useEffect } from "react";
|
2024-07-01 18:19:20 +00:00
|
|
|
import { useTheme } from "next-themes";
|
2024-07-25 03:25:01 +00:00
|
|
|
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
|
|
|
|
2024-07-25 03:25:01 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setMounted(true);
|
|
|
|
setIsDark(theme === "dark");
|
|
|
|
}, [theme]);
|
2024-07-01 18:19:20 +00:00
|
|
|
|
2024-07-25 03:25:01 +00:00
|
|
|
if (!mounted) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const toggleDarkMode = () => {
|
|
|
|
setIsDark(!isDark);
|
|
|
|
setTheme(theme === "light" ? "dark" : "light");
|
|
|
|
};
|
2024-07-01 18:19:20 +00:00
|
|
|
|
|
|
|
return (
|
2024-07-25 03:25:01 +00:00
|
|
|
<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
|
|
|
);
|
|
|
|
}
|
2024-07-25 03:25:01 +00:00
|
|
|
|
|
|
|
export default DarkToggle;
|