Todae

Todae

paginationRange

paginationRange({ page, count, siblings, boundaries }) returns the pages to show for page out of count: page numbers, with "start-ellipsis" and "end-ellipsis" for the gaps. It shows the current page, siblings pages on each side of it and boundaries pages at each end; both default to 1. Pagination renders the list.

On this page
import { Code, Field, Label, paginationRange, Stack } from '@tounsoo/todae';
import { useId, useState } from 'react';

export default function PaginationRangeItems({ locale }: { locale: string }) {
  const pageId = useId();
  const countId = useId();
  const [count, setCount] = useState<number | null>(20);
  const [page, setPage] = useState<number | null>(10);
  const total = count ?? 1;
  const current = Math.min(page ?? 1, total);
  return (
    <Stack.Root gap="condensed">
      <Stack.Root direction="row" style={{ flexWrap: 'wrap' }}>
        <Field.Group>
          <Label.Root htmlFor={pageId}>Page</Label.Root>
          <Field.Root id={pageId}>
            <Field.NumberInput value={page} onValueChange={setPage} min={1} max={total} step={1} locale={locale} />
          </Field.Root>
        </Field.Group>
        <Field.Group>
          <Label.Root htmlFor={countId}>Pages</Label.Root>
          <Field.Root id={countId}>
            <Field.NumberInput value={count} onValueChange={setCount} min={1} max={500} step={1} locale={locale} />
          </Field.Root>
        </Field.Group>
      </Stack.Root>
      <Code.Root>{JSON.stringify(paginationRange({ page: current, count: total }))}</Code.Root>
    </Stack.Root>
  );
}
[1,"start-ellipsis",9,10,11,"end-ellipsis",20]

A steady length

While there are more pages than slots, the list keeps one length, so the number of controls doesn't change as the page does and nothing jumps under the pointer. With the defaults, that is seven items: page 1 of 20 gives 1, 2, 3, 4, 5, end-ellipsis, 20 and page 10 gives 1, start-ellipsis, 9, 10, 11, end-ellipsis, 20. A gap always stands for two or more pages, so where an ellipsis would hide one page, that page is shown instead. A count below 1 gives an empty list, and a page outside 1 to count is moved to the nearest end.

Accessible composition

Todae handles: Nothing on screen: it returns the list. Pagination gives it the landmark, the current page and the ellipses.

You supply: A link or button for each page number, with aria-current="page" on the current one, as Pagination shows.

Watch for: Keying items by their index: key a page by its number and an ellipsis by its name, since both ellipses can show at once.

When to use

When: The page numbers for Pagination, or for paging controls of your own, such as under a Table.

When not: A list people scroll through without pages → load more as they reach the end.

Props

paginationRange props
NameTypeDescription
page Requirednumber

The current page, from 1. Clamped to 1..count.

count Requirednumber

How many pages there are. Below 1, or not a finite number, gives an empty list.

siblingsnumber

Pages shown on each side of the current one.

Default: 1

boundariesnumber

Pages always shown at each end.

Default: 1