Skip to content

Instantly share code, notes, and snippets.

@kjmczk
Created May 30, 2021 05:23
Show Gist options
  • Save kjmczk/0902a150c0eca181b42a7a474d7c7192 to your computer and use it in GitHub Desktop.
Save kjmczk/0902a150c0eca181b42a7a474d7c7192 to your computer and use it in GitHub Desktop.
components/theme-switch.tsx - MDX Blog with Dark Mode - Medium
// components/theme-switch.tsx
import { useState, useEffect } from 'react';
import { useTheme } from 'next-themes';
import Switch from 'react-switch';
import { IconContext } from 'react-icons';
import { FaMoon, FaSun } from 'react-icons/fa';
const ThemeSwitch: React.FC = () => {
const { theme, setTheme } = useTheme();
const dark = theme === 'dark' ? true : false;
const [checked, setChecked] = useState(dark);
const [mounted, setMounted] = useState(false);
const handleChange = (nextChecked: boolean) => {
setChecked(nextChecked);
};
// When mounted on client, now we can show the UI
useEffect(() => setMounted(true), []);
useEffect(() => {
setTheme(checked ? 'dark' : 'light');
}, [checked, setTheme]);
if (!mounted) return null;
return (
<Switch
onChange={handleChange}
checked={checked}
aria-label="switch between day and night themes"
offColor="#555"
onHandleColor="#eee"
handleDiameter={20}
uncheckedIcon={
<div className="flex justify-center items-center h-full">
<IconContext.Provider
value={{
color: 'gold',
size: '80%',
}}
>
<FaSun />
</IconContext.Provider>
</div>
}
checkedIcon={
<div className="flex justify-center items-center h-full">
<IconContext.Provider
value={{
color: 'yellow',
size: '80%',
}}
>
<FaMoon />
</IconContext.Provider>
</div>
}
height={24}
width={48}
/>
);
};
export default ThemeSwitch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment