Data
Pagination
Previous, next and the pages around the current one, as links.
import { Pagination, PaginationContent, PaginationItem, PaginationLink } from 'omuk-ui/pagination';Data
Previous, next and the pages around the current one, as links.
import { Pagination, PaginationContent, PaginationItem, PaginationLink } from 'omuk-ui/pagination';Change the props on the right. The pagination and its code follow.
import {
Pagination,
PaginationContent,
PaginationEllipsis,
PaginationItem,
PaginationLink,
PaginationNext,
PaginationPrevious,
} from 'omuk-ui/pagination';
import { type MouseEvent, useState } from 'react';
const LAST_PAGE = 12;
/** The first page, the current one and its neighbours, and the last. */
function pagesAround(current: number, last: number, siblings: number) {
const pages = [1, last];
for (let page = current - siblings; page <= current + siblings; page++) {
pages.push(page);
}
const shown = pages.filter((page) => page >= 1 && page <= last);
return [...new Set(shown)].sort((a, b) => a - b);
}
const [current, setCurrent] = useState(5);
function go(page: number) {
return (event: MouseEvent) => {
event.preventDefault();
if (page >= 1 && page <= LAST_PAGE) setCurrent(page);
};
}
<Pagination>
<PaginationContent>
<PaginationItem>
<PaginationPrevious
href={`?page=${current - 1}`}
disabled={current === 1}
onClick={go(current - 1)}
/>
</PaginationItem>
{pagesAround(current, LAST_PAGE, 1).map((number, index, shown) => (
<PaginationItem key={number} className="flex gap-1">
{index > 0 && number - (shown[index - 1] ?? 0) > 1 && (
<PaginationEllipsis />
)}
<PaginationLink
href={`?page=${number}`}
isActive={number === current}
onClick={go(number)}
>
{number}
</PaginationLink>
</PaginationItem>
))}
<PaginationItem>
<PaginationNext
href={`?page=${current + 1}`}
disabled={current === LAST_PAGE}
onClick={go(current + 1)}
/>
</PaginationItem>
</PaginationContent>
</Pagination>Previous, the first page, the current page and its neighbours, the last page, and next. The current page has the outline and says so to a screen reader; a gap shows as an ellipsis.
Under a table, say which rows show on the left and put the arrows on the right. The arrows turn off at the ends.
21–40 of 134 projects
Pagination is the <nav>; PaginationContent the list; PaginationItem one entry in it. The links are anchors: give each one an href, or a router link through its props.