|
| 1 | +import React, { type FC, Fragment } from 'react' |
| 2 | + |
| 3 | +import { Menu, Transition } from '@headlessui/react' |
| 4 | +import clsx from 'clsx' |
| 5 | +import { useTranslation } from 'react-i18next' |
| 6 | + |
| 7 | +import { type LANG_CODES, LANG_ICONS } from 'common/constants' |
| 8 | + |
| 9 | +const Item: FC<ItemProps> = ({ language }) => { |
| 10 | + const { i18n, t } = useTranslation() |
| 11 | + |
| 12 | + const handleClick = async (): Promise<void> => { |
| 13 | + await i18n.changeLanguage(language) |
| 14 | + } |
| 15 | + |
| 16 | + return ( |
| 17 | + <Menu.Item> |
| 18 | + {({ active }) => ( |
| 19 | + <button |
| 20 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 21 | + onClick={handleClick} |
| 22 | + className={clsx( |
| 23 | + 'flex w-full items-center rounded-md px-4 py-2 font-medium transition-all', |
| 24 | + !active && 'text-gray-900', |
| 25 | + active && 'bg-blue-600 text-white' |
| 26 | + )} |
| 27 | + > |
| 28 | + <span className="text-xl mr-2"> |
| 29 | + {LANG_ICONS[language]} |
| 30 | + </span> |
| 31 | + {t(`languageSwitcher.${i18n.language}.${language}.label`)} |
| 32 | + </button> |
| 33 | + )} |
| 34 | + </Menu.Item> |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +interface ItemProps { |
| 39 | + language: typeof LANG_CODES[number] |
| 40 | +} |
| 41 | + |
| 42 | +const LanguageSwitcher: FC = () => { |
| 43 | + const { i18n, t } = useTranslation() |
| 44 | + |
| 45 | + return ( |
| 46 | + <Menu as="div" className="relative"> |
| 47 | + <Menu.Button className="flex items-center text-slate-300 font-medium hover:text-blue-500 transition-all"> |
| 48 | + <span className="text-xl mr-2"> |
| 49 | + {LANG_ICONS[i18n.language as typeof LANG_CODES[number]]} |
| 50 | + </span> |
| 51 | + <span className="hidden sm:block"> |
| 52 | + {t(`languageSwitcher.${i18n.language}.${i18n.language}.label`)} |
| 53 | + </span> |
| 54 | + </Menu.Button> |
| 55 | + <Transition |
| 56 | + as={Fragment} |
| 57 | + enter="transition ease-out duration-100" |
| 58 | + enterFrom="transform opacity-0 scale-95" |
| 59 | + enterTo="transform opacity-100 scale-100" |
| 60 | + leave="transition ease-in duration-75" |
| 61 | + leaveFrom="transform opacity-100 scale-100" |
| 62 | + leaveTo="transform opacity-0 scale-95" |
| 63 | + > |
| 64 | + <Menu.Items |
| 65 | + 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" |
| 66 | + > |
| 67 | + <Item language="pl" /> |
| 68 | + <Item language="en" /> |
| 69 | + </Menu.Items> |
| 70 | + </Transition> |
| 71 | + </Menu> |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +export default LanguageSwitcher |
0 commit comments