Pagination: larger text, pages stay put when switching

Three-column grid keeps the page numbers centred whatever the range
label width; with many pages there are always 7 slots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ilya Mukhortov 2026-10-28 12:00:13 +10:00
parent 5a2b156bef
commit b960dd334f
1 changed files with 21 additions and 10 deletions

View File

@ -4,12 +4,16 @@ import { cn } from "../lib/utils"
const DEFAULT_SIZES = [15, 30, 50] as const const DEFAULT_SIZES = [15, 30, 50] as const
/** Номера страниц с многоточиями: 1 … 4 5 6 … 12 */ /**
* Номера страниц с многоточиями: 1 … 4 5 6 … 12. Мест всегда 7 — при переключении стрелки не сдвигаются:
* у начала 1 2 3 4 5 … 12, у конца 1 … 8 9 10 11 12.
*/
function pageList(current: number, pages: number): (number | "gap")[] { function pageList(current: number, pages: number): (number | "gap")[] {
if (pages <= 7) return Array.from({ length: pages }, (_, i) => i) if (pages <= 7) return Array.from({ length: pages }, (_, i) => i)
const set = new Set([0, pages - 1, current - 1, current, current + 1]) const last = pages - 1
const sorted = [...set].filter((p) => p >= 0 && p < pages).sort((a, b) => a - b) if (current < 4) return [0, 1, 2, 3, 4, "gap", last]
return sorted.flatMap((p, i) => (i > 0 && p - sorted[i - 1] > 1 ? ["gap" as const, p] : [p])) if (current > last - 4) return [0, "gap", last - 4, last - 3, last - 2, last - 1, last]
return [0, "gap", current - 1, current, current + 1, "gap", last]
} }
/** /**
@ -38,11 +42,18 @@ export function Pagination({
const from = total ? current * size + 1 : 0 const from = total ? current * size + 1 : 0
const to = Math.min((current + 1) * size, total) const to = Math.min((current + 1) * size, total)
const pageButton = "grid h-7 min-w-7 place-items-center rounded-md px-1.5 text-xs font-medium tabular transition" const pageButton = "grid h-8 min-w-8 place-items-center rounded-md px-2 text-ui font-medium tabular transition"
return ( return (
<div className={cn("flex flex-wrap items-center justify-between gap-3 px-4 py-2.5", className)}> // Три колонки: страницы строго по центру, сколько бы места ни занимали «1 – 15 из 28» и размеры.
<span className="rounded-md bg-muted px-2 py-1 text-xs text-muted-foreground tabular"> // На телефоне не влезает в строку — переносится, как раньше.
<div
className={cn(
"flex flex-wrap items-center justify-between gap-3 px-4 py-2.5 sm:grid sm:grid-cols-[1fr_auto_1fr]",
className,
)}
>
<span className="justify-self-start rounded-md bg-muted px-2 py-1 text-ui text-muted-foreground tabular">
{from} – {to} из {total} {from} – {to} из {total}
</span> </span>
@ -58,7 +69,7 @@ export function Pagination({
</button> </button>
{pageList(current, pages).map((p, i) => {pageList(current, pages).map((p, i) =>
p === "gap" ? ( p === "gap" ? (
<span key={`gap-${i}`} className="px-1 text-xs text-muted-foreground"> <span key={`gap-${i}`} className="grid h-8 min-w-8 place-items-center text-ui text-muted-foreground">
… …
</span> </span>
) : ( ) : (
@ -88,7 +99,7 @@ export function Pagination({
</div> </div>
{onSizeChange ? ( {onSizeChange ? (
<div className="flex items-center gap-0.5 rounded-lg bg-muted p-0.5"> <div className="flex items-center gap-0.5 justify-self-end rounded-lg bg-muted p-0.5">
{sizeOptions.map((s) => ( {sizeOptions.map((s) => (
<button <button
key={s} key={s}
@ -98,7 +109,7 @@ export function Pagination({
onPageChange(0) onPageChange(0)
}} }}
className={cn( className={cn(
"h-6 rounded-md px-2 text-xs font-medium tabular transition", "h-7 rounded-md px-2.5 text-ui font-medium tabular transition",
s === size ? "bg-card text-foreground shadow-xs" : "text-muted-foreground hover:text-foreground", s === size ? "bg-card text-foreground shadow-xs" : "text-muted-foreground hover:text-foreground",
)} )}
> >