From c03c40d2876503ee82430059875677fbab6f79ed Mon Sep 17 00:00:00 2001 From: Maurice Date: Thu, 29 Oct 2026 14:28:22 +0100 Subject: [PATCH] fix(mobile): line the day rail up on today once the trip splash lifts The opening day is seeded while the loading splash still covers the shell, so the rail's auto-scroll ran before any chip existed and never ran again: its dependencies did not change when the rail first mounted. A trip opened mid-way showed today's day below a rail stuck at day 1. The rail element is now a dependency of the alignment, so the active chip is brought into view when the rail appears, including after re-entering the trip. The first alignment jumps instead of gliding across a rail nobody has seen yet, and runs before paint; later day changes still glide. Closes #2880 --- client/src/mobile/screens/trip/MTripShell.tsx | 23 +++++--- .../unit/mobile/trip/MTripShell.test.tsx | 58 ++++++++++++++++++- 2 files changed, 72 insertions(+), 9 deletions(-) diff --git a/client/src/mobile/screens/trip/MTripShell.tsx b/client/src/mobile/screens/trip/MTripShell.tsx index 6f67bc27f..477d302f4 100644 --- a/client/src/mobile/screens/trip/MTripShell.tsx +++ b/client/src/mobile/screens/trip/MTripShell.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState, type ComponentType, type ReactNode } from 'react' +import { useEffect, useLayoutEffect, useRef, useState, type ComponentType, type ReactNode } from 'react' import { findFocusDayId, findTodayDayId } from '../../../components/Planner/today' import { takeResumeDay } from '../../../utils/resumeRoute' import { @@ -266,22 +266,29 @@ export default function MTripShell({ // screen — the rail overflows from roughly six days on — so the active chip // pulls itself back into view. Only when it is really clipped, so tapping a // chip you can already see never shifts the rail under your thumb. + // The opening day is chosen while the splash still covers the shell (#2880), + // so the rail itself is a dependency: the chip is lined up when the rail + // appears, in place rather than gliding across a rail nobody has seen yet. const chipRefs = useRef(new Map()) - useEffect(() => { + const [dayRail, setDayRail] = useState(null) + const alignedRailRef = useRef(null) + useLayoutEffect(() => { const chip = planner.selectedDayId != null ? chipRefs.current.get(planner.selectedDayId) : undefined - const rail = chip?.parentElement - if (!chip || !rail) return + if (!chip || !dayRail) return + const firstView = alignedRailRef.current !== dayRail + alignedRailRef.current = dayRail const c = chip.getBoundingClientRect() - const r = rail.getBoundingClientRect() + const r = dayRail.getBoundingClientRect() if (c.left >= r.left - 1 && c.right <= r.right + 1) return + const reducedMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)')?.matches // block:'nearest' is not optional: the shell root is `fixed inset-0`, and // 'center' would scroll the document behind it instead. chip.scrollIntoView({ - behavior: window.matchMedia?.('(prefers-reduced-motion: reduce)')?.matches ? 'auto' : 'smooth', + behavior: firstView || reducedMotion ? 'auto' : 'smooth', inline: 'center', block: 'nearest', }) - }, [planner.selectedDayId, days]) + }, [planner.selectedDayId, days, dayRail]) const trTab = planner.activeTab @@ -491,7 +498,7 @@ export default function MTripShell({ {/* ── Day chips (z-25 — covered by non-plan tab overlays, stays mounted) ── */} {days.length > 0 && (
-
+
{days.map((day, idx) => { const active = day.id === planner.selectedDayId const isToday = day.id === todayDayId diff --git a/client/tests/unit/mobile/trip/MTripShell.test.tsx b/client/tests/unit/mobile/trip/MTripShell.test.tsx index 3c5792233..085e79b57 100644 --- a/client/tests/unit/mobile/trip/MTripShell.test.tsx +++ b/client/tests/unit/mobile/trip/MTripShell.test.tsx @@ -569,14 +569,70 @@ describe('MTripShell', () => { afterEach(() => { vi.restoreAllMocks() }) - it('FE-MOB-SHELL-044: a clipped active chip pulls itself into the middle of the rail', () => { + it('FE-MOB-SHELL-044: a clipped active chip is in the middle of the rail the moment it appears', () => { const scrollIntoView = vi.mocked(Element.prototype.scrollIntoView) scrollIntoView.mockClear() stubRects({ left: 480, right: 540 }) renderShell({ selectedDayId: 12 } as Partial) + expect(scrollIntoView).toHaveBeenCalledWith({ behavior: 'auto', inline: 'center', block: 'nearest' }) + }) + + it('FE-MOB-SHELL-093: a later day change on the rail glides to the chip', () => { + const scrollIntoView = vi.mocked(Element.prototype.scrollIntoView) + stubRects({ left: 480, right: 540 }) + const { planner, rerenderShell } = renderShell({ selectedDayId: 11 } as Partial) + scrollIntoView.mockClear() + + ;(planner as { selectedDayId: number | null }).selectedDayId = 12 + rerenderShell() + + expect(scrollIntoView).toHaveBeenCalledTimes(1) expect(scrollIntoView).toHaveBeenCalledWith({ behavior: 'smooth', inline: 'center', block: 'nearest' }) }) + // #2880: the day is seeded while the splash still covers the shell, so the + // rail only exists after the selection has settled. + it('FE-MOB-SHELL-094: the active day is brought into view once the splash lifts (#2880)', () => { + const scrollIntoView = vi.mocked(Element.prototype.scrollIntoView) + scrollIntoView.mockClear() + stubRects({ left: 480, right: 540 }) + const { planner, rerenderShell } = renderShell({ selectedDayId: 12, splashDone: false } as Partial) + expect(scrollIntoView).not.toHaveBeenCalled() + + ;(planner as { splashDone: boolean }).splashDone = true + rerenderShell() + + expect(scrollIntoView).toHaveBeenCalledTimes(1) + expect(scrollIntoView).toHaveBeenCalledWith({ behavior: 'auto', inline: 'center', block: 'nearest' }) + }) + + it('FE-MOB-SHELL-095: after the splash a chip already in view leaves the rail alone', () => { + const scrollIntoView = vi.mocked(Element.prototype.scrollIntoView) + scrollIntoView.mockClear() + stubRects({ left: 40, right: 100 }) + const { planner, rerenderShell } = renderShell({ selectedDayId: 12, splashDone: false } as Partial) + + ;(planner as { splashDone: boolean }).splashDone = true + rerenderShell() + + expect(scrollIntoView).not.toHaveBeenCalled() + }) + + it('FE-MOB-SHELL-096: re-entering the trip lines the rail up again without a glide', () => { + const scrollIntoView = vi.mocked(Element.prototype.scrollIntoView) + stubRects({ left: 480, right: 540 }) + const { planner, rerenderShell } = renderShell({ selectedDayId: 12 } as Partial) + + ;(planner as { isLoading: boolean }).isLoading = true + rerenderShell() + scrollIntoView.mockClear() + ;(planner as { isLoading: boolean }).isLoading = false + rerenderShell() + + expect(scrollIntoView).toHaveBeenCalledTimes(1) + expect(scrollIntoView).toHaveBeenCalledWith({ behavior: 'auto', inline: 'center', block: 'nearest' }) + }) + it('FE-MOB-SHELL-045: a chip already in view never shifts the rail under the thumb', () => { const scrollIntoView = vi.mocked(Element.prototype.scrollIntoView) scrollIntoView.mockClear()