Skip to the code

Nav links that show where you are

Leaf and Kettle's header, in the root layout, links to the shop's sections and shows the visitor where they are. Build its navigation: app/components/site-nav.tsx, whose default export is SiteNav. Render a <nav aria-label="Main"> with a next/link Link for each section, in this order: Home to /, Teas to /teas, Teaware to /teaware, and Journal to /journal.

A Server Component can't read the current URL, so SiteNav is a Client Component that gets the path from usePathname(), and the layout renders it. Mark each link by where the visitor is:

On the link's own page, give it aria-current="page" and the class active.

On a page inside its section, give it aria-current="true" and the class active: Teas on /teas/green. Only whole segments count, so /teaspoons isn't inside /teas, and Home is only marked on / itself.

Anywhere else, leave out aria-current and the active class.

/teas/green
→ <nav aria-label="Main">
    <a href="/">Home</a>
    <a href="/teas" aria-current="true" class="active">Teas</a>
    <a href="/teaware">Teaware</a>
    <a href="/journal">Journal</a>
  </nav>

What the tests check

  1. marks the link to the page you're on
    const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
    renderClient(<SiteNav />, { url: "/teas" });
    expect(screen.getByRole("navigation", { name: "Main" })).toBeInTheDocument();
    expect(screen.getAllByRole("link").map((item) => item.getAttribute("href"))).toEqual(["/", "/teas", "/teaware", "/journal"]);
    expect(marks()).toEqual([
      ["Home", null, false],
      ["Teas", "page", true],
      ["Teaware", null, false],
      ["Journal", null, false],
    ]);
    
  2. marks the section you're inside on the pages under it
    const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
    renderClient(<SiteNav />, { url: "/teas/green" });
    expect(marks()).toEqual([
      ["Home", null, false],
      ["Teas", "true", true],
      ["Teaware", null, false],
      ["Journal", null, false],
    ]);
    cleanup();
    renderClient(<SiteNav />, { url: "/journal/first-flush/notes" });
    expect(marks()[3]).toEqual(["Journal", "true", true]);
    
  3. only whole segments count, and Home is only marked on /
    const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
    renderClient(<SiteNav />, { url: "/teaspoons" });
    expect(marks().filter(([, current, active]) => current !== null || active)).toEqual([]);
    cleanup();
    renderClient(<SiteNav />, { url: "/" });
    expect(marks()).toEqual([
      ["Home", "page", true],
      ["Teas", null, false],
      ["Teaware", null, false],
      ["Journal", null, false],
    ]);
    
  4. ignores the search params
    const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
    renderClient(<SiteNav />, { url: "/teaware?sort=price" });
    expect(marks()[2]).toEqual(["Teaware", "page", true]);
    
  5. moves its marks as the visitor navigates
    const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
    renderClient(<SiteNav />, { url: "/" });
    await user.click(screen.getByRole("link", { name: "Journal" }));
    expect(router.url).toBe("/journal");
    expect(marks()).toEqual([
      ["Home", null, false],
      ["Teas", null, false],
      ["Teaware", null, false],
      ["Journal", "page", true],
    ]);
    
  6. is a Client Component, so the root layout can show it
    const marks = () => screen.getAllByRole("link").map((item) => [item.textContent, item.getAttribute("aria-current"), item.classList.contains("active")]);
    function RootLayout({ children }) {
      return (
        <div>
          <header>
            <SiteNav />
          </header>
          <main>{children}</main>
        </div>
      );
    }
    await renderServer(<RootLayout><h1>Kettles</h1></RootLayout>, { url: "/teaware/kettles" });
    expect(screen.getByRole("heading", { name: "Kettles" })).toBeInTheDocument();
    expect(marks()[2]).toEqual(["Teaware", "true", true]);
    

Learn more

app/components/site-nav.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).