el.xwx.moe/components/ToggleDarkMode.tsx

41 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-11-24 06:50:16 -06:00
import useLocalSettingsStore from "@/store/localSettings";
2023-11-24 02:06:33 -06:00
import { useEffect, useState } from "react";
2023-08-02 12:53:55 -05:00
2023-11-16 02:22:16 -06:00
type Props = {
className?: string;
};
export default function ToggleDarkMode({ className }: Props) {
2023-11-29 14:17:51 -06:00
const { settings, updateSettings } = useLocalSettingsStore();
2023-11-24 06:50:16 -06:00
const [theme, setTheme] = useState(localStorage.getItem("theme"));
2023-08-02 12:53:55 -05:00
2023-11-24 02:06:33 -06:00
const handleToggle = (e: any) => {
2023-12-16 08:46:29 -06:00
setTheme(e.target.checked ? "dark" : "light");
2023-08-02 12:53:55 -05:00
};
2023-11-24 02:06:33 -06:00
useEffect(() => {
2023-11-24 06:50:16 -06:00
updateSettings({ theme: theme as string });
2023-11-24 02:06:33 -06:00
}, [theme]);
2023-08-02 12:53:55 -05:00
return (
2023-11-29 14:17:51 -06:00
<div
className="tooltip tooltip-bottom"
data-tip={`Switch to ${settings.theme === "light" ? "Dark" : "Light"}`}
2023-11-27 15:38:38 -06:00
>
2023-11-29 14:17:51 -06:00
<label
className={`swap swap-rotate btn-square text-neutral btn btn-ghost btn-sm ${className}`}
2023-11-24 02:06:33 -06:00
>
2023-11-29 14:17:51 -06:00
<input
type="checkbox"
onChange={handleToggle}
className="theme-controller"
checked={localStorage.getItem("theme") === "light" ? false : true}
/>
2023-12-17 22:32:33 -06:00
<i className="bi-sun-fill text-xl swap-on"></i>
<i className="bi-moon-fill text-xl swap-off"></i>
2023-11-29 14:17:51 -06:00
</label>
</div>
2023-08-02 12:53:55 -05:00
);
}