mirror of
https://github.com/bitbaum/vitareba.git
synced 2026-12-05 07:08:31 +01:00
Replace 4-button pill (130px wide) with a single trigger button showing current locale + chevron. Dropdown opens on click, closes on outside click. Frees nav space and looks cleaner on all screen sizes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
71 lines
2.0 KiB
TypeScript
71 lines
2.0 KiB
TypeScript
"use client";
|
|
|
|
import { useLocale } from "next-intl";
|
|
import { useRouter, usePathname } from "@/lib/i18n/navigation";
|
|
import { routing } from "@/i18n/routing";
|
|
import { useState, useRef, useEffect } from "react";
|
|
import styles from "./LanguageSwitcher.module.css";
|
|
|
|
const LOCALE_LABELS: Record<string, string> = {
|
|
de: "DE",
|
|
en: "EN",
|
|
fr: "FR",
|
|
it: "IT",
|
|
};
|
|
|
|
export function LanguageSwitcher() {
|
|
const locale = useLocale();
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const [open, setOpen] = useState(false);
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
function handleClick(e: MouseEvent) {
|
|
if (ref.current && !ref.current.contains(e.target as Node)) {
|
|
setOpen(false);
|
|
}
|
|
}
|
|
document.addEventListener("mousedown", handleClick);
|
|
return () => document.removeEventListener("mousedown", handleClick);
|
|
}, []);
|
|
|
|
function select(l: string) {
|
|
router.replace(pathname, { locale: l });
|
|
setOpen(false);
|
|
}
|
|
|
|
return (
|
|
<div className={styles.root} ref={ref}>
|
|
<button
|
|
type="button"
|
|
className={styles.trigger}
|
|
onClick={() => setOpen((o) => !o)}
|
|
aria-haspopup="listbox"
|
|
aria-expanded={open}
|
|
>
|
|
{LOCALE_LABELS[locale]}
|
|
<svg className={`${styles.chevron} ${open ? styles.chevronOpen : ""}`} width="10" height="10" viewBox="0 0 10 10" fill="none">
|
|
<path d="M2 3.5L5 6.5L8 3.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
|
|
</svg>
|
|
</button>
|
|
{open && (
|
|
<div className={styles.dropdown} role="listbox">
|
|
{routing.locales.map((l) => (
|
|
<button
|
|
key={l}
|
|
type="button"
|
|
role="option"
|
|
aria-selected={l === locale}
|
|
className={`${styles.option} ${l === locale ? styles.optionActive : ""}`}
|
|
onClick={() => select(l)}
|
|
>
|
|
{LOCALE_LABELS[l]}
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|