0
0
mirror of https://github.com/yude-jp/yude.jp synced 2024-11-02 00:18:00 +09:00
yude.jp/pages/components/ThemeSelector.js

30 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-05-21 18:36:19 +09:00
import "tailwindcss/tailwind.css";
import { useRouter } from 'next/router'
import React, { useEffect, useState } from 'react'
import { useTheme } from "next-themes";
2021-06-22 12:08:26 +09:00
import { faMoon, faSun } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
2021-05-21 18:36:19 +09:00
2021-06-22 12:08:26 +09:00
const ThemeSelector = (props) => {
2021-05-21 18:36:19 +09:00
const { title, children } = props
const [isMounted, setIsMounted] = useState(false);
2021-05-22 10:23:46 +09:00
const { theme, setTheme, getTheme } = useTheme();
useEffect(() => {
setIsMounted(true);
}, []);
2021-05-22 10:23:46 +09:00
const switchTheme = () => {
if (isMounted) {
setTheme(theme === "light" ? "dark" : "light");
}
};
2021-05-21 18:36:19 +09:00
return (
2021-06-22 12:39:51 +09:00
<button className="inline-flex rounded-md border border-gray-300 dark:border-gray-800 shadow-sm px-2 bg-white font-medium text-gray-700 hover:bg-gray-50 dark:bg-gray-700 dark:text-white my-3 py-1 text-2xl focus:outline-none" onClick={switchTheme}>
{theme !== undefined && (theme === "light" ? (
2021-06-22 12:39:51 +09:00
<FontAwesomeIcon icon={faMoon} className="w-10 h-7" />
2021-05-22 10:23:46 +09:00
) : (
2021-06-22 12:39:51 +09:00
<FontAwesomeIcon icon={faSun} className="w-10 h-7" />
))}
2021-05-22 10:23:46 +09:00
</button>
2021-05-21 18:36:19 +09:00
)
}
2021-06-22 12:08:26 +09:00
export default ThemeSelector