1
0

language-switcher.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import Link from 'next/link';
  2. import { Languages } from 'lucide-react';
  3. import { i18n, locales } from '@/lib/i18n';
  4. import { cn } from '@/lib/cn';
  5. // Home-navbar language switcher.
  6. //
  7. // fumadocs' built-in popover switcher (`LanguageSelect`) has its item clicks
  8. // swallowed when it is nested inside HomeLayout's Radix `NavigationMenu` — the
  9. // dropdown opens but selecting a locale never fires `onChange`/`router.push`.
  10. // The docs sidebar isn't wrapped in a NavigationMenu, so the built-in one works
  11. // there and is kept. Here we use a native `<details>` toggle + real `<Link>`
  12. // anchors, which navigate reliably inside the navbar (like the other nav links).
  13. //
  14. // The home navbar only renders on the landing page, so the targets are simply
  15. // each locale's home (`/`, `/fa`, `/ru`, `/zh`).
  16. export function HomeLanguageSwitcher({ current }: { current: string }) {
  17. return (
  18. <details className="group relative [&>summary::-webkit-details-marker]:hidden">
  19. <summary
  20. aria-label="Choose a language"
  21. className="flex cursor-pointer list-none items-center rounded-lg p-1.5 text-fd-muted-foreground transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground group-open:bg-fd-accent"
  22. >
  23. <Languages className="size-5" />
  24. </summary>
  25. <div className="absolute end-0 z-50 mt-1.5 flex min-w-40 flex-col gap-0.5 rounded-lg border bg-fd-popover p-1 text-fd-popover-foreground shadow-lg">
  26. <p className="p-2 text-xs font-medium text-fd-muted-foreground">Choose a language</p>
  27. {locales.map(({ locale, name }) => (
  28. <Link
  29. key={locale}
  30. href={locale === i18n.defaultLanguage ? '/' : `/${locale}`}
  31. className={cn(
  32. 'rounded-md px-2 py-1.5 text-start text-sm transition-colors',
  33. locale === current
  34. ? 'bg-fd-primary/10 text-fd-primary'
  35. : 'text-fd-muted-foreground hover:bg-fd-accent hover:text-fd-accent-foreground',
  36. )}
  37. >
  38. {name}
  39. </Link>
  40. ))}
  41. </div>
  42. </details>
  43. );
  44. }