@@ -1,34 +1,28 @@
|
||||
import useLocalSettingsStore from "@/store/localSettings";
|
||||
import { useEffect, useState, ChangeEvent } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslation } from "next-i18next";
|
||||
import clsx from "clsx";
|
||||
|
||||
type Props = {
|
||||
className?: string;
|
||||
align?: "left" | "right";
|
||||
};
|
||||
|
||||
export default function ToggleDarkMode({ className, align }: Props) {
|
||||
export default function ToggleDarkMode({ className }: Props) {
|
||||
const { t } = useTranslation();
|
||||
const { settings, updateSettings } = useLocalSettingsStore();
|
||||
|
||||
const [theme, setTheme] = useState<string | null>(
|
||||
localStorage.getItem("theme")
|
||||
);
|
||||
const [theme, setTheme] = useState(localStorage.getItem("theme"));
|
||||
|
||||
const handleToggle = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
const handleToggle = (e: any) => {
|
||||
setTheme(e.target.checked ? "dark" : "light");
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (theme) {
|
||||
updateSettings({ theme });
|
||||
}
|
||||
updateSettings({ theme: theme as string });
|
||||
}, [theme]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={clsx("tooltip", align ? `tooltip-${align}` : "tooltip-bottom")}
|
||||
className="tooltip tooltip-bottom"
|
||||
data-tip={t("switch_to", {
|
||||
theme: settings.theme === "light" ? "Dark" : "Light",
|
||||
})}
|
||||
@@ -40,7 +34,7 @@ export default function ToggleDarkMode({ className, align }: Props) {
|
||||
type="checkbox"
|
||||
onChange={handleToggle}
|
||||
className="theme-controller"
|
||||
checked={theme === "dark"}
|
||||
checked={localStorage.getItem("theme") === "light" ? false : true}
|
||||
/>
|
||||
<i className="bi-sun-fill text-xl swap-on"></i>
|
||||
<i className="bi-moon-fill text-xl swap-off"></i>
|
||||
|
||||
Reference in New Issue
Block a user