-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from instytutfi/feature/language-toggler
- Loading branch information
Showing
8 changed files
with
117 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react' | ||
|
||
import { render } from '@testing-library/react' | ||
|
||
import LanguageSwitcher from './LanguageSwitcher' | ||
|
||
describe('LanguageSwitcher', () => { | ||
it('renders without error', async () => { | ||
render(<LanguageSwitcher />) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React, { type FC, Fragment } from 'react' | ||
|
||
import { Menu, Transition } from '@headlessui/react' | ||
import clsx from 'clsx' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
import { type LANG_CODES, LANG_ICONS } from 'common/constants' | ||
|
||
const Item: FC<ItemProps> = ({ language }) => { | ||
const { i18n, t } = useTranslation() | ||
|
||
const handleClick = async (): Promise<void> => { | ||
await i18n.changeLanguage(language) | ||
} | ||
|
||
return ( | ||
<Menu.Item> | ||
{({ active }) => ( | ||
<button | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
onClick={handleClick} | ||
className={clsx( | ||
'flex w-full items-center rounded-md px-4 py-2 font-medium transition-all', | ||
!active && 'text-gray-900', | ||
active && 'bg-blue-600 text-white' | ||
)} | ||
> | ||
<span className="text-xl mr-2"> | ||
{LANG_ICONS[language]} | ||
</span> | ||
{t(`languageSwitcher.${i18n.language}.${language}.label`)} | ||
</button> | ||
)} | ||
</Menu.Item> | ||
) | ||
} | ||
|
||
interface ItemProps { | ||
language: typeof LANG_CODES[number] | ||
} | ||
|
||
const LanguageSwitcher: FC = () => { | ||
const { i18n, t } = useTranslation() | ||
|
||
return ( | ||
<Menu as="div" className="relative"> | ||
<Menu.Button className="flex items-center text-slate-300 font-medium hover:text-blue-500 transition-all"> | ||
<span className="text-xl mr-2"> | ||
{LANG_ICONS[i18n.language as typeof LANG_CODES[number]]} | ||
</span> | ||
<span className="hidden sm:block"> | ||
{t(`languageSwitcher.${i18n.language}.${i18n.language}.label`)} | ||
</span> | ||
</Menu.Button> | ||
<Transition | ||
as={Fragment} | ||
enter="transition ease-out duration-100" | ||
enterFrom="transform opacity-0 scale-95" | ||
enterTo="transform opacity-100 scale-100" | ||
leave="transition ease-in duration-75" | ||
leaveFrom="transform opacity-100 scale-100" | ||
leaveTo="transform opacity-0 scale-95" | ||
> | ||
<Menu.Items | ||
className="absolute right-0 mt-2 p-2 w-44 origin-top-right divide-y divide-gray-100 rounded-md bg-slate-50 shadow-2xl shadow-slate-800/40 ring-1 ring-black ring-opacity-5 focus:outline-none" | ||
> | ||
<Item language="pl" /> | ||
<Item language="en" /> | ||
</Menu.Items> | ||
</Transition> | ||
</Menu> | ||
) | ||
} | ||
|
||
export default LanguageSwitcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './LanguageSwitcher' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters