Skip to the code

Tea page tabs in the URL

Each tea's page at Leaf and Kettle, /teas/<slug>, has three tabs: Description, Brewing, and Reviews. The open tab lives in the URL, /teas/green?tab=brewing, so a link can open a tea on its reviews, and the page, a Server Component, reads searchParams to show the right panel. Build the tab bar it shows: app/teas/[slug]/tea-tabs.tsx, whose default export is TeaTabs.

It's a Client Component that reads the URL with usePathname() and useSearchParams(). Render a <nav aria-label="Tea details"> with a next/link Link for each tab: Description, Brewing, and Reviews. The open tab is the one tab names, brewing or reviews; anything else, or no tab at all, means Description. Give the open tab's link aria-current="page".

Each link goes to the same page with tab set to its tab, keeping the URL's other params, like the size the visitor picked. Description is the default, so its link leaves tab out.

Switching tabs shouldn't add to the history, so Back leaves the tea's page instead of stepping through tabs, and shouldn't scroll the page: give every link replace and scroll={false}.

/teas/green?size=250g&tab=reviews
→ Description  /teas/green?size=250g
  Brewing      /teas/green?size=250g&tab=brewing
  Reviews      /teas/green?size=250g&tab=reviews   aria-current="page"

What the tests check

  1. links each tab to the tea's page with its tab
    const tabs = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("href"), item.getAttribute("aria-current")]);
    renderClient(<TeaTabs />, { url: "/teas/green" });
    expect(screen.getByRole("navigation", { name: "Tea details" })).toBeInTheDocument();
    expect(tabs()).toEqual([
      ["Description", "/teas/green", "page"],
      ["Brewing", "/teas/green?tab=brewing", null],
      ["Reviews", "/teas/green?tab=reviews", null],
    ]);
    
  2. marks the open tab, and Description for a tab it doesn't know
    const tabs = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("href"), item.getAttribute("aria-current")]);
    const marked = () => tabs().map(([name, , current]) => [name, current]);
    renderClient(<TeaTabs />, { url: "/teas/green?tab=reviews" });
    expect(marked()).toEqual([["Description", null], ["Brewing", null], ["Reviews", "page"]]);
    cleanup();
    renderClient(<TeaTabs />, { url: "/teas/green?tab=price" });
    expect(marked()).toEqual([["Description", "page"], ["Brewing", null], ["Reviews", null]]);
    
  3. keeps the URL's other params
    const tabs = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("href"), item.getAttribute("aria-current")]);
    renderClient(<TeaTabs />, { url: "/teas/green?size=250g&tab=reviews" });
    expect(tabs()).toEqual([
      ["Description", "/teas/green?size=250g", null],
      ["Brewing", "/teas/green?size=250g&tab=brewing", null],
      ["Reviews", "/teas/green?size=250g&tab=reviews", "page"],
    ]);
    
  4. switches tabs without a new history entry or a scroll
    renderClient(<TeaTabs />, { url: "/teas/green" });
    await user.click(screen.getByRole("link", { name: "Brewing" }));
    expect(router.calls).toEqual([{ method: "replace", href: "/teas/green?tab=brewing", options: { scroll: false } }]);
    expect(router.history).toEqual(["/teas/green?tab=brewing"]);
    expect(screen.getByRole("link", { name: "Brewing" })).toHaveAttribute("aria-current", "page");
    expect(screen.getByRole("link", { name: "Description" })).not.toHaveAttribute("aria-current");
    
  5. works on every tea's page
    const tabs = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("href"), item.getAttribute("aria-current")]);
    renderClient(<TeaTabs />, { url: "/teas/oolong?tab=brewing" });
    expect(tabs()).toEqual([
      ["Description", "/teas/oolong", null],
      ["Brewing", "/teas/oolong?tab=brewing", "page"],
      ["Reviews", "/teas/oolong?tab=reviews", null],
    ]);
    

Learn more

app/teas/[slug]/tea-tabs.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).