132 lines
4.4 KiB
TypeScript
132 lines
4.4 KiB
TypeScript
import { useEffect, useId, useRef, useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { changeLanguage } from '../i18n';
|
|
import type { Language } from '../i18n/language';
|
|
import { cn } from '../lib/utils';
|
|
|
|
const LANGS: Language[] = ['pl', 'en'];
|
|
|
|
/** Flagi rysowane inline (emoji flagi nie renderują się w Chrome na Windows). */
|
|
function Flag({ code }: { code: Language }) {
|
|
const clipId = useId();
|
|
return (
|
|
<svg
|
|
width="16"
|
|
height="11"
|
|
viewBox="0 0 16 11"
|
|
aria-hidden="true"
|
|
className="shrink-0 rounded-[2px] shadow-[0_0_0_1px_rgb(0_0_0/0.3)]"
|
|
>
|
|
<defs>
|
|
<clipPath id={clipId}>
|
|
<rect width="16" height="11" rx="1.5" />
|
|
</clipPath>
|
|
</defs>
|
|
<g clipPath={`url(#${clipId})`}>
|
|
{code === 'pl' ? (
|
|
<>
|
|
<rect width="16" height="5.5" fill="#f5f5f3" />
|
|
<rect y="5.5" width="16" height="5.5" fill="#dc143c" />
|
|
</>
|
|
) : (
|
|
<>
|
|
<rect width="16" height="11" fill="#012169" />
|
|
<path d="M0 0 16 11M16 0 0 11" stroke="#f5f5f3" strokeWidth="2.4" />
|
|
<path d="M0 0 16 11M16 0 0 11" stroke="#c8102e" strokeWidth="0.9" />
|
|
<path d="M8 0V11M0 5.5H16" stroke="#f5f5f3" strokeWidth="3" />
|
|
<path d="M8 0V11M0 5.5H16" stroke="#c8102e" strokeWidth="1.7" />
|
|
</>
|
|
)}
|
|
</g>
|
|
</svg>
|
|
);
|
|
}
|
|
|
|
/** Przełącznik języka: przycisk z flagą i kodem + rozwijane menu wyboru (stylistyka okien OS). */
|
|
export function LangSwitcher() {
|
|
const { t, i18n } = useTranslation(['nav', 'common']);
|
|
const [open, setOpen] = useState(false);
|
|
const rootRef = useRef<HTMLDivElement>(null);
|
|
const current: Language = i18n.language === 'en' ? 'en' : 'pl';
|
|
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
const onPointerDown = (e: PointerEvent) => {
|
|
if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);
|
|
};
|
|
const onKeyDown = (e: KeyboardEvent) => {
|
|
if (e.key === 'Escape') setOpen(false);
|
|
};
|
|
document.addEventListener('pointerdown', onPointerDown);
|
|
document.addEventListener('keydown', onKeyDown);
|
|
return () => {
|
|
document.removeEventListener('pointerdown', onPointerDown);
|
|
document.removeEventListener('keydown', onKeyDown);
|
|
};
|
|
}, [open]);
|
|
|
|
return (
|
|
<div ref={rootRef} className="relative">
|
|
<button
|
|
type="button"
|
|
aria-haspopup="menu"
|
|
aria-expanded={open}
|
|
aria-label={t('nav:langSwitchAria')}
|
|
onClick={() => setOpen((v) => !v)}
|
|
className="flex items-center gap-1.5 rounded-md border border-line bg-input px-2 py-1.5 text-ink transition-colors hover:border-accent/50"
|
|
>
|
|
<Flag code={current} />
|
|
<span className="font-mono text-[11px] font-semibold">{current.toUpperCase()}</span>
|
|
<span
|
|
aria-hidden="true"
|
|
className={cn('font-mono text-[9px] text-dim transition-transform', open && 'rotate-180')}
|
|
>
|
|
▾
|
|
</span>
|
|
</button>
|
|
|
|
{open && (
|
|
<div
|
|
role="menu"
|
|
aria-label={t('nav:langSwitchAria')}
|
|
className="window-glow absolute right-0 top-full z-50 mt-1.5 w-44 overflow-hidden rounded-lg border border-line bg-(image:--card-grad) shadow-card"
|
|
>
|
|
{LANGS.map((lang) => {
|
|
const active = lang === current;
|
|
return (
|
|
<button
|
|
key={lang}
|
|
type="button"
|
|
role="menuitemradio"
|
|
aria-checked={active}
|
|
onClick={() => {
|
|
changeLanguage(lang);
|
|
setOpen(false);
|
|
}}
|
|
className={cn(
|
|
'flex w-full items-center gap-2.5 px-3 py-2.5 text-left text-[12.5px] transition-colors hover:bg-input',
|
|
active ? 'text-ink' : 'text-dim',
|
|
)}
|
|
>
|
|
<Flag code={lang} />
|
|
<span className="font-medium">
|
|
{lang === 'pl' ? t('nav:langPl') : t('nav:langEn')}
|
|
</span>
|
|
<span
|
|
aria-hidden="true"
|
|
className={cn(
|
|
'ml-auto font-mono text-[11px]',
|
|
active ? 'text-accent' : 'text-transparent',
|
|
)}
|
|
>
|
|
✓
|
|
</span>
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|