1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 04:58:26 -04:00
jarv.is/components/ThemeToggle/ThemeToggle.tsx
Jake Jarvis 283eb62446
less corny header and note meta icons (#746)
* less corny header and note meta icons

* swap out more twemojis

* indicate active page in nav bar

* update favicons

* extract `<MenuLink />` into its own component

* change hover effect to an underline

* cropped header photo
2022-01-27 10:06:26 -05:00

42 lines
1.1 KiB
TypeScript

import { useEffect, useState, memo } from "react";
import { useTheme } from "next-themes";
import classNames from "classnames";
import { SunIcon, MoonIcon } from "../Icons";
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 button until we're fully mounted and self-aware
useEffect(() => setMounted(true), []);
if (!mounted) {
return (
<button className={styles.button} aria-hidden={true}>
<SunIcon className={classNames("icon", className)} />
</button>
);
}
return (
<button
className={styles.button}
onClick={() => setTheme(resolvedTheme === "light" ? "dark" : "light")}
title={resolvedTheme === "light" ? "Toggle Dark Mode" : "Toggle Light Mode"}
>
{resolvedTheme === "light" ? (
<SunIcon className={classNames("icon", className)} />
) : (
<MoonIcon className={classNames("icon", className)} />
)}
</button>
);
};
export default memo(ThemeToggle);