2023-01-31 06:44:07 +01:00
|
|
|
import { Icon, Button, PopupTrigger, Popup, Tooltip, Text } from 'react-basics';
|
2023-01-28 06:53:13 +01:00
|
|
|
import { useIntl } from 'react-intl';
|
|
|
|
import classNames from 'classnames';
|
2021-06-30 03:41:34 +02:00
|
|
|
import { languages } from 'lib/lang';
|
2020-09-17 09:17:11 +02:00
|
|
|
import useLocale from 'hooks/useLocale';
|
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-10-02 21:15:42 +02:00
|
|
|
import styles from './LanguageButton.module.css';
|
2020-09-07 10:22:16 +02:00
|
|
|
|
2023-01-28 06:53:13 +01:00
|
|
|
export default function LanguageButton({ tooltipPosition = 'top' }) {
|
|
|
|
const { formatMessage } = useIntl();
|
2021-06-30 03:41:34 +02:00
|
|
|
const { locale, saveLocale } = useLocale();
|
2023-01-28 06:53:13 +01:00
|
|
|
const items = Object.keys(languages).map(key => ({ ...languages[key], value: key }));
|
2020-09-07 10:22:16 +02:00
|
|
|
|
|
|
|
function handleSelect(value) {
|
2021-06-30 03:41:34 +02:00
|
|
|
saveLocale(value);
|
2020-09-07 10:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-01-28 06:53:13 +01:00
|
|
|
<PopupTrigger>
|
2023-02-08 07:17:55 +01:00
|
|
|
<Tooltip label={formatMessage(labels.language)} position={tooltipPosition}>
|
2023-01-28 06:53:13 +01:00
|
|
|
<Button variant="quiet">
|
|
|
|
<Icon>
|
2023-01-31 06:44:07 +01:00
|
|
|
<Icons.Globe />
|
2023-01-28 06:53:13 +01:00
|
|
|
</Icon>
|
|
|
|
</Button>
|
2023-02-08 07:17:55 +01:00
|
|
|
</Tooltip>
|
2023-01-28 06:53:13 +01:00
|
|
|
<Popup position="right" alignment="end">
|
|
|
|
<div className={styles.menu}>
|
|
|
|
{items.map(({ value, label }) => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key={value}
|
|
|
|
className={classNames(styles.item, { [styles.selected]: value === locale })}
|
|
|
|
onClick={handleSelect.bind(null, value)}
|
|
|
|
>
|
|
|
|
<Text>{label}</Text>
|
|
|
|
{value === locale && (
|
|
|
|
<Icon className={styles.icon}>
|
|
|
|
<Icons.Check />
|
|
|
|
</Icon>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</Popup>
|
|
|
|
</PopupTrigger>
|
2020-09-07 10:22:16 +02:00
|
|
|
);
|
|
|
|
}
|