fix(presentation): window pagination links and stop nesting its landmark (Q-6.8-12)

Pagination links only the first and last page, the current page and its
neighbours, with an aria-hidden ellipsis for a longer gap and the page
itself for a one-page gap, so 5000 pages render five links instead of
5000. Inside CollectionPage, whose slot already is the pagination `nav`,
Pagination renders a labelled group instead of a nested `nav`; on its own
it stays a navigation landmark.

Verified with tests/component/pagination.test.tsx (RED: 3 of 5 failed
before the change) and the lane gates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 13:44:38 +09:00
co-authored by Claude Opus 5.5
parent 21233e8d9b
commit 22d4e8a29d
3 changed files with 141 additions and 21 deletions
@@ -1,4 +1,4 @@
import { useId, useRef, useState } from "react";
import { createContext, useContext, useId, useRef, useState } from "react";
import {
Link,
useInRouterContext,
@@ -160,15 +160,28 @@ type PaginationProps = Readonly<{
onChange(page: number): void;
}>;
/**
* Set by a container that already is the pagination navigation landmark
* (CollectionPage), so Pagination renders a labelled group instead of a
* nested `nav`.
*/
export const PaginationLandmarkContext = createContext(false);
export function Pagination(props: PaginationProps) {
const { label, page, pageCount, previousLabel, nextLabel, onChange } = props;
const inRouter = useInRouterContext();
const nested = useContext(PaginationLandmarkContext);
const Container = nested ? "div" : "nav";
const { direction } = useLocale();
const PreviousDirectionalIcon =
direction === "rtl" ? NextIcon : PreviousIcon;
const NextDirectionalIcon = direction === "rtl" ? PreviousIcon : NextIcon;
return (
<nav aria-label={label} className="ui-pagination">
<Container
aria-label={label}
className="ui-pagination"
role={nested ? "group" : undefined}
>
<IconButton
accessibleName={previousLabel}
disabled={page <= 1}
@@ -195,10 +208,29 @@ export function Pagination(props: PaginationProps) {
>
<NextDirectionalIcon />
</IconButton>
</nav>
</Container>
);
}
/**
* The first and last page, the current page and its neighbours. A gap of one
* page shows that page instead of an ellipsis.
*/
function visiblePages(page: number, pageCount: number): Array<number | "gap"> {
const wanted = [...new Set([1, page - 1, page, page + 1, pageCount])]
.filter((candidate) => candidate >= 1 && candidate <= pageCount)
.sort((left, right) => left - right);
const visible: Array<number | "gap"> = [];
let previous = 0;
for (const candidate of wanted) {
if (candidate - previous === 2) visible.push(candidate - 1);
else if (candidate - previous > 2) visible.push("gap");
visible.push(candidate);
previous = candidate;
}
return visible;
}
/** Keeps every other search parameter and only replaces `page`. */
function withPage(search: string, page: number): string {
const params = new URLSearchParams(search);
@@ -223,21 +255,26 @@ function PageLinks({
onChange,
target,
}: PaginationProps & Readonly<{ target(page: number): LinkTarget }>) {
const pages = Array.from({ length: pageCount }, (_, index) => index + 1);
return pages.map((candidate) => (
<LinkButton
{...target(candidate)}
aria-current={candidate === page ? "page" : undefined}
key={candidate}
onClick={(event) => {
// The owner decides how a page change is applied; the href is for
// open-in-new-tab and copy-link.
event.preventDefault();
onChange(candidate);
}}
variant={candidate === page ? "primary" : "ghost"}
>
{pageLabel(candidate)}
</LinkButton>
));
return visiblePages(page, pageCount).map((candidate, index) =>
candidate === "gap" ? (
<span aria-hidden="true" key={`gap-${index}`}>
…
</span>
) : (
<LinkButton
{...target(candidate)}
aria-current={candidate === page ? "page" : undefined}
key={candidate}
onClick={(event) => {
// The owner decides how a page change is applied; the href is for
// open-in-new-tab and copy-link.
event.preventDefault();
onChange(candidate);
}}
variant={candidate === page ? "primary" : "ghost"}
>
{pageLabel(candidate)}
</LinkButton>
),
);
}
@@ -6,6 +6,7 @@ import {
LinkButton,
type LinkTarget,
} from "../design-system/primitives/core.tsx";
import { PaginationLandmarkContext } from "../design-system/primitives/navigation.tsx";
import { useLocale } from "../i18n/index.ts";
export type PageHeading = Readonly<{
@@ -134,7 +135,9 @@ export function CollectionPage(
className="collection-page__pagination"
aria-label={message("template.pagination")}
>
{props.pagination}
<PaginationLandmarkContext.Provider value>
{props.pagination}
</PaginationLandmarkContext.Provider>
</nav>
) : null}
</StandardPage>