From 0f7508b325d1b6ed26ef036f4edcf6191ec56e98 Mon Sep 17 00:00:00 2001 From: therealsama <263038098+therealsama@users.noreply.github.com> Date: Mon, 25 May 2026 20:13:43 +0200 Subject: [PATCH] Fix centered state layouts and mobile offline spacing Center shared list and swipe fallback states around the fixed header and network bar. Keep mobile story spacing stable when the offline bar appears and harden the WebKit geometry regression by waiting for the active PageStage to settle. Add focused E2E and component coverage for the shared list-state centering contract. --- e2e/domain-filter.spec.ts | 38 ++++++- e2e/fixtures/layout-helpers.ts | 90 +++++++++++++++ e2e/network-status-bar.spec.ts | 77 +++++++++++++ e2e/user-submissions.spec.ts | 14 ++- src/components/InfiniteStoryListPage.test.tsx | 10 +- src/components/InfiniteStoryListPage.tsx | 104 +++++++++--------- src/components/SwipeCommentViewer.tsx | 4 +- src/components/SwipeStoryViewer.test.tsx | 34 ++++++ src/components/SwipeStoryViewer.tsx | 11 +- src/index.css | 30 +++-- 10 files changed, 342 insertions(+), 70 deletions(-) create mode 100644 e2e/fixtures/layout-helpers.ts create mode 100644 src/components/SwipeStoryViewer.test.tsx diff --git a/e2e/domain-filter.spec.ts b/e2e/domain-filter.spec.ts index f9d675d..7e81ca9 100644 --- a/e2e/domain-filter.spec.ts +++ b/e2e/domain-filter.spec.ts @@ -7,6 +7,10 @@ import { smoothScrollAndAwaitSettled, waitForScrollAtIndex, } from './fixtures/swipe-helpers'; +import { + expectLocatorCenteredBetweenChrome, + setOfflineAndWaitForBar, +} from './fixtures/layout-helpers'; test.describe('Domain Filter', () => { test.beforeEach(async ({ page }) => { @@ -58,6 +62,28 @@ test.describe('Domain Filter - Edge Cases', () => { }); }); +test.describe('Domain Filter - Centered state layouts', () => { + test.use({ viewport: { width: 1280, height: 720 }, deviceScaleFactor: 1 }); + + test.beforeEach(async ({ page }) => { + await setupApiMocks(page); + }); + + test('keeps the empty domain state centered with the offline bar visible', async ({ page, context }) => { + await stubEmptyDomainSearch(page, 'unknown-domain.xyz'); + + await page.goto('/#/from/unknown-domain.xyz'); + + const stateRoot = page.getByRole('heading', { + name: 'No submissions found from "unknown-domain.xyz"', + }).locator('xpath=..'); + await expect(stateRoot).toBeVisible(); + + await setOfflineAndWaitForBar(page, context); + await expectLocatorCenteredBetweenChrome(stateRoot); + }); +}); + test.describe('Domain Filter - Header "from" button', () => { // Scoped to desktop viewport because the "stays active after opening a // story" and "browser back restores the list" tests below click @@ -167,11 +193,13 @@ test.describe('Domain Filter - Header "from" button', () => { }); test.describe('Domain Filter - Empty domain', () => { + test.use({ viewport: { width: 1280, height: 720 }, deviceScaleFactor: 1 }); + test.beforeEach(async ({ page }) => { await setupApiMocks(page); }); - test('shows error with link to home when domain is empty', async ({ page }) => { + test('shows error with link to home when domain is empty', async ({ page, context }) => { await page.goto('/#/from/'); // Document title should fall back to default @@ -180,6 +208,14 @@ test.describe('Domain Filter - Empty domain', () => { // Should show error message await expect(page.getByText('No domain specified')).toBeVisible(); + await setOfflineAndWaitForBar(page, context); + + // `DomainStories` uses the shared `page-state-center-padded` helper. + const stateRoot = page.getByRole('heading', { + name: 'No domain specified', + }).locator('xpath=..'); + await expectLocatorCenteredBetweenChrome(stateRoot); + // Should have a link back to home that works const homeLink = page.getByRole('link', { name: /Back to Home/i }); await expect(homeLink).toBeVisible(); diff --git a/e2e/fixtures/layout-helpers.ts b/e2e/fixtures/layout-helpers.ts new file mode 100644 index 0000000..f6da9ab --- /dev/null +++ b/e2e/fixtures/layout-helpers.ts @@ -0,0 +1,90 @@ +import type { BrowserContext, Locator, Page } from '@playwright/test'; +import { expect } from '@playwright/test'; + +interface CenteredBetweenChromeOptions { + tolerance?: number; + topSelector?: string; + bottomSelector?: string; +} + +/** + * Force the app into its offline-bar state after the route has already loaded. + * This lets geometry assertions include the fixed bottom chrome without making + * the page's initial data fetch nondeterministic. + */ +export async function setOfflineAndWaitForBar(page: Page, context: BrowserContext) { + await context.setOffline(true); + await page.evaluate(() => { + Object.defineProperty(navigator, 'onLine', { + value: false, + writable: true, + configurable: true, + }); + window.dispatchEvent(new Event('offline')); + }); + + await expect(page.getByText('No internet connection')).toBeVisible({ timeout: 5000 }); + + await page.waitForFunction(() => { + const bar = document.querySelector('.network-status-bar'); + if (!bar) return false; + + const rect = bar.getBoundingClientRect(); + return Math.abs(rect.bottom - window.innerHeight) <= 2; + }, { timeout: 5000 }); +} + +/** + * Assert that a block is centered in the usable viewport between the fixed + * header and the optional fixed bottom bar. + */ +export async function expectLocatorCenteredBetweenChrome( + locator: Locator, + { + tolerance = 8, + topSelector = 'header', + bottomSelector = '.network-status-bar', + }: CenteredBetweenChromeOptions = {}, +) { + await expect(locator).toBeVisible(); + + const geometry = await locator.evaluate((el, selectors) => { + const childRects = Array.from(el.children) + .map((child) => child.getBoundingClientRect()) + .filter((rect) => rect.width > 0 && rect.height > 0); + + const rect = childRects.length > 0 + ? childRects.reduce( + (bounds, childRect) => ({ + top: Math.min(bounds.top, childRect.top), + right: Math.max(bounds.right, childRect.right), + bottom: Math.max(bounds.bottom, childRect.bottom), + left: Math.min(bounds.left, childRect.left), + }), + { + top: childRects[0].top, + right: childRects[0].right, + bottom: childRects[0].bottom, + left: childRects[0].left, + }, + ) + : el.getBoundingClientRect(); + + const topBoundary = document.querySelector(selectors.topSelector)?.getBoundingClientRect().bottom ?? 0; + const bottomBoundary = document.querySelector(selectors.bottomSelector)?.getBoundingClientRect().top ?? window.innerHeight; + const expectedCenterX = window.innerWidth / 2; + const expectedCenterY = topBoundary + ((bottomBoundary - topBoundary) / 2); + const width = 'width' in rect ? rect.width : rect.right - rect.left; + const height = 'height' in rect ? rect.height : rect.bottom - rect.top; + const centerX = rect.left + (width / 2); + const centerY = rect.top + (height / 2); + + return { + deltaX: Math.abs(centerX - expectedCenterX), + deltaY: Math.abs(centerY - expectedCenterY), + }; + }, { topSelector, bottomSelector }); + + expect(geometry.deltaX).toBeLessThanOrEqual(tolerance); + expect(geometry.deltaY).toBeLessThanOrEqual(tolerance); +} \ No newline at end of file diff --git a/e2e/network-status-bar.spec.ts b/e2e/network-status-bar.spec.ts index 100f9cf..14f7775 100644 --- a/e2e/network-status-bar.spec.ts +++ b/e2e/network-status-bar.spec.ts @@ -2,6 +2,10 @@ import { test, expect } from '@playwright/test'; import { test as fixtureTest } from './fixtures/test'; import { setupApiMocks } from './fixtures/api-mocks'; import { smoothScrollAndAwaitSettled } from './fixtures/swipe-helpers'; +import { + expectLocatorCenteredBetweenChrome, + setOfflineAndWaitForBar, +} from './fixtures/layout-helpers'; test.describe('Network Status Bar - Viewport Switch', () => { test.use({ viewport: { width: 1280, height: 720 } }); @@ -126,6 +130,79 @@ fixtureTest.describe('Network Status Bar - Offline Item Detail to Mobile', () => }); }); +test.describe('Network Status Bar - Centered state layouts', () => { + test.use({ viewport: { width: 1280, height: 720 }, deviceScaleFactor: 1 }); + + test.beforeEach(async ({ page }) => { + await setupApiMocks(page); + }); + + test('keeps the full-page not-found state centered with the offline bar visible', async ({ page, context }) => { + await page.goto('/#/missing-route'); + + const stateRoot = page.getByRole('heading', { + name: 'Lost in the feed', + }).locator('xpath=..'); + await expect(stateRoot).toBeVisible(); + + await setOfflineAndWaitForBar(page, context); + await expectLocatorCenteredBetweenChrome(stateRoot); + }); +}); + +test.describe('Network Status Bar - Mobile content spacing', () => { + test.use({ viewport: { width: 375, height: 667 }, hasTouch: true }); + + test.beforeEach(async ({ page }) => { + await setupApiMocks(page); + }); + + test('keeps the story title gap stable when the offline bar appears', async ({ page, context }) => { + await page.goto('/#/item/12345'); + + const title = page.getByRole('heading', { level: 1 }).first(); + await expect(title).toBeVisible(); + await page.waitForFunction(() => { + const heading = document.querySelector('h1'); + const stage = heading?.closest('.page-stage'); + return !!stage + && stage.classList.contains('fade-skeleton') + && !stage.classList.contains('play-real'); + }); + + const beforeGap = await page.evaluate(() => { + const heading = document.querySelector('h1'); + const header = document.querySelector('header'); + if (!(heading instanceof HTMLElement) || !(header instanceof HTMLElement)) return null; + return heading.getBoundingClientRect().top - header.getBoundingClientRect().bottom; + }); + + expect(beforeGap).not.toBeNull(); + + await setOfflineAndWaitForBar(page, context); + + const afterMetrics = await page.evaluate(() => { + const heading = document.querySelector('h1'); + const header = document.querySelector('header'); + const panel = document.querySelector('[data-testid="swipe-panel"].active') ?? document.querySelector('[data-testid="swipe-panel"]'); + if (!(heading instanceof HTMLElement) || !(header instanceof HTMLElement) || !(panel instanceof HTMLElement)) { + return null; + } + + return { + gap: heading.getBoundingClientRect().top - header.getBoundingClientRect().bottom, + paddingTop: getComputedStyle(panel).paddingTop, + paddingBottom: getComputedStyle(panel).paddingBottom, + }; + }); + + expect(afterMetrics).not.toBeNull(); + expect(afterMetrics?.paddingTop).toBe('53px'); + expect(afterMetrics?.paddingBottom).toBe('32px'); + expect(Math.abs((afterMetrics?.gap ?? 0) - (beforeGap ?? 0))).toBeLessThanOrEqual(2); + }); +}); + test.describe('Swipe viewer - offline end-of-feed behavior', () => { test.use({ viewport: { width: 375, height: 667 }, hasTouch: true }); diff --git a/e2e/user-submissions.spec.ts b/e2e/user-submissions.spec.ts index e34aad1..ac31bf8 100644 --- a/e2e/user-submissions.spec.ts +++ b/e2e/user-submissions.spec.ts @@ -7,6 +7,10 @@ import { smoothScrollAndAwaitSettled, waitForScrollAtIndex, } from './fixtures/swipe-helpers'; +import { + expectLocatorCenteredBetweenChrome, + setOfflineAndWaitForBar, +} from './fixtures/layout-helpers'; test.describe('User Submissions - Desktop', () => { // Desktop viewport — `MobileUserSubmissionsWrapper` falls through to the @@ -247,7 +251,7 @@ test.describe('User Submissions - Mobile Swipe', () => { ), { timeout: 5_000 }).toBe(1); }); - test('shows empty state for a user with no submissions on mobile', async ({ page }) => { + test('shows empty state for a user with no submissions on mobile', async ({ page, context }) => { await stubEmptyUserSubmissions(page, 'noresults'); await page.goto('/#/submitted/noresults'); @@ -255,6 +259,14 @@ test.describe('User Submissions - Mobile Swipe', () => { // The mobile swipe viewer's empty branch surfaces the same copy as the // desktop list (centralized in `formatNoUserSubmissionsTitle`). await expect(page.getByText('No submissions found by "noresults"')).toBeVisible({ timeout: 10_000 }); + + await setOfflineAndWaitForBar(page, context); + + // Mobile empty states use the shared swipe centering helper. + const stateRoot = page.getByRole('heading', { + name: 'No submissions found by "noresults"', + }).locator('xpath=..'); + await expectLocatorCenteredBetweenChrome(stateRoot); }); test('shows error state when Algolia returns 503 on mobile swipe', async ({ page }) => { diff --git a/src/components/InfiniteStoryListPage.test.tsx b/src/components/InfiniteStoryListPage.test.tsx index a4b3e87..9d1a3fe 100644 --- a/src/components/InfiniteStoryListPage.test.tsx +++ b/src/components/InfiniteStoryListPage.test.tsx @@ -119,7 +119,10 @@ describe('InfiniteStoryListPage', () => { emptyTitle: 'No stories from this domain', }); - expect(screen.getByText('No stories from this domain')).toBeInTheDocument(); + const title = screen.getByText('No stories from this domain'); + + expect(title).toBeInTheDocument(); + expect(title.closest('.page-state-center-padded')).not.toBeNull(); }); it('omits the empty StateView when emptyTitle is undefined', () => { @@ -140,9 +143,12 @@ describe('InfiniteStoryListPage', () => { result: makeResult({ error: 'Network down' }), }); - expect(screen.getByText('Failed to load items')).toBeInTheDocument(); + const title = screen.getByText('Failed to load items'); + + expect(title).toBeInTheDocument(); expect(screen.getByText('Network down')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Try Again' })).toBeInTheDocument(); + expect(title.closest('.page-state-center-padded')).not.toBeNull(); }); it('renders the cold-error StateView with an overridden failTitle', () => { diff --git a/src/components/InfiniteStoryListPage.tsx b/src/components/InfiniteStoryListPage.tsx index 4f359d2..28c2bf2 100644 --- a/src/components/InfiniteStoryListPage.tsx +++ b/src/components/InfiniteStoryListPage.tsx @@ -86,13 +86,12 @@ export function InfiniteStoryListPage({ // layout (no skeleton overlay), so they sit above the wrapper. if (error && stories.length === 0 && !isRetrying) { return ( -
+
); @@ -106,65 +105,66 @@ export function InfiniteStoryListPage({ ); } + if (emptyTitle && stories.length === 0 && !loading) { + return ( +
+ +
+ ); + } + return (
{/* `loading` is gated on `stories.length === 0` so a refresh / - context change with cached cards doesn't blank them out. The - empty-state StateView sits inside `children` so it inherits - the `'done'` opacity baseline (no spurious cascade). */} + context change with cached cards doesn't blank them out. */} } > - {emptyTitle && stories.length === 0 ? ( - - ) : ( - <> -
- {stories.map((story, index) => ( - - ))} + <> +
+ {stories.map((story, index) => ( + + ))} +
+ + {hasMore && ( +
+ {loading && }
+ )} + + {!hasMore && stories.length > 0 && ( + + )} + + {error && stories.length > 0 && !isRetrying && ( +
+ +
+ )} - {hasMore && ( -
- {loading && } -
- )} - - {!hasMore && stories.length > 0 && ( - - )} - - {error && stories.length > 0 && !isRetrying && ( -
- -
- )} - - {isRetrying && stories.length > 0 && ( -
- -
- )} - - )} + {isRetrying && stories.length > 0 && ( +
+ +
+ )} +
); diff --git a/src/components/SwipeCommentViewer.tsx b/src/components/SwipeCommentViewer.tsx index 32a3309..d482369 100644 --- a/src/components/SwipeCommentViewer.tsx +++ b/src/components/SwipeCommentViewer.tsx @@ -136,8 +136,8 @@ export function SwipeCommentViewer({ initialCommentId }: SwipeCommentViewerProps if (error && siblingIds.length <= 1 && !isRetrying) { return ( -
- void retry().catch(() => { /* error state set internally */ }) }} /> +
+ void retry().catch(() => { /* error state set internally */ }) }} className="swipe-state-center" />
); } diff --git a/src/components/SwipeStoryViewer.test.tsx b/src/components/SwipeStoryViewer.test.tsx new file mode 100644 index 0000000..38247f3 --- /dev/null +++ b/src/components/SwipeStoryViewer.test.tsx @@ -0,0 +1,34 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { screen } from '@testing-library/react'; +import { render } from '../test/test-utils'; +import { SwipeStoryViewerCore } from './SwipeStoryViewer'; + +Object.defineProperty(window, 'innerWidth', { value: 375, writable: true }); + +beforeEach(() => { + Element.prototype.scrollTo = vi.fn(); + window.scrollTo = vi.fn(); +}); + +describe('SwipeStoryViewerCore', () => { + it('uses swipe-mode viewport centering for empty states', () => { + render( + , + ); + + const title = screen.getByRole('heading', { + name: 'No submissions found by "levhawk"', + }); + + expect(title.closest('div')).toHaveClass('swipe-state-center'); + }); +}); \ No newline at end of file diff --git a/src/components/SwipeStoryViewer.tsx b/src/components/SwipeStoryViewer.tsx index e933e6a..0299029 100644 --- a/src/components/SwipeStoryViewer.tsx +++ b/src/components/SwipeStoryViewer.tsx @@ -456,12 +456,13 @@ export function SwipeStoryViewerCore({ if (injectedError) { return ( -
+
void navigate(0) }} + className="swipe-state-center" />
); @@ -501,8 +502,8 @@ export function SwipeStoryViewerCore({ if (error && mergedStories.length === 0 && !isRetrying) { return ( -
- { resetRetry(); void loadMore().catch(() => { /* error state set internally */ }); } }} /> +
+ { resetRetry(); void loadMore().catch(() => { /* error state set internally */ }); } }} className="swipe-state-center" />
); } @@ -531,8 +532,8 @@ export function SwipeStoryViewerCore({ !initialItemId ) { return ( -
- +
+
); } diff --git a/src/index.css b/src/index.css index 3cadf81..a5edc06 100644 --- a/src/index.css +++ b/src/index.css @@ -284,10 +284,26 @@ button { width: 100%; } +/* Full-screen fallback states in swipe mode sit under a fixed header, so they + need their own viewport box instead of relying on the container's auto height. */ +.swipe-state-center { + min-height: var(--swipe-viewport-height, 100vh); + padding-top: var(--header-height); + padding-bottom: var(--network-bar-height); + padding-inline: 1.5rem; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + text-align: center; +} + .swipe-snap-panel { display: none; - /* Push content below the fixed header */ - padding-top: calc(var(--header-height) + var(--network-bar-height)); + /* Push content below the fixed header and reserve bottom room for the + fixed network bar without shifting the title downward. */ + padding-top: var(--header-height); + padding-bottom: var(--network-bar-height); } .swipe-snap-panel.active { @@ -302,10 +318,10 @@ button { .swipe-snap-panel.incoming { display: block; position: fixed; - top: calc(var(--header-height) + var(--network-bar-height)); + top: var(--header-height); left: 0; right: 0; - bottom: 0; + bottom: var(--network-bar-height); padding-top: 0; overflow: hidden; z-index: 1; @@ -352,7 +368,7 @@ body.swipe-mode { body.swipe-mode::after { content: ''; position: fixed; - top: calc(var(--header-height) + var(--network-bar-height)); + top: var(--header-height); right: 2px; width: 4px; height: calc((var(--swipe-viewport-height, 100vh) - var(--header-height) - var(--network-bar-height)) * 0.2); @@ -386,7 +402,7 @@ body.swipe-mode.is-scrolling::after { /* Full-page centered state (error / not-found / empty) */ .page-state-center { - min-height: calc(100dvh - var(--header-height)); + min-height: calc(100dvh - var(--header-height) - var(--network-bar-height)); display: flex; flex-direction: column; align-items: center; @@ -398,7 +414,7 @@ body.swipe-mode.is-scrolling::after { max-width: 72rem; /* 6xl */ margin-inline: auto; padding-inline: 1rem; - min-height: calc(100dvh - var(--header-height)); + min-height: calc(100dvh - var(--header-height) - var(--network-bar-height)); display: flex; align-items: center; justify-content: center;