Web: przejacznik jezyka jako menu z flaga za CTA Launch — przycisk z flaga i kodem jezyka + rozwijane menu wyboru w stylistyce okien OS (flagi PL/UK rysowane inline SVG, zamkniecie kliknieciem poza menu i Escape), asercje unit 8 i e2e 7 zsynchronizowane
This commit is contained in:
131
web/src/components/LangSwitcher.tsx
Normal file
131
web/src/components/LangSwitcher.tsx
Normal file
@@ -0,0 +1,131 @@
|
||||
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>
|
||||
);
|
||||
}
|
||||
@@ -1,15 +1,11 @@
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Brandmark } from '../components/Brandmark';
|
||||
import { LangSwitcher } from '../components/LangSwitcher';
|
||||
import { appUrl } from '../lib/links';
|
||||
import { changeLanguage } from '../i18n';
|
||||
import { cn } from '../lib/utils';
|
||||
import type { Language } from '../i18n/language';
|
||||
|
||||
const LANGS: Language[] = ['pl', 'en'];
|
||||
|
||||
/** Pasek „systemowy” — sticky, jak topbar apki: znak, wersja, kotwice, język, CTA. */
|
||||
/** Pasek „systemowy” — sticky, jak topbar apki: znak, wersja, kotwice, CTA, język. */
|
||||
export function SiteHeader() {
|
||||
const { t, i18n } = useTranslation(['nav', 'common']);
|
||||
const { t } = useTranslation(['nav', 'common']);
|
||||
const anchors = [
|
||||
{ href: '#ekosystem', label: t('nav:anchors.ekosystem') },
|
||||
{ href: '#slownik', label: t('nav:anchors.slownik') },
|
||||
@@ -40,33 +36,13 @@ export function SiteHeader() {
|
||||
</a>
|
||||
))}
|
||||
</nav>
|
||||
<div
|
||||
role="group"
|
||||
aria-label={t('nav:langSwitchAria')}
|
||||
className="ml-auto flex items-center gap-1.5 sm:ml-0"
|
||||
>
|
||||
{LANGS.map((lang) => (
|
||||
<button
|
||||
key={lang}
|
||||
type="button"
|
||||
aria-pressed={i18n.language === lang}
|
||||
aria-label={lang === 'pl' ? t('nav:langPl') : t('nav:langEn')}
|
||||
onClick={() => changeLanguage(lang)}
|
||||
className={cn(
|
||||
'font-mono text-[11px] font-semibold uppercase transition-colors',
|
||||
i18n.language === lang ? 'text-ink' : 'text-dim hover:text-ink',
|
||||
)}
|
||||
>
|
||||
{lang}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<a
|
||||
href={appUrl()}
|
||||
className="inline-flex items-center gap-1 rounded-md border-0 bg-(image:--btn-grad) px-3.5 py-1.5 text-[13px] font-bold text-accent-ink shadow-(--btn-shadow) transition-transform hover:-translate-y-px"
|
||||
className="ml-auto inline-flex items-center gap-1 rounded-md border-0 bg-(image:--btn-grad) px-3.5 py-1.5 text-[13px] font-bold text-accent-ink shadow-(--btn-shadow) transition-transform hover:-translate-y-px sm:ml-0"
|
||||
>
|
||||
{t('nav:launch')} <span aria-hidden="true">▸</span>
|
||||
</a>
|
||||
<LangSwitcher />
|
||||
</div>
|
||||
</header>
|
||||
);
|
||||
|
||||
@@ -1,26 +1,38 @@
|
||||
import { expect, test } from './fixtures';
|
||||
|
||||
test('przełącznik PL/EN: zmiana języka, tytułu i persistencja po przeładowaniu', async ({
|
||||
test('przełącznik języka (menu z flagą): zmiana, tytułu i persistencja po przeładowaniu', async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.goto('/');
|
||||
await expect(page.getByRole('heading', { level: 1 })).toHaveText('Trening, który się dowozi.');
|
||||
await expect(page.locator('html')).toHaveAttribute('lang', 'pl');
|
||||
|
||||
await page.getByRole('button', { name: 'English' }).click();
|
||||
const switcher = page.getByRole('button', { name: 'Zmień język' });
|
||||
await expect(switcher).toContainText('PL');
|
||||
await switcher.click();
|
||||
const enItem = page.getByRole('menuitemradio', { name: 'English' });
|
||||
await expect(enItem).toHaveAttribute('aria-checked', 'false');
|
||||
await enItem.click();
|
||||
|
||||
await expect(page.getByRole('heading', { level: 1 })).toHaveText(
|
||||
'Training, that actually delivers.',
|
||||
);
|
||||
await expect(page).toHaveTitle('FitOS — Training that delivers.');
|
||||
await expect(page.locator('html')).toHaveAttribute('lang', 'en');
|
||||
await expect(page.getByRole('button', { name: 'English' })).toHaveAttribute('aria-pressed', 'true');
|
||||
await expect(page.getByRole('button', { name: 'Change language' })).toContainText('EN');
|
||||
|
||||
await page.reload();
|
||||
await expect(page.getByRole('heading', { level: 1 })).toHaveText(
|
||||
'Training, that actually delivers.',
|
||||
);
|
||||
|
||||
await page.getByRole('button', { name: 'Polski' }).click();
|
||||
const enSwitcher = page.getByRole('button', { name: 'Change language' });
|
||||
await enSwitcher.click();
|
||||
await expect(page.getByRole('menuitemradio', { name: 'English' })).toHaveAttribute(
|
||||
'aria-checked',
|
||||
'true',
|
||||
);
|
||||
await page.getByRole('menuitemradio', { name: 'Polski' }).click();
|
||||
await expect(page.getByRole('heading', { level: 1 })).toHaveText('Trening, który się dowozi.');
|
||||
await expect(page.locator('html')).toHaveAttribute('lang', 'pl');
|
||||
});
|
||||
@@ -35,5 +47,6 @@ test.describe('język przeglądarki jako domyślny', () => {
|
||||
);
|
||||
await expect(page).toHaveTitle('FitOS — Training that delivers.');
|
||||
await expect(page.locator('html')).toHaveAttribute('lang', 'en');
|
||||
await expect(page.getByRole('button', { name: 'Change language' })).toContainText('EN');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -75,13 +75,22 @@ describe('strona-wizytówka FitOS', () => {
|
||||
expect(trenerBtn).toHaveAttribute('aria-expanded', 'false');
|
||||
});
|
||||
|
||||
it('przełącznik PL/EN zmienia język interfejsu i zapisuje wybór', () => {
|
||||
it('przełącznik języka (menu z flagą) zmienia język interfejsu', () => {
|
||||
render(<App />);
|
||||
fireEvent.click(screen.getByRole('button', { name: 'English' }));
|
||||
const trigger = screen.getByRole('button', { name: 'Zmień język' });
|
||||
expect(trigger).toHaveAttribute('aria-expanded', 'false');
|
||||
fireEvent.click(trigger);
|
||||
expect(trigger).toHaveAttribute('aria-expanded', 'true');
|
||||
const enItem = screen.getByRole('menuitemradio', { name: 'English' });
|
||||
expect(enItem).toHaveAttribute('aria-checked', 'false');
|
||||
fireEvent.click(enItem);
|
||||
expect(
|
||||
screen.getByRole('heading', { level: 1, name: 'Training, that actually delivers.' }),
|
||||
).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'English' })).toHaveAttribute('aria-pressed', 'true');
|
||||
expect(screen.getByRole('button', { name: 'Polski' })).toHaveAttribute('aria-pressed', 'false');
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Change language' }));
|
||||
expect(screen.getByRole('menuitemradio', { name: 'English' })).toHaveAttribute(
|
||||
'aria-checked',
|
||||
'true',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user