diff --git a/src/components/LanguageSwitcher.tsx b/src/components/LanguageSwitcher.tsx index 4eae700..6283e5a 100644 --- a/src/components/LanguageSwitcher.tsx +++ b/src/components/LanguageSwitcher.tsx @@ -1,44 +1,105 @@ 'use client' +import { useEffect, useRef, useState } from 'react' import { useLocale } from 'next-intl' import { useRouter, usePathname } from '@/navigation' import { routing } from '@/i18n/routing' -const FLAG_LABELS: Record = { +const LANGUAGE_LABELS: Record = { en: 'English', fr: 'Français', es: 'Español', it: 'Italiano', } +function Flag({ locale, className }: { locale: string; className?: string }) { + return ( + + ) +} + +function ChevronIcon() { + return ( + + + + ) +} + export function LanguageSwitcher() { const locale = useLocale() const router = useRouter() const pathname = usePathname() + const [open, setOpen] = useState(false) + const containerRef = useRef(null) + + useEffect(() => { + if (!open) return + + function handlePointerDown(event: MouseEvent) { + if (containerRef.current && !containerRef.current.contains(event.target as Node)) { + setOpen(false) + } + } + + function handleKeyDown(event: KeyboardEvent) { + if (event.key === 'Escape') setOpen(false) + } + + document.addEventListener('mousedown', handlePointerDown) + document.addEventListener('keydown', handleKeyDown) + return () => { + document.removeEventListener('mousedown', handlePointerDown) + document.removeEventListener('keydown', handleKeyDown) + } + }, [open]) + + function selectLocale(loc: string) { + setOpen(false) + router.replace(pathname, { locale: loc }) + } return ( -
- {routing.locales.map((loc) => ( - + + {open && ( +
    - {FLAG_LABELS[loc]} - - ))} + {routing.locales.map((loc) => ( +
  • + +
  • + ))} +
+ )}
) } diff --git a/src/components/SubmitForm.tsx b/src/components/SubmitForm.tsx index 53e8842..69bdcba 100644 --- a/src/components/SubmitForm.tsx +++ b/src/components/SubmitForm.tsx @@ -81,7 +81,7 @@ export function SubmitForm() {