2020-09-21 00:42:01 +02:00
|
|
|
import { useTransition, animated } from 'react-spring';
|
2023-01-28 06:53:13 +01:00
|
|
|
import { Button, Icon, PopupTrigger, Tooltip } from 'react-basics';
|
|
|
|
import { useIntl } from 'react-intl';
|
2020-09-20 10:33:39 +02:00
|
|
|
import useTheme from 'hooks/useTheme';
|
2023-01-31 06:44:07 +01:00
|
|
|
import Icons from 'components/icons';
|
2023-01-28 06:53:13 +01:00
|
|
|
import { labels } from 'components/messages';
|
2020-09-21 00:42:01 +02:00
|
|
|
import styles from './ThemeButton.module.css';
|
2020-09-20 10:33:39 +02:00
|
|
|
|
2023-01-28 06:53:13 +01:00
|
|
|
export default function ThemeButton({ tooltipPosition = 'top' }) {
|
2020-09-20 10:33:39 +02:00
|
|
|
const [theme, setTheme] = useTheme();
|
2023-01-28 06:53:13 +01:00
|
|
|
const { formatMessage } = useIntl();
|
2020-09-20 10:33:39 +02:00
|
|
|
|
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
|
|
|
},
|
2023-01-28 06:53:13 +01:00
|
|
|
enter: { opacity: 1, transform: 'translateY(0px) scale(1.0)' },
|
2020-09-21 00:42:01 +02:00
|
|
|
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 (
|
2023-02-04 17:59:52 +01:00
|
|
|
<Tooltip label={formatMessage(labels.theme)} position={tooltipPosition}>
|
2023-01-28 06:53:13 +01:00
|
|
|
<Button variant="quiet" className={styles.button} onClick={handleClick}>
|
|
|
|
{transitions((style, item) => (
|
|
|
|
<animated.div key={item} style={style}>
|
2023-01-31 06:44:07 +01:00
|
|
|
<Icon className={styles.icon}>{item === 'light' ? <Icons.Sun /> : <Icons.Moon />}</Icon>
|
2023-01-28 06:53:13 +01:00
|
|
|
</animated.div>
|
|
|
|
))}
|
|
|
|
</Button>
|
2023-02-04 17:59:52 +01:00
|
|
|
</Tooltip>
|
2020-09-20 11:54:38 +02:00
|
|
|
);
|
2020-09-20 10:33:39 +02:00
|
|
|
}
|