Skip to content

fix: stabilize pagination nav width to prevent layout shift on page change#1435

Open
octo-patch wants to merge 1 commit intoWei-Shaw:mainfrom
octo-patch:fix/issue-1363-stable-pagination-width
Open

fix: stabilize pagination nav width to prevent layout shift on page change#1435
octo-patch wants to merge 1 commit intoWei-Shaw:mainfrom
octo-patch:fix/issue-1363-stable-pagination-width

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #1363

Problem

The pagination component's <nav> element changes width when navigating between pages, causing a visible layout "jump". This happens because the number of rendered page buttons is variable:

  • Page 1 of 23: « 1 2 3 … 23 » → 5 page items
  • Page 6 of 23: « 1 … 4 5 6 7 8 … 23 » → 9 page items

The old visiblePages algorithm used page ± 2 as a sliding window, producing between 5 and 9 items depending on the current page, causing the nav to grow and shift other layout elements.

Solution

Replace the variable-width algorithm with one that always returns exactly 7 items when totalPages > 7:

Condition Result (7 items)
page ≤ 4 1 2 3 4 5 … total
page ≥ total - 3 1 … t-4 t-3 t-2 t-1 t
middle 1 … p-1 p p+1 … total

This ensures the <nav> maintains a constant width regardless of which page the user is on, eliminating the layout shift.

Testing

Verified the output length of visiblePages is stable across all pages for a 23-page dataset by manually tracing the algorithm:

  • Page 1: [1,2,3,4,5,'...',23] → 7 items ✓
  • Page 5: [1,'...',4,5,6,'...',23] → 7 items ✓
  • Page 12: [1,'...',11,12,13,'...',23] → 7 items ✓
  • Page 20: [1,'...',19,20,21,22,23] → 7 items ✓
  • Page 23: [1,'...',19,20,21,22,23] → 7 items ✓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

【体验优化】分页组件在翻页时宽度变化,导致位置轻微跳动,希望优化一下~

1 participant