2023-03-22 05:28:36 +01:00
|
|
|
import { Icon, Button, PopupTrigger, Popup, Text } from 'react-basics';
|
2023-01-28 06:53:13 +01:00
|
|
|
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';
|
2020-10-02 21:15:42 +02:00
|
|
|
import styles from './LanguageButton.module.css';
|
2020-09-07 10:22:16 +02:00
|
|
|
|
2023-04-21 17:00:42 +02:00
|
|
|
export function LanguageButton() {
|
2023-04-19 06:30:09 +02:00
|
|
|
const { locale, saveLocale, dir } = 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
|
|
|
|
2023-05-25 06:40:02 +02:00
|
|
|
function handleSelect(value, close) {
|
|
|
|
saveLocale(value);
|
|
|
|
close();
|
2020-09-07 10:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-01-28 06:53:13 +01:00
|
|
|
<PopupTrigger>
|
2023-03-22 05:28:36 +01:00
|
|
|
<Button variant="quiet">
|
|
|
|
<Icon>
|
|
|
|
<Icons.Globe />
|
|
|
|
</Icon>
|
|
|
|
</Button>
|
2023-04-19 06:30:09 +02:00
|
|
|
<Popup position="bottom" alignment={dir === 'rtl' ? 'start' : 'end'}>
|
2023-05-25 06:40:02 +02:00
|
|
|
{close => {
|
|
|
|
return (
|
|
|
|
<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, close)}
|
|
|
|
>
|
|
|
|
<Text>{label}</Text>
|
|
|
|
{value === locale && (
|
|
|
|
<Icon className={styles.icon}>
|
|
|
|
<Icons.Check />
|
|
|
|
</Icon>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
2023-01-28 06:53:13 +01:00
|
|
|
</Popup>
|
|
|
|
</PopupTrigger>
|
2020-09-07 10:22:16 +02:00
|
|
|
);
|
|
|
|
}
|
2023-04-21 17:00:42 +02:00
|
|
|
|
|
|
|
export default LanguageButton;
|