Skip to content

Commit

Permalink
feat: Language picker was added
Browse files Browse the repository at this point in the history
  • Loading branch information
Metastasis committed Mar 24, 2024
1 parent c789574 commit 7cefff6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 25 deletions.
22 changes: 0 additions & 22 deletions frontend/src/mockingbird/components/Language/Language.tsx

This file was deleted.

18 changes: 18 additions & 0 deletions frontend/src/mockingbird/components/Language/LanguagePicker.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.control {
display: flex;
width: 150px;
justify-content: flex-end;
padding: 8px 16px;
border-radius: 16px;
transition: background-color 150ms ease;
}

.itemContent {
align-content: center;
justify-content: space-between;
}

.icon {
width: 1.2rem;
height: 1.2rem;
}
54 changes: 54 additions & 0 deletions frontend/src/mockingbird/components/Language/LanguagePicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { UnstyledButton, Menu, Group } from '@mantine/core';
import IconCheck from '@tabler/icons-react/dist/esm/icons/IconCheck';
import IconLanguage from '@tabler/icons-react/dist/esm/icons/IconLanguage';
import styles from './LanguagePicker.css';

type LanguageItem = { label: string; value: string };

const data: LanguageItem[] = [
{ label: 'English', value: 'en' },
{ label: 'Русский', value: 'ru' },
];

export function LanguagePicker() {
const { i18n } = useTranslation();
const [opened, setOpened] = useState(false);
const [selected, setSelected] = useState(() => {
return data.find((item) => item.value === i18n.language) || data[0];
});
const handleSelectLanguage = (item: LanguageItem) => {
setOpened(false);
setSelected(item);
i18n.changeLanguage(item.value);
};
const items = data.map((item) => (
<Menu.Item onClick={() => handleSelectLanguage(item)} key={item.label}>
<Group className={styles.itemContent}>
<span>{item.label}</span>
{item.value === selected.value && <IconCheck className={styles.icon} />}
</Group>
</Menu.Item>
));

return (
<Menu
onOpen={() => setOpened(true)}
onClose={() => setOpened(false)}
radius="md"
width="target"
withinPortal
>
<Menu.Target>
<UnstyledButton
className={styles.control}
data-expanded={opened || undefined}
>
<IconLanguage color="grey" size="1.2rem" />
</UnstyledButton>
</Menu.Target>
<Menu.Dropdown>{items}</Menu.Dropdown>
</Menu>
);
}
2 changes: 1 addition & 1 deletion frontend/src/mockingbird/components/Language/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { LanguageSwitcher } from './Language';
export { LanguagePicker } from './LanguagePicker';
4 changes: 2 additions & 2 deletions frontend/src/mockingbird/layers/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react';
import { connect } from '@tramvai/state';
import { Container, Title, Flex } from '@mantine/core';
import { getJson } from 'src/infrastructure/request';
import { LanguageSwitcher } from 'src/mockingbird/components/Language';
import { LanguagePicker } from 'src/mockingbird/components/Language';
import { Shadow } from './Shadow';

type Props = {
Expand All @@ -26,7 +26,7 @@ function Header({ assetsPrefix }: Props) {
<Title order={2} mr="auto">
{title}
</Title>
<LanguageSwitcher />
<LanguagePicker />
</Flex>
</Container>
</Shadow>
Expand Down

0 comments on commit 7cefff6

Please sign in to comment.