2020-09-20 10:33:39 +02:00
|
|
|
import React from 'react';
|
2020-09-21 00:42:01 +02:00
|
|
|
import { useTransition, animated } from 'react-spring';
|
2020-09-20 10:33:39 +02:00
|
|
|
import useTheme from 'hooks/useTheme';
|
2020-09-21 00:42:01 +02:00
|
|
|
import Sun from 'assets/sun.svg';
|
|
|
|
import Moon from 'assets/moon.svg';
|
|
|
|
import styles from './ThemeButton.module.css';
|
|
|
|
import Icon from '../common/Icon';
|
2020-09-20 10:33:39 +02:00
|
|
|
|
|
|
|
export default function ThemeButton() {
|
|
|
|
const [theme, setTheme] = useTheme();
|
|
|
|
|
2022-03-03 00:23:47 +01:00
|
|
|
const transitions = useTransition(theme, {
|
|
|
|
initial: { opacity: 1 },
|
2020-09-21 00:42:01 +02:00
|
|
|
from: {
|
|
|
|
opacity: 0,
|
2020-09-21 06:31:53 +02:00
|
|
|
transform: `translateY(${theme === 'light' ? '20px' : '-20px'}) scale(0.5)`,
|
2020-09-21 00:42:01 +02:00
|
|
|
},
|
|
|
|
enter: { opacity: 1, transform: 'translateY(0px) scale(1)' },
|
|
|
|
leave: {
|
|
|
|
opacity: 0,
|
2020-09-21 06:31:53 +02:00
|
|
|
transform: `translateY(${theme === 'light' ? '-20px' : '20px'}) scale(0.5)`,
|
2020-09-21 00:42:01 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-09-20 10:33:39 +02:00
|
|
|
function handleClick() {
|
|
|
|
setTheme(theme === 'light' ? 'dark' : 'light');
|
|
|
|
}
|
|
|
|
|
2020-09-20 11:54:38 +02:00
|
|
|
return (
|
2020-09-26 07:31:18 +02:00
|
|
|
<div className={styles.button} onClick={handleClick}>
|
2022-03-03 00:23:47 +01:00
|
|
|
{transitions((styles, item) => (
|
|
|
|
<animated.div key={item} style={styles}>
|
|
|
|
<Icon icon={item === 'light' ? <Sun /> : <Moon />} />
|
|
|
|
</animated.div>
|
|
|
|
))}
|
2020-09-26 07:31:18 +02:00
|
|
|
</div>
|
2020-09-20 11:54:38 +02:00
|
|
|
);
|
2020-09-20 10:33:39 +02:00
|
|
|
}
|