({ locale }: LangSwitcherProps)
| 9 | } |
| 10 | |
| 11 | export default function LangSwitcher({ locale }: LangSwitcherProps) { |
| 12 | const pathname = usePathname(); |
| 13 | const [opened, setOpened] = useState(false); |
| 14 | const { locales } = i18n; |
| 15 | |
| 16 | const getSwitchLink = (newLocale: Locale) => |
| 17 | `/${newLocale}${pathname.replace(`/${locale}`, "")}`; |
| 18 | |
| 19 | return ( |
| 20 | <div className="relative bg-white text-blue cursor-pointer"> |
| 21 | <button |
| 22 | onClick={() => setOpened(!opened)} |
| 23 | className="flex items-center font-bold uppercase px-2 gap-1" |
| 24 | > |
| 25 | <span>{locale}</span> |
| 26 | <svg |
| 27 | xmlns="http://www.w3.org/2000/svg" |
| 28 | viewBox="0 0 24 24" |
| 29 | strokeWidth="2" |
| 30 | stroke="currentColor" |
| 31 | fill="none" |
| 32 | className={classNames( |
| 33 | "w-5 h-5 transition-all", |
| 34 | opened && "rotate-180" |
| 35 | )} |
| 36 | > |
| 37 | <path d="M19.5 8.25l-7.5 7.5-7.5-7.5" /> |
| 38 | </svg> |
| 39 | </button> |
| 40 | <div className="absolute right-0 top-full flex flex-col w-full"> |
| 41 | <div className="relative w-full overflow-hidden"> |
| 42 | <div |
| 43 | className={classNames( |
| 44 | "transition-all bg-white", |
| 45 | opened |
| 46 | ? "-translate-y-0 opacity-100" |
| 47 | : "-translate-y-full opacity-100" |
| 48 | )} |
| 49 | > |
| 50 | {locales |
| 51 | .filter((l) => l !== locale) |
| 52 | .map((newLocale: Locale) => ( |
| 53 | <a |
| 54 | key={locale} |
| 55 | className="z-10 w-full px-2 block" |
| 56 | href={getSwitchLink(newLocale)} |
| 57 | > |
| 58 | {newLocale} |
| 59 | </a> |
| 60 | ))} |
| 61 | </div> |
| 62 | </div> |
| 63 | </div> |
| 64 | </div> |
| 65 | ); |
| 66 | } |
nothing calls this directly
no test coverage detected