Skip to the code

Page links with gaps that keep the filters

Leaf and Kettle's tea list can run to dozens of pages, so its page links show only the pages around the current one. Build them: app/teas/pagination.tsx, whose default export is Pagination({ pageCount }). It's a Client Component that reads the current page and the rest of the URL with useSearchParams() and usePathname(), so any list page can show it, passing only how many pages there are.

The current page is the URL's page when that's a whole number from 1 up, and 1 otherwise. A page past pageCount means the last page. With one page or none, render nothing.

Render a <nav aria-label="Pagination"> holding a <ul>, with each item in an <li>: a Previous link (not on the first page), the page links, and a Next link (not on the last page). The page links are for the first page, the last page, the current page, and the pages right before and after it. Where pages are left out, put an <li> with …, but when a gap would hide just one page, show a link to that page instead. Each page link's text is its number, and the current page's link has aria-current="page".

Make every link with <Link href={{ pathname, query }}>. Keep every other param in the URL in its order, repeated ones like type=green&type=black included, then put page last, leaving it out for page 1, so /teas?page=1 is never linked.

/teas?type=green&type=black&page=6&sort=price, with pageCount 12
→ Previous 1 … 5 6 7 … 12 Next
  7 → /teas?type=green&type=black&sort=price&page=7
  1 → /teas?type=green&type=black&sort=price

What the tests check

  1. links every page when there are only a few
    const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
    const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
    renderClient(<Pagination pageCount={5} />, { url: "/teas?page=3" });
    expect(items()).toEqual(["Previous", "1", "2", "3", "4", "5", "Next"]);
    expect(screen.getByRole("link", { name: "3" })).toHaveAttribute("aria-current", "page");
    expect(screen.getAllByRole("link").filter((link) => link.hasAttribute("aria-current")).length).toBe(1);
    
  2. shows the first, the last, and the pages around the current one, with gaps
    const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
    const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
    renderClient(<Pagination pageCount={12} />, { url: "/teas?page=6" });
    expect(items()).toEqual(["Previous", "1", "…", "5", "6", "7", "…", "12", "Next"]);
    cleanup();
    renderClient(<Pagination pageCount={12} />, { url: "/teas" });
    expect(items()).toEqual(["1", "2", "…", "12", "Next"]);
    cleanup();
    renderClient(<Pagination pageCount={12} />, { url: "/teas?page=12" });
    expect(items()).toEqual(["Previous", "1", "…", "11", "12"]);
    
  3. shows the page instead of a gap that would hide only that page
    const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
    const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
    renderClient(<Pagination pageCount={12} />, { url: "/teas?page=4" });
    expect(items()).toEqual(["Previous", "1", "2", "3", "4", "5", "…", "12", "Next"]);
    cleanup();
    renderClient(<Pagination pageCount={12} />, { url: "/teas?page=9" });
    expect(items()).toEqual(["Previous", "1", "…", "8", "9", "10", "11", "12", "Next"]);
    
  4. keeps every other param, repeated ones too, with page last and left out for page 1
    const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
    const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
    renderClient(<Pagination pageCount={12} />, { url: "/teas?type=green&type=black&page=6&sort=price" });
    expect(href("7")).toBe("/teas?type=green&type=black&sort=price&page=7");
    expect(href("Next")).toBe("/teas?type=green&type=black&sort=price&page=7");
    expect(href("Previous")).toBe("/teas?type=green&type=black&sort=price&page=5");
    expect(href("12")).toBe("/teas?type=green&type=black&sort=price&page=12");
    expect(href("1")).toBe("/teas?type=green&type=black&sort=price");
    cleanup();
    renderClient(<Pagination pageCount={3} />, { url: "/teas?page=2" });
    expect(href("1")).toBe("/teas");
    
  5. reads a bad page as 1 and one past the end as the last, and hides a single page
    const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
    const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
    for (const page of ["abc", "0", "-2", "2.5", ""]) {
      renderClient(<Pagination pageCount={3} />, { url: "/teas?page=" + page });
      expect(items()).toEqual(["1", "2", "3", "Next"]);
      expect(screen.getByRole("link", { name: "1" })).toHaveAttribute("aria-current", "page");
      cleanup();
    }
    renderClient(<Pagination pageCount={3} />, { url: "/teas?page=40" });
    expect(items()).toEqual(["Previous", "1", "2", "3"]);
    expect(screen.getByRole("link", { name: "3" })).toHaveAttribute("aria-current", "page");
    cleanup();
    for (const pageCount of [1, 0]) {
      renderClient(<Pagination pageCount={pageCount} />, { url: "/teas?page=2" });
      expect(screen.queryByRole("navigation")).toBe(null);
      cleanup();
    }
    
  6. a click moves to that page with a new history entry
    const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
    const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
    renderClient(<Pagination pageCount={12} />, { url: "/teas?sort=name" });
    await user.click(screen.getByRole("link", { name: "2" }));
    expect(router.history).toEqual(["/teas?sort=name", "/teas?sort=name&page=2"]);
    expect(items()).toEqual(["Previous", "1", "2", "3", "…", "12", "Next"]);
    expect(screen.getByRole("link", { name: "2" })).toHaveAttribute("aria-current", "page");
    await user.click(screen.getByRole("link", { name: "Previous" }));
    expect(router.url).toBe("/teas?sort=name");
    

Learn more

app/teas/pagination.tsxSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).