From d7f3a47f40898d8ab4b341c32df4b472ea8170b9 Mon Sep 17 00:00:00 2001 From: Mattia Simonato Date: Wed, 22 May 2024 08:03:09 +0200 Subject: [PATCH] feat(SLB-277): use Headless UI for creating new LanguageSwitcher.tsx --- .../components/Molecules/LanguageSwitcher.tsx | 100 ++++++++++++++---- 1 file changed, 81 insertions(+), 19 deletions(-) diff --git a/packages/ui/src/components/Molecules/LanguageSwitcher.tsx b/packages/ui/src/components/Molecules/LanguageSwitcher.tsx index 234c27439..5684fef3f 100644 --- a/packages/ui/src/components/Molecules/LanguageSwitcher.tsx +++ b/packages/ui/src/components/Molecules/LanguageSwitcher.tsx @@ -1,30 +1,92 @@ import { Link, Locale, useLocation } from '@custom/schema'; +import { Menu, Transition } from '@headlessui/react'; +import { ChevronDownIcon } from '@heroicons/react/20/solid'; import clsx from 'clsx'; -import React from 'react'; +import React, { Fragment } from 'react'; import { useTranslations } from '../../utils/translations'; +function getLanguageName(locale: string) { + const languageNames = new Intl.DisplayNames([locale], { + type: 'language', + }); + return languageNames.of(locale); +} + export function LanguageSwitcher() { const translations = useTranslations(); const [location] = useLocation(); + const currentLocale = Object.values(Locale).find((locale) => { + const translationPath = translations[locale]; + return location.pathname.includes(translationPath || ''); + }); + + if (!currentLocale) { + console.error( + 'No matching locale found in current path:', + location.pathname, + ); + return null; + } + + const otherLocales = Object.values(Locale).filter( + (locale) => locale !== currentLocale, + ); + return ( - +
+ +
+ + {getLanguageName(currentLocale as string)} + +
+ + + +
+ {otherLocales.map((locale) => ( + + {({ focus }) => + translations[locale] ? ( + + {getLanguageName(locale as string)} + + ) : ( + + {getLanguageName(locale as string)} + + ) + } + + ))} +
+
+
+
+
); }