Repository navigation
[IRN-6854] Fix card-list rail pagination on short final page - #4975
Ezreal Yang (Supremeyh) merged 4 commits into
Conversation
When a row/rail's final page holds fewer cards than initiallyShownCards (odd totals, e.g. 3 cards shown 2-wide), physical scrolling to the end never advanced the pagination indicator: firstVisibleIndex could not reach the last page's start card, so Math.floor(firstVisibleIndex / shown) stayed on the previous page. Fix: in usePageScrollSync, if the last card is visible we've reached the end, so snap the indicator to the final page. Adds two Storybook repro stories (2-wide/3-card and 4-wide/10-card) and a unit test for the short-final-page case. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4975 to see this build running in a browser. |
|
Visit https://backpack.github.io/storybook-prs/4975 to see this build running in a browser. |
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Fixes a pagination indicator bug in BpkCardList row/rail mode when the final page contains fewer cards than initiallyShownCards, preventing the indicator from advancing on physical scroll to the end.
Changes:
- Update
usePageScrollSyncto treat “last card visible” as being on the final page. - Add a unit test covering the short-final-page scroll case.
- Add Storybook repro stories for narrow and wider short-final-page scenarios.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/backpack-web/src/bpk-component-card-list/src/BpkCardListRowRail/utils.tsx | Adjusts page index derivation to handle short final pages. |
| packages/backpack-web/src/bpk-component-card-list/src/BpkCardListRowRail/utils-test.tsx | Adds regression test for the “short final page can’t align first card” behavior. |
| packages/backpack-web/src/bpk-component-card-list/src/BpkCardList.stories.tsx | Adds Storybook stories to reproduce and verify the fix manually. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // A short final page can't scroll its first card to the viewport start, so | ||
| // firstVisibleIndex stays on the previous page. Treat a visible last card as | ||
| // the final page. | ||
| const newPageIndex = | ||
| visibilityList[visibilityList.length - 1] === 1 | ||
| ? Math.ceil(visibilityList.length / initiallyShownCards) - 1 | ||
| : Math.floor(firstVisibleIndex / initiallyShownCards); |
| expect(mockSetCurrentIndex).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should snap to the last page when the final (short) page cannot scroll its first card to the start', () => { |
| const newPageIndex = | ||
| visibilityList[visibilityList.length - 1] === 1 | ||
| ? Math.ceil(visibilityList.length / initiallyShownCards) - 1 | ||
| : Math.floor(firstVisibleIndex / initiallyShownCards); |
There was a problem hiding this comment.
This fires unconditionally whenever the last card is in the viewport and isUserScrollingRef.current is true. On a wide viewport that fits all cards simultaneously, any wheel or touchstart event sets isUserScrollingRef.current = true, then visibilityList[last] === 1 immediately triggers the override — forcing the indicator to the last page even though the user has never scrolled the rail.
There was a problem hiding this comment.
Last page guard added
|
|
||
| it('should snap to the last page when the final (short) page cannot scroll its first card to the start', () => { |
There was a problem hiding this comment.
The added test (utils-test.tsx line 584) covers only the intended happy path (short final page, user scrolled to end → indicator advances). There is no test asserting the indicator does not jump when all cards are visible but the user has not scrolled the rail to the end.
Without a negative test, the false positive scenario in Issue 1 has no regression guard. Once the logic is tightened (per Issue 1), a negative test locks in the correct boundary and prevents future regressions.
Suggested addition (after fixing Issue 1):
it('should NOT snap to the last page when all cards are visible in a wide viewport', () => {
// 4 cards, 2 shown per page, wide viewport: all visible from the start.
// A wheel event should not force the indicator to page 1.
const allVisibleList = [1, 1, 1, 1];
// ... render hook with currentIndex: 0, initiallyShownCards: 2
act(() => { mockContainer.dispatchEvent(new Event('wheel')); });
rerender({ visibilityList: allVisibleList });
expect(mockSetCurrentIndex).not.toHaveBeenCalled();
});There was a problem hiding this comment.
test case added
Only treat a visible last card as the final page when the final page is genuinely short (card count not divisible by initiallyShownCards). This prevents falsely snapping the indicator when all cards fit in a wide viewport. Adds a negative regression test to lock in the correct boundary. Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4975 to see this build running in a browser. |
Stephen Hailey (steviehailey-skyscanner)
left a comment
There was a problem hiding this comment.
lgtm
| const newPageIndex = | ||
| visibilityList[visibilityList.length - 1] === 1 | ||
| ? Math.ceil(visibilityList.length / initiallyShownCards) - 1 | ||
| : Math.floor(firstVisibleIndex / initiallyShownCards); |
There was a problem hiding this comment.
Last page guard added
Summary
Fixes a pagination bug in
BpkCardListrow/rail mode.Problem
When the final page of a rail holds fewer cards than
initiallyShownCards(odd totals — e.g. 3 cards shown 2-wide), physically scrolling to the end does not advance the pagination indicator.Cause: in
usePageScrollSync, the page index is derived fromMath.floor(firstVisibleIndex / initiallyShownCards). On a short final page there isn't enough content to scroll that page's first card to the viewport start, sofirstVisibleIndexnever reaches it and the indicator stays on the previous page.Change
BpkCardListRowRail/utils.tsx— inusePageScrollSync, treat a visible last card as the final page; otherwise the existing calculation is used.BpkCardListRowRail/utils-test.tsx— unit test for the short-final-page case.BpkCardList.stories.tsx— two repro stories:RowToRailShortFinalPage(2-wide, 3 cards)RowToRailShortFinalPageWider(4-wide, 10 cards)How to review
Storybook →
bpk-component-card-list→ the twoRowToRailShortFinalPage*stories. On desktop, physically scroll to the last card; the indicator now tracks to the final page.pnpm jest packages/backpack-web/src/bpk-component-card-listis green (61 tests).Notes
Draft — no changelog entry yet, wanted a sanity check on the approach before polishing.
🤖 Generated with Claude Code