mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
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
This commit is contained in:
@@ -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<number, HTMLButtonElement>())
|
||||
useEffect(() => {
|
||||
const [dayRail, setDayRail] = useState<HTMLDivElement | null>(null)
|
||||
const alignedRailRef = useRef<HTMLDivElement | null>(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 && (
|
||||
<div className="absolute inset-x-4 z-[25] flex gap-[6px] top-[calc(var(--m-safe-top,12px)+50px)]">
|
||||
<div className="flex flex-1 items-center gap-[2px] overflow-x-auto rounded-full border border-[color:var(--m-gbr)] bg-[color:var(--m-glass)] p-[3px] backdrop-blur-[24px] backdrop-saturate-[1.7]">
|
||||
<div ref={setDayRail} className="flex flex-1 items-center gap-[2px] overflow-x-auto rounded-full border border-[color:var(--m-gbr)] bg-[color:var(--m-glass)] p-[3px] backdrop-blur-[24px] backdrop-saturate-[1.7]">
|
||||
{days.map((day, idx) => {
|
||||
const active = day.id === planner.selectedDayId
|
||||
const isToday = day.id === todayDayId
|
||||
|
||||
@@ -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<TripPlanner>)
|
||||
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<TripPlanner>)
|
||||
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<TripPlanner>)
|
||||
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<TripPlanner>)
|
||||
|
||||
;(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<TripPlanner>)
|
||||
|
||||
;(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()
|
||||
|
||||
Reference in New Issue
Block a user