Tabs

PageTabs: several views of one topic on a page, linkable by URL hash and fully readable by crawlers.

PageTabs shows several views of one topic on a page: a row of tabs along the top and one panel at a time underneath. It lives in src/components/site/page-tabs.tsx and is built on the shadcn Tabs (Base UI) in its line variant.

Using it

tsx
import { PageTabs } from "@/components/site/page-tabs";

<PageTabs
  tabs={[
    { id: "overview", label: "Overview", children: <Overview /> },
    { id: "details", label: "Details", children: <Details /> },
  ]}
/>

Each tab is an id, a label and its content. The ids end up in the URL hash, so keep them short, URL-safe and unique on the page.

What it does

  • Every panel is in the HTML. Closed panels are only hidden, so crawlers and find-in-page can read them. This matters for the same reason it does on the accordions.
  • The chosen tab lives in the URL hash. #details opens the Details tab, a reload keeps your place, and a tab can be linked to. The hash is the only source of truth, so Back and Forward, a pasted link and a click on a tab all end up in the same place.
  • Panel content follows the page's rhythm. Each panel is wrapped in the page body, so headings, lists, code blocks and tables are spaced as they would be outside the tabs.
  • The bar scrolls sideways when the tabs don't fit, instead of wrapping or widening the page.
  • Several sets on one page work independently. Give each set different ids.

Styling a set

A page can restyle its own tabs without changing the standard look, with listClassName for the bar and tabClassName for each tab, from the page's CSS module. The About page does this to make its two tabs wide, large and centred under the portrait. The tabs always have a rule under them, so a page that has its own separator above them should turn that one off.

Things to know

  • Headings in a closed tab can't be scrolled to. A contents link to one does nothing until its tab is open, so avoid putting headings you want in the contents inside tabs.
  • A hash that isn't a tab id shows the first tab. That includes a heading anchor inside a panel.
  • Everything mounts at once. Because closed panels stay in the page, anything heavy or stateful in a tab runs on load.
  • Link between tabs with plain anchors (<a href="#details">). A router link to a hash changes the address without the browser's hash change event, and the tabs listen for that event.
  • The tab label isn't a heading. To a crawler the panels are role="tabpanel" blocks labelled by their tab, so the heading outline doesn't say which tab a section belongs to.

When not to use it

  • Different topics belong on separate pages.
  • A list of questions and answers is an accordion (QuestionList), not tabs.

Test page

The component is exercised on the tabs test page: rhythm against the page's own spacing, deep links, a closed-tab crawler check and a row of nine tabs for overflow.

  • Code blocks

    The VS Code-style code block: snippets, multi-file tabs, explorer, wrap, copy.

  • Component library

    Every typography and shadcn component, rendered in the real theme.

  • Layouts with route groups

    The plan to give each kind of page its own layout with Next.js route groups: (site), (notes) with a left notes aside, (bare), and (artifacts) for self-contained HTML files.

  • Highlight

    The Highlight text component: a faint blue tint with square corners, whose side padding repeats on every line it wraps across.