1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-26 13:58:25 -04:00
jarv.is/components/ThemeToggle/ThemeToggle.tsx
2022-01-22 10:06:24 -05:00

32 lines
917 B
TypeScript

import { useEffect, useState, memo } from "react";
import { useTheme } from "next-themes";
import BulbIcon from "./BulbIcon";
import styles from "./ThemeToggle.module.css";
type Props = {
className?: string;
};
const ThemeToggle = ({ className }: Props) => {
const [mounted, setMounted] = useState(false);
const { resolvedTheme, setTheme } = useTheme();
// render a dummy bulb until we're fully mounted and self-aware
useEffect(() => setMounted(true), []);
if (!mounted) return <BulbIcon on={false} className={className} />;
return (
<button
className={styles.button}
onClick={() => setTheme(resolvedTheme === "light" ? "dark" : "light")}
title={resolvedTheme === "light" ? "Toggle Dark Mode" : "Toggle Light Mode"}
aria-hidden={true}
>
<BulbIcon on={resolvedTheme === "light"} className={className} />
</button>
);
};
export default memo(ThemeToggle);