Skip to content
Product block

Pagination With Page Window

Page size, range summary and a fixed-width number window that stays the same size from page 1 to page 200.

184 lineslucide-reactAdded 8 Aug 2026Updated 10 Sept 2026
  • pagination
  • paging
  • table
  • page size

What's included

  • components/data-table-pagination.tsx
  • Needs lucide-react

Works with

  • React
  • Next.js
  • Tailwind CSS
  • TypeScript

npx hoverlab add data-table-pagination

Or over MCP, from your editor's agent — no account needed.

License

Free to read, copy and install, for personal and non-commercial projects. Shipping it in client work or a paid product needs Pro ($79 once). The source lands in your repo and stops being ours — no attribution, nothing to upgrade.

Was this useful?

Start a page with this section — add more, order them, and leave with the page source.

Preview

Showing 1–25 of 2,847

Rendered live in your current theme — this is the same component whose source is below, not a screenshot of it.

Source

components/data-table-pagination.tsx
'use client'

/**
 * <DataTablePagination> — page size, range summary and page controls.
 *
 * The page-number window is the fiddly part: with 200 pages you cannot
 * render 200 buttons, and naive slicing makes the control jump around as
 * you move through it. `pageWindow()` keeps a fixed-width run of numbers
 * centred on the current page, clamped at both ends, with ellipses only
 * where pages are genuinely skipped — so the control stays the same width
 * from page 1 to page 200.
 *
 * The "Showing 1–25 of 2,847" line is announced through `aria-live`,
 * because for a keyboard or screen-reader user the only feedback that a
 * page change worked is the range text.
 */

import * as React from 'react'
import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight } from 'lucide-react'

export interface DataTablePaginationProps {
  totalItems?: number
  pageSizeOptions?: number[]
  initialPageSize?: number
  onChange?: (state: { page: number; pageSize: number }) => void
  className?: string
}

/**
 * A fixed-width run of page numbers centred on `page`.
 * Returns numbers and `'gap'` markers, never more than `span + 2` entries.
 */
function pageWindow(page: number, pageCount: number, span = 5): Array<number | 'gap'> {
  if (pageCount <= span + 2) {
    return Array.from({ length: pageCount }, (_, i) => i + 1)
  }

  const half = Math.floor(span / 2)
  let start = Math.max(1, page - half)
  const end = Math.min(pageCount, start + span - 1)

  // Re-clamp so the window keeps its width at the top end.
  start = Math.max(1, end - span + 1)

  const out: Array<number | 'gap'> = []
  if (start > 1) out.push(1, 'gap')
  for (let i = start; i <= end; i += 1) out.push(i)
  if (end < pageCount) out.push('gap', pageCount)

  return out
}

export function DataTablePagination({
  totalItems = 2847,
  pageSizeOptions = [10, 25, 50, 100],
  initialPageSize = 25,
  onChange,
  className = '',
}: DataTablePaginationProps) {
  // Per-instance ids. A literal id in a reusable component is a
  // collision waiting for the second copy on the page — and a <label>
  // then resolves to whichever input rendered first.
  const uid = React.useId()
  const [page, setPage] = React.useState(1)
  const [pageSize, setPageSize] = React.useState(initialPageSize)

  const pageCount = Math.max(1, Math.ceil(totalItems / pageSize))
  const first = totalItems === 0 ? 0 : (page - 1) * pageSize + 1
  const last = Math.min(page * pageSize, totalItems)

  function goTo(next: number) {
    const clamped = Math.max(1, Math.min(pageCount, next))
    setPage(clamped)
    onChange?.({ page: clamped, pageSize })
  }

  function changeSize(size: number) {
    // Keep the first visible record in view rather than resetting to page 1
    // — jumping back to the top after "show 100" loses the user's place.
    const anchor = (page - 1) * pageSize
    const nextPage = Math.floor(anchor / size) + 1
    setPageSize(size)
    setPage(nextPage)
    onChange?.({ page: nextPage, pageSize: size })
  }

  // Named `pages`, not `window` — shadowing the global inside a component
  // is legal and reliably confusing.
  const pages = pageWindow(page, pageCount)

  return (
    <div
      className={`flex flex-col items-center justify-between gap-4 border-t border-border/60 px-4 py-3 sm:flex-row ${className}`}
    >
      <div className="flex items-center gap-2 text-sm">
        <label htmlFor={`${uid}-page-size`} className="text-muted-foreground">
          Rows per page
        </label>
        <select
          id={`${uid}-page-size`}
          value={pageSize}
          onChange={(e) => changeSize(Number(e.target.value))}
          className="rounded-lg border border-border/60 bg-background px-2 py-1 text-sm outline-none focus-visible:ring-2 focus-visible:ring-primary"
        >
          {pageSizeOptions.map((size) => (
            <option key={size} value={size}>
              {size}
            </option>
          ))}
        </select>
      </div>

      <p aria-live="polite" className="text-sm text-muted-foreground">
        Showing <span className="font-medium text-foreground">{first.toLocaleString('en-US')}</span>–
        <span className="font-medium text-foreground">{last.toLocaleString('en-US')}</span> of{' '}
        <span className="font-medium text-foreground">{totalItems.toLocaleString('en-US')}</span>
      </p>

      <nav aria-label="Pagination" className="flex items-center gap-1">
        <PageButton onClick={() => goTo(1)} disabled={page === 1} label="First page">
          <ChevronsLeft aria-hidden className="h-4 w-4 rtl:rotate-180" />
        </PageButton>
        <PageButton onClick={() => goTo(page - 1)} disabled={page === 1} label="Previous page">
          <ChevronLeft aria-hidden className="h-4 w-4 rtl:rotate-180" />
        </PageButton>

        {pages.map((entry, i) =>
          entry === 'gap' ? (
            <span key={`gap-${i}`} aria-hidden className="px-1.5 text-sm text-muted-foreground">
              …
            </span>
          ) : (
            <button
              key={entry}
              type="button"
              onClick={() => goTo(entry)}
              aria-label={`Page ${entry}`}
              aria-current={entry === page ? 'page' : undefined}
              className={`h-8 min-w-8 rounded-lg px-2 text-sm font-medium transition-colors ${
                entry === page
                  ? 'bg-primary text-primary-foreground'
                  : 'text-muted-foreground hover:bg-muted hover:text-foreground'
              }`}
            >
              {entry}
            </button>
          ),
        )}

        <PageButton onClick={() => goTo(page + 1)} disabled={page === pageCount} label="Next page">
          <ChevronRight aria-hidden className="h-4 w-4 rtl:rotate-180" />
        </PageButton>
        <PageButton onClick={() => goTo(pageCount)} disabled={page === pageCount} label="Last page">
          <ChevronsRight aria-hidden className="h-4 w-4 rtl:rotate-180" />
        </PageButton>
      </nav>
    </div>
  )
}

function PageButton({
  onClick,
  disabled,
  label,
  children,
}: {
  onClick: () => void
  disabled: boolean
  label: string
  children: React.ReactNode
}) {
  return (
    <button
      type="button"
      onClick={onClick}
      disabled={disabled}
      aria-label={label}
      className="rounded-lg p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-40"
    >
      {children}
    </button>
  )
}

Before you paste

  • Styling is Tailwind utility classes on semantic tokens (bg-card, text-muted-foreground) — it inherits your theme instead of overriding it.
  • Install: npm i lucide-react
  • Every prop has a default, so it renders standalone before you wire it up.

Where it goes

Drop it at components/data-table-pagination.tsx and import it where you need the section:

import { DataTablePagination } from '@/components/data-table-pagination'

Customize

2 of this block’s props are simple enough to drive from here. Change them and the block below re-renders — it is the same component whose source is above, not a mock of it. Everything else it accepts is in the table underneath.

Props

Read out of the component’s own type and signature, so this cannot drift from the source below. Every prop has a default — the component renders standalone before you pass it anything.

PropTypeDefault
totalItemsnumber2847
pageSizeOptionsnumber[][10, 25, 50, 100]
initialPageSizenumber25
onChange(state: { page: number; pageSize: number }) => void—
classNamestring''

Not using React?

The same block rendered once to markup, wrapped as a file your framework compiles. Tailwind classes are framework-agnostic, so the design transfers intact — the behaviour does not.

This block is interactive. The markup below is its initial state with the event handlers stripped — you will need to re-wire the behaviour in your framework.

data-table-pagination.html
<!--
  Pagination With Page Window — markup from the Hoverlab catalog.

  This is the block rendered once to HTML and wrapped as a component
  file. It is not a port of the React source: the Tailwind classes carry
  the design, which is the part that took the work, and they are the same
  in every framework.

  This block is interactive in React and the handlers are NOT here.
  Buttons, toggles and menus render in their initial state and do
  nothing until you wire them up.
-->
<div class="flex flex-col items-center justify-between gap-4 border-t border-border/60 px-4 py-3 sm:flex-row ">
  <div class="flex items-center gap-2 text-sm">
    <label for="_R_0_-page-size" class="text-muted-foreground">Rows per page</label>
    <select id="_R_0_-page-size" class="rounded-lg border border-border/60 bg-background px-2 py-1 text-sm outline-none focus-visible:ring-2 focus-visible:ring-primary">
      <option value="10">10</option>
      <option value="25" selected="">25</option>
      <option value="50">50</option>
      <option value="100">100</option>
    </select>
  </div>
  <p aria-live="polite" class="text-sm text-muted-foreground">Showing <span class="font-medium text-foreground">1</span>–<span class="font-medium text-foreground">25</span> of <span class="font-medium text-foreground">2,847</span></p>
  <nav aria-label="Pagination" class="flex items-center gap-1">
    <button type="button" disabled="" aria-label="First page" class="rounded-lg p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-40">
      <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevrons-left h-4 w-4 rtl:rotate-180" aria-hidden="true">
        <path d="m11 17-5-5 5-5"></path>
        <path d="m18 17-5-5 5-5"></path>
      </svg>
    </button>
    <button type="button" disabled="" aria-label="Previous page" class="rounded-lg p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-40">
      <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevron-left h-4 w-4 rtl:rotate-180" aria-hidden="true">
        <path d="m15 18-6-6 6-6"></path>
      </svg>
    </button>
    <button type="button" aria-label="Page 1" aria-current="page" class="h-8 min-w-8 rounded-lg px-2 text-sm font-medium transition-colors bg-primary text-primary-foreground">1</button>
    <button type="button" aria-label="Page 2" class="h-8 min-w-8 rounded-lg px-2 text-sm font-medium transition-colors text-muted-foreground hover:bg-muted hover:text-foreground">2</button>
    <button type="button" aria-label="Page 3" class="h-8 min-w-8 rounded-lg px-2 text-sm font-medium transition-colors text-muted-foreground hover:bg-muted hover:text-foreground">3</button>
    <button type="button" aria-label="Page 4" class="h-8 min-w-8 rounded-lg px-2 text-sm font-medium transition-colors text-muted-foreground hover:bg-muted hover:text-foreground">4</button>
    <button type="button" aria-label="Page 5" class="h-8 min-w-8 rounded-lg px-2 text-sm font-medium transition-colors text-muted-foreground hover:bg-muted hover:text-foreground">5</button>
    <span aria-hidden="true" class="px-1.5 text-sm text-muted-foreground">…</span>
    <button type="button" aria-label="Page 114" class="h-8 min-w-8 rounded-lg px-2 text-sm font-medium transition-colors text-muted-foreground hover:bg-muted hover:text-foreground">114</button>
    <button type="button" aria-label="Next page" class="rounded-lg p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-40">
      <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevron-right h-4 w-4 rtl:rotate-180" aria-hidden="true">
        <path d="m9 18 6-6-6-6"></path>
      </svg>
    </button>
    <button type="button" aria-label="Last page" class="rounded-lg p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-40">
      <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevrons-right h-4 w-4 rtl:rotate-180" aria-hidden="true">
        <path d="m6 17 5-5-5-5"></path>
        <path d="m13 17 5-5-5-5"></path>
      </svg>
    </button>
  </nav>
</div>
  • This is rendered HTML, not a translation of the React source. The Tailwind classes carry the design and work in any framework.
  • It is one frame: the component in its initial state, with no props applied beyond the defaults.
  • This block is interactive in React — toggles, menus or form state. None of that survives here; the markup is the closed/default state and the handlers are gone. Re-wire them in your own framework.
  • Requires Tailwind, and the design tokens the classes reference (bg-card, text-muted-foreground, and so on). The template ZIPs ship a globals.css that defines them.

What each framework gets across the whole catalog — effects convert properly; this rung is markup.

For AI

The component, its props, the design tokens it expects and the command that installs it — as one prompt. Paste it into Claude, Cursor, v0 or ChatGPT and what they build around it will match the rest of the catalog instead of inventing its own system.

See the prompt

Used in these pages

Want the whole screen instead of this one section? Open a page and copy it entire.

7 more blocks in Data Tables

All of them free to read, copy and install — no account, no locked tiles, no watermarked preview. The whole catalog is open, and so are the API and the CLI.

Browse Data Tables

Shipping one commercially

Copying the code is free. Putting it in client work or a paid product is what Pro is for — the licence, not the access.

  • A commercial licence for everything in the catalog
  • Unlimited bundle exports, in Vue, Svelte and Tailwind
  • One payment — no subscription, nothing to renew
Pro — $79 once

More Data Tables blocks

View category
Open the full page for this block

Sortable Selectable Table

Type-aware sorting with aria-sort, a header checkbox that goes properly indeterminate, and selection that survives a re-sort.

Data Tables297 lines1 dep
Open the full page for this block

Table Search & Filter Bar

Search, filter count and view switch, with active filters shown as removable chips so a filtered table never looks unfiltered.

Data Tables180 lines1 dep
Open the full page for this block

Expandable Detail Rows

Rows that open into a full-width detail panel via colSpan, with aria-expanded and aria-controls wired to the detail row.

Data Tables205 lines1 dep
Open the full page for this block

Column Show, Hide & Reorder

Reordering that works from the keyboard rather than by drag alone, pinned columns that cannot be hidden or moved, and a guard so the last visible column cannot be unchecked into a blank table.

Data Tables219 lines1 dep
Open the full page for this block

Bulk Action Bar

The selection bar that asks the question most tables skip: this page, or all 1,284 rows the filter matches. Typed confirmation above a threshold, an undo with the window counting down, and a select-all that names its own scope.

Data Tables405 lines1 dep
Open the full page for this block

Inline Editable Table

Edit cells in the grid with staged changes, per-cell validation, the original value kept beside the attempt, and a save that counts what is still pending.

Data Tables301 lines1 dep
Open the full page for this block

Grouped Table with Subtotals

Collapsible group headers that carry their own subtotals while collapsed, with a grand total that sums every row rather than only the visible ones.

Data Tables238 lines1 dep