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:
co-authored by
Claude Opus 5.5
parent
21233e8d9b
commit
22d4e8a29d
@@ -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>
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { render, screen, within } from "@testing-library/react";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { Pagination } from "../../src/presentation/design-system/index.ts";
|
||||
import { CollectionPage } from "../../src/presentation/templates/index.ts";
|
||||
|
||||
function pagination(page: number, pageCount: number) {
|
||||
return (
|
||||
<Pagination
|
||||
label="Pages"
|
||||
nextLabel="Next"
|
||||
onChange={() => {}}
|
||||
page={page}
|
||||
pageCount={pageCount}
|
||||
pageLabel={(candidate) => `Page ${candidate}`}
|
||||
previousLabel="Previous"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function pageLinks(): string[] {
|
||||
return screen
|
||||
.getAllByRole("link")
|
||||
.map((link) => link.textContent ?? "");
|
||||
}
|
||||
|
||||
describe("Pagination windowing", () => {
|
||||
it("links the first, last and neighbouring pages only", () => {
|
||||
render(pagination(50, 5000));
|
||||
|
||||
expect(pageLinks()).toEqual([
|
||||
"Page 1",
|
||||
"Page 49",
|
||||
"Page 50",
|
||||
"Page 51",
|
||||
"Page 5000",
|
||||
]);
|
||||
expect(screen.getByRole("link", { name: "Page 50" })).toHaveAttribute(
|
||||
"aria-current",
|
||||
"page",
|
||||
);
|
||||
});
|
||||
|
||||
it("links a single skipped page instead of eliding it", () => {
|
||||
render(pagination(3, 5000));
|
||||
expect(pageLinks()).toEqual([
|
||||
"Page 1",
|
||||
"Page 2",
|
||||
"Page 3",
|
||||
"Page 4",
|
||||
"Page 5000",
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps every page of a short range", () => {
|
||||
render(pagination(1, 3));
|
||||
expect(pageLinks()).toEqual(["Page 1", "Page 2", "Page 3"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Pagination landmarks", () => {
|
||||
it("is a navigation landmark on its own", () => {
|
||||
render(pagination(1, 2));
|
||||
expect(screen.getByRole("navigation", { name: "Pages" })).toBeVisible();
|
||||
});
|
||||
|
||||
it("does not nest a navigation landmark inside the collection page slot", () => {
|
||||
render(
|
||||
<CollectionPage heading={{ title: "Collection" }} pagination={pagination(1, 2)}>
|
||||
Results
|
||||
</CollectionPage>,
|
||||
);
|
||||
|
||||
const slot = screen.getByRole("navigation", { name: "페이지 탐색" });
|
||||
expect(within(slot).queryAllByRole("navigation")).toHaveLength(0);
|
||||
expect(within(slot).getByRole("group", { name: "Pages" })).toBeVisible();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user