A navbar made of folder tabs: building it in React

Build a navbar whose links are paper folder tabs standing on the page: the section you're reading steps forward and joins the page, with a layout that moves and recolours only.

5 min read

The real component. Click the picture to open it live and try it before you read how it was built.

Most navbars are a row of grey links over the page. This one stands on the page: every link is a folder tab, and the tab for the section you're reading is in front, joined to the white page below it.

One tab steps forward

The front tab is the page's own white and has no line under it, so tab and page read as one sheet. The others sit a shade darker and a few pixels lower, behind it. Changing the front tab only moves and recolours, so it reads as one tab stepping forward and another stepping back.

src/registry/navbars/folder-tabs/folder-tab.tsx

import { cn } from "@/lib/utils";
import type { NavItem } from "@/registry/navbars/shared/types";

interface FolderTabProps {
  item: NavItem;
  isFront: boolean;
  // Tabs further back sit lower in the stack, so the front one always overlaps its neighbours.
  depth: number;
}

// A paper folder tab standing on the page's top edge. The front tab is the page's own white and joins it with no line
// between; the others sit a shade darker, a few pixels lower, behind it. Changing the front tab only moves and
// recolours, so it reads as one tab stepping forward and another stepping back.
export function FolderTab({ item, isFront, depth }: FolderTabProps) {
  return (
    <a
      href={item.href}
      aria-current={isFront ? "true" : undefined}
      style={{ zIndex: isFront ? 20 : 10 - depth }}
      className={cn(
        "relative -ml-1 flex h-10 shrink-0 items-center rounded-t-lg border border-b-0 px-5 text-sm outline-none first:ml-0",
        "transition duration-200 ease-out focus-visible:text-zinc-950",
        isFront
          ? "translate-y-0 border-zinc-300 bg-white text-zinc-950"
          : "translate-y-1 border-zinc-300 bg-zinc-200 text-zinc-500 hover:translate-y-0.5 hover:bg-zinc-100 hover:text-zinc-800",
      )}
    >
      {item.label}
    </a>
  );
}
One tab

Which section are you reading?

The active tab follows the section in view, so the navbar doubles as a quiet progress marker. On phones the tabs move to their own row and scroll sideways rather than squeezing.

src/registry/navbars/folder-tabs/navbar-folder-tabs.tsx

"use client";

import { useEffect, useRef, type ReactNode } from "react";
import { NavCtaLink } from "@/registry/navbars/shared/nav-cta-link";
import { useActiveLabel } from "@/registry/navbars/shared/use-active-label";
import type { NavCta, NavItem } from "@/registry/navbars/shared/types";
import { cn } from "@/lib/utils";
import { FolderTab } from "./folder-tab";

export interface NavbarFolderTabsProps {
  logo: ReactNode;
  // One tab per section, in page order. The first one is in front until another section is on screen.
  items: NavItem[];
  cta?: NavCta;
  login?: NavCta;
  className?: string;
}

// The links as folder tabs standing on the page's top edge: the tab for the section you're reading is in front and
// joins the white page, the rest wait behind it. On phones the tabs move to their own row and scroll sideways.
export function NavbarFolderTabs({ logo, items, cta, login, className }: NavbarFolderTabsProps) {
  const front = useActiveLabel(items) ?? items[0]?.label;
  const tabsRef = useRef<HTMLElement>(null);

  // On phones the row scrolls sideways: keep the front tab in the middle of it.
  useEffect(() => {
    const row = tabsRef.current;
    const tab = row?.querySelector<HTMLElement>('[aria-current="true"]');
    if (!row || !tab || row.scrollWidth <= row.clientWidth) return;
    row.scrollTo({ left: tab.offsetLeft - (row.clientWidth - tab.offsetWidth) / 2, behavior: "smooth" });
  }, [front]);

  return (
    <header className={cn("fixed inset-x-0 top-0 z-50 bg-zinc-100 text-zinc-950", className)}>
      <div className="mx-auto flex max-w-6xl flex-wrap items-end gap-x-8 px-5 md:flex-nowrap">
        <a href="#top" aria-label="Home" className="flex h-14 items-center outline-none md:h-16">
          {logo}
        </a>
        <div className="ml-auto flex h-14 items-center gap-5 md:order-last md:h-16">
          {login && (
            <a
              href={login.href}
              className="hidden text-sm text-zinc-600 transition-colors outline-none hover:text-zinc-950 focus-visible:text-zinc-950 md:block"
            >
              {login.label}
            </a>
          )}
          {cta && <NavCtaLink cta={cta} variant="solid" size="md" />}
        </div>
        <nav
          ref={tabsRef}
          aria-label="Main"
          className="relative -mx-5 flex w-full items-end overflow-x-auto overflow-y-hidden px-5 pt-2 md:mx-0 md:w-auto md:px-0"
        >
          {items.map((item, index) => (
            <FolderTab key={item.label} item={item} isFront={item.label === front} depth={index} />
          ))}
        </nav>
      </div>
      {/* The page's top edge: the tabs behind it sit under the line, the front tab covers it. */}
      <span aria-hidden className="absolute inset-x-0 bottom-0 z-15 h-px bg-zinc-300" />
    </header>
  );
}
The navbar

Why it works

It is still a plain list of links with a visible focus ring. The character comes from structure, not an effect, so it is recognisable in a still screenshot and never gets in the way.

More articles