Skip to content
Recipes/Pagination bar

Pagination bar

src/articles/pagination-bar.tsx
import { getPageWindow } from '@qubeejs/core';
import type { QubeeListHandle } from '@qubeejs/react';
import type { MouseEvent } from 'react';
import type { articleList } from './article-list';
import { isPlainClick } from './plain-click';
type PaginationBarProps = {
/** The last page the API reported; 1 until the first page arrives. */
lastPage: number;
list: QubeeListHandle<typeof articleList>;
};
/** Previous, the page window with its gaps, and next — every page a real link. */
export function PaginationBar({ lastPage, list }: PaginationBarProps) {
const { page } = list.state;
/** Navigate in place on a plain click; leave any other click to the browser. */
const goTo = (target: number) => (event: MouseEvent<HTMLAnchorElement>) => {
if (!isPlainClick(event)) {
return;
}
event.preventDefault();
list.setPage(target);
};
return (
<nav aria-label="Pagination">
<ul>
<li>
{page > 1 ? (
<a aria-label="Previous page" href={list.href({ page: page - 1 })} onClick={goTo(page - 1)}>
‹
</a>
) : (
<span aria-hidden="true">‹</span>
)}
</li>
{getPageWindow(page, lastPage).map((item, index) => (
<li key={item === 'gap' ? `gap-${index}` : item}>
{item === 'gap' ? (
<span aria-hidden="true">…</span>
) : (
<a
aria-current={item === page ? 'page' : undefined}
aria-label={`Page ${item}`}
href={list.href({ page: item })}
onClick={goTo(item)}
>
{item}
</a>
)}
</li>
))}
<li>
{page < lastPage ? (
<a aria-label="Next page" href={list.href({ page: page + 1 })} onClick={goTo(page + 1)}>
›
</a>
) : (
<span aria-hidden="true">›</span>
)}
</li>
</ul>
</nav>
);
}
src/articles/plain-click.ts
import type { MouseEvent } from 'react';
/**
* Whether a click on a link should navigate in place: the primary button, no
* modifier key. Anything else — a middle click, a ⌘-click — is left to the
* browser, which opens the href in a new tab.
*/
export function isPlainClick(event: MouseEvent<HTMLAnchorElement>): boolean {
return event.button === 0 && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey;
}
src/articles/result-range.tsx
import { getPageRange, type PaginatedResult } from '@qubeejs/core';
import type { Article } from './article-list';
/** "21–40 of 57 articles" — or "21–40 articles" when the API reports no total. */
export function ResultRange({ result }: { result: PaginatedResult<Article> }) {
const { from, to, total } = getPageRange(result);
if (to === 0) {
return <p role="status">No articles match.</p>;
}
return (
<p role="status">
{from === to ? from : `${from}–${to}`}
{total === null ? ' articles' : ` of ${total} articles`}
</p>
);
}
  • getPageWindow returns the same number of items for a given last page, so the bar keeps its width as the reader pages; a 'gap' stands for two or more pages, never one.
  • Every page’s href comes from list.href({ page }): it opens in a new tab, and search engines can follow it. isPlainClick lets a plain click navigate in place with setPage.
  • At either end, previous or next is a <span> hidden from assistive technology: there is nowhere to go.
  • aria-current="page" marks the page being shown.
  • Pass lastPage={result?.lastPage ?? 1} — the last page is only known once a response has reported it.
  • ResultRange reads from, to and total from getPageRange and leaves the wording to you.

With React Router’s <Link> instead of anchors: React Router.