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"
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],
]);
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]]);
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"],
]);
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");
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],
]);
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).
I'm here to help with Tea page tabs in the URL. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.