From 22d4e8a29da0032eee696a953f3ecda8910d9a97 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 13:25:59 +0900 Subject: [PATCH] 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 --- .../design-system/primitives/navigation.tsx | 77 +++++++++++++----- src/presentation/templates/page-templates.tsx | 5 +- tests/component/pagination.test.tsx | 80 +++++++++++++++++++ 3 files changed, 141 insertions(+), 21 deletions(-) create mode 100644 tests/component/pagination.test.tsx diff --git a/src/presentation/design-system/primitives/navigation.tsx b/src/presentation/design-system/primitives/navigation.tsx index 5fde71f9..d19b6074 100644 --- a/src/presentation/design-system/primitives/navigation.tsx +++ b/src/presentation/design-system/primitives/navigation.tsx @@ -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 ( - + ); } +/** + * 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 { + 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 = []; + 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) => ( - { - // 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)} - - )); + return visiblePages(page, pageCount).map((candidate, index) => + candidate === "gap" ? ( + + ) : ( + { + // 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)} + + ), + ); } diff --git a/src/presentation/templates/page-templates.tsx b/src/presentation/templates/page-templates.tsx index f9908e9e..de2890a9 100644 --- a/src/presentation/templates/page-templates.tsx +++ b/src/presentation/templates/page-templates.tsx @@ -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} + + {props.pagination} + ) : null} diff --git a/tests/component/pagination.test.tsx b/tests/component/pagination.test.tsx new file mode 100644 index 00000000..a6bdc5b0 --- /dev/null +++ b/tests/component/pagination.test.tsx @@ -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 ( + {}} + 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( + + Results + , + ); + + const slot = screen.getByRole("navigation", { name: "페이지 탐색" }); + expect(within(slot).queryAllByRole("navigation")).toHaveLength(0); + expect(within(slot).getByRole("group", { name: "Pages" })).toBeVisible(); + }); +});