From 84f2e566337a510f0914c4fe4b6b8f5f7a040f06 Mon Sep 17 00:00:00 2001 From: therealsama <263038098+therealsama@users.noreply.github.com> Date: Sat, 29 Aug 2026 21:10:03 +0200 Subject: [PATCH] fix: keep the active tab visible when the header row overflows The nav packer filled the row strictly left-to-right, so a 320px bar spent its only slot on Best whichever feed was open. Picking Ask from the More menu left the bar reading Best, with the active pill hidden inside the menu it had just been chosen from. An active contextual pill escaped this only because it already sits at index 0. usePackedNav now takes a pinned item that claims its slot before the pass, and Header pins the active tab. The row still reads in canonical order, and a pin too wide to fit alone is dropped rather than overflowing the row. --- e2e/header-overflow.spec.ts | 34 +++++++++++++++++++ src/components/Header.test.tsx | 45 ++++++++++++++++++++++--- src/components/Header.tsx | 13 +++++--- src/hooks/usePackedNav.test.ts | 61 ++++++++++++++++++++++++++++++++++ src/hooks/usePackedNav.ts | 48 +++++++++++++++++--------- 5 files changed, 176 insertions(+), 25 deletions(-) diff --git a/e2e/header-overflow.spec.ts b/e2e/header-overflow.spec.ts index 3b77df7..68aeb84 100644 --- a/e2e/header-overflow.spec.ts +++ b/e2e/header-overflow.spec.ts @@ -129,6 +129,40 @@ test.describe('Header overflow — More dropdown', () => { }); }); +// The packer fills the row in canonical order, so the one slot a 320px bar +// has went to Best whichever feed you opened — the active pill was reachable +// only by opening the menu it had been packed into. Header pins the active +// item now; this is that fix meeting a real ResizeObserver. +test.describe('Header overflow — the active feed keeps its pill', () => { + test.use({ viewport: { width: 320, height: 568 } }); + + test.beforeEach(async ({ page }) => { + await setupApiMocks(page); + }); + + // Label, not route key: New lives at /newest and renders as "new". + for (const { route, label } of [ + { route: '/show', label: 'show' }, + { route: '/ask', label: 'ask' }, + { route: '/newest', label: 'new' }, + ]) { + test(`${label} is in the bar, not the menu`, async ({ page }) => { + await page.goto(`/#${route}`); + + // The trigger only renders once the packer has measured, so waiting on + // it also proves the row overflowed — without that, a bar wide enough + // for every tab would pass this test without exercising anything. + await expect(page.getByRole('button', { name: 'More tabs' })).toBeVisible(); + + const pill = page + .getByRole('navigation', { name: 'Sections' }) + .getByRole('link', { name: label }); + await expect(pill).toBeVisible(); + await expect(pill).toHaveAttribute('aria-current', 'page'); + }); + } +}); + test.describe('Theme toggle — accessible label', () => { test.beforeEach(async ({ page }) => { await setupApiMocks(page); diff --git a/src/components/Header.test.tsx b/src/components/Header.test.tsx index b8ff46e..e8a2ca5 100644 --- a/src/components/Header.test.tsx +++ b/src/components/Header.test.tsx @@ -23,9 +23,16 @@ interface MockPackedNavKeys { hidden: string[]; showOverflow: boolean; } +interface MockPackableItem { + key: string; + pinned?: boolean; +} +// `lastItems` records what Header hands the packer, so the pinning contract +// can be asserted without the real measurement the mock stands in for. const mockPackedNav = vi.hoisted( - (): { state: MockPackedNavKeys } => ({ + (): { state: MockPackedNavKeys; lastItems: MockPackableItem[] } => ({ state: { visible: [], hidden: [], showOverflow: false }, + lastItems: [], }), ); @@ -35,10 +42,11 @@ vi.mock('../hooks/usePackedNav', () => ({ // actual input items and returns the matching object. The default // empty-state branch returns the full input untouched (= everything // visible, no overflow) so unrelated tests keep finding all nav links. - usePackedNav: ( + usePackedNav: ( _ref: unknown, items: T[], ): { visible: T[]; hidden: T[]; showOverflow: boolean } => { + mockPackedNav.lastItems = items; if (mockPackedNav.state.visible.length === 0 && !mockPackedNav.state.showOverflow) { return { visible: items, hidden: [], showOverflow: false }; } @@ -358,8 +366,8 @@ describe('Header', () => { describe('nav DOM order', () => { // Locks the canonical packing rule: when an active contextual pill - // exists, it lives at index 0 of the