Files
DefectingCat.github.io/components/DarkModeBtn.tsx
DefectingCat 047fc0c775 Add new post
add new code sandbox component
2022-08-12 11:01:18 +08:00

39 lines
929 B
TypeScript

import classNames from 'classnames';
import useMounted from 'lib/hooks/useMounted';
import { useTheme } from 'next-themes';
import { FiMoon, FiSun } from 'react-icons/fi';
const DarkModeBtn = () => {
const { mounted } = useMounted();
const { systemTheme, theme, setTheme } = useTheme();
const currentTheme = theme === 'system' ? systemTheme : theme;
if (!mounted)
return (
<button>
<div
className={classNames(
'w-5 h-5 rounded-md animate-pulse',
'bg-gray-300 dark:bg-gray-500'
)}
></div>
</button>
);
return (
<>
{currentTheme === 'dark' ? (
<button>
<FiSun className="w-5 h-5" onClick={() => setTheme('light')} />
</button>
) : (
<button>
<FiMoon className="w-5 h-5" onClick={() => setTheme('dark')} />
</button>
)}
</>
);
};
export default DarkModeBtn;