From c1ea2e0ed4fb641a033a7ff34e1b839a9f0f3cb2 Mon Sep 17 00:00:00 2001
From: therealsama <263038098+therealsama@users.noreply.github.com>
Date: Sun, 1 Nov 2026 19:02:25 +0100
Subject: [PATCH] fix: scroll to the top when opening a feed on mobile
On a phone, the story panels scroll the page itself, so the page keeps one
scroll position for whatever is on screen. Tapping a feed tab (or the logo)
mounts a new viewer, which shows its first story without touching that
position: scroll down a Top story, tap Best, and the first Best story opens
partway down its comments.
The viewer now scrolls to the top when it opens without a story in the URL,
which is how a tab, the logo, and a domain or user list open it. A viewer
opened on a story (Back, a reload, a link) leaves the page alone, so its
restore still sets the offset, and the story you left keeps its saved
position for Back.
---
e2e/mobile-swipe.spec.ts | 18 ++++++++++++++++++
src/components/SwipeStoryViewer.test.tsx | 20 ++++++++++++++++++++
src/components/SwipeStoryViewer.tsx | 6 ++++++
3 files changed, 44 insertions(+)
diff --git a/e2e/mobile-swipe.spec.ts b/e2e/mobile-swipe.spec.ts
index 5b304d2..4338dff 100644
--- a/e2e/mobile-swipe.spec.ts
+++ b/e2e/mobile-swipe.spec.ts
@@ -412,6 +412,24 @@ test.describe('Mobile Swipe Viewer', () => {
expect(ids.filter((id) => mockTopItemIds.includes(id))).toEqual([]);
});
+ test('a feed opened from its tab starts at the top, not where the last story was scrolled to', async ({ page }) => {
+ await page.goto('/#/');
+ await waitForSwipeReady(page, 6);
+ await expect(page).toHaveURL(/\/item\/12345/, { timeout: 5000 });
+
+ // Both stories taller than the viewport, so a short one can't clamp the
+ // page back to the top on its own.
+ await page.addStyleTag({ content: '[data-testid="swipe-panel"].active { min-height: 3000px !important; }' });
+ await page.evaluate(() => window.scrollTo({ top: 1000, behavior: 'instant' }));
+ await expect.poll(() => page.evaluate(() => window.scrollY)).toBe(1000);
+
+ await page.getByRole('link', { name: 'best', exact: true }).click();
+
+ await expect(page).toHaveURL(/\/item\/33001/, { timeout: 5000 });
+ await expect(getActiveSwipePanel(page)).toHaveAttribute('data-item-id', '33001');
+ await expect.poll(() => page.evaluate(() => window.scrollY)).toBe(0);
+ });
+
test('a feed refresh that lands after a swipe keeps the story on screen', async ({ page }) => {
// An hour-old cached Top feed in the reverse of the live order, and Firebase
// reads that take 1.5s, so the refresh lands after the swipe.
diff --git a/src/components/SwipeStoryViewer.test.tsx b/src/components/SwipeStoryViewer.test.tsx
index 7baf5f3..a0827e1 100644
--- a/src/components/SwipeStoryViewer.test.tsx
+++ b/src/components/SwipeStoryViewer.test.tsx
@@ -476,6 +476,8 @@ describe('SwipeStoryViewerCore — swipe-position restore', () => {
document.dispatchEvent(new Event('visibilitychange'));
});
};
+ // Opening the feed scrolled to the top already; only the restore counts.
+ vi.mocked(window.scrollTo).mockClear();
try {
showTab('hidden');
showTab('visible');
@@ -591,6 +593,24 @@ describe('SwipeStoryViewerCore — swipe-position restore', () => {
expect(readSwipePosition({ viewer: { from: 'best' } })).toBeNull();
expect(readSwipePosition({ viewer: { from: 'top' } })?.storyId).toBe(2);
});
+
+ it('opens a feed from its tab at the top of the page', () => {
+ const stories = [createStoryItem({ id: 1 }), createStoryItem({ id: 2 })];
+
+ render();
+
+ expect(window.scrollTo).toHaveBeenCalledWith(0, 0);
+ });
+
+ it('leaves the page where it is when the viewer opens on a story', () => {
+ const stories = [createStoryItem({ id: 1 }), createStoryItem({ id: 2 })];
+
+ render(
+ ,
+ );
+
+ expect(window.scrollTo).not.toHaveBeenCalled();
+ });
});
describe('SwipeStoryViewerCore — end-of-feed panel', () => {
diff --git a/src/components/SwipeStoryViewer.tsx b/src/components/SwipeStoryViewer.tsx
index 1c1a2d4..88b4eca 100644
--- a/src/components/SwipeStoryViewer.tsx
+++ b/src/components/SwipeStoryViewer.tsx
@@ -368,6 +368,12 @@ export function SwipeStoryViewerCore({
useLayoutEffect(() => {
if (opensFresh) clearSwipePosition(backState);
}, [opensFresh, backState]);
+
+ // The page is still scrolled where the previous screen left it, and panels
+ // don't reset it.
+ useLayoutEffect(() => {
+ if (opensFresh) window.scrollTo(0, 0);
+ }, [opensFresh]);
useEffect(() => {
// Save position + scrollY when page is about to be hidden/cached