|Kit

TableOfContents

Polymorphic

An on-page table of contents for long subjects and docs pages, highlighting the section being read.

TableOfContents lists the headings of a page, indented by level. A thin track runs beside the entries and bends in and out with the nesting. As the page scrolls, the stretch of track beside the sections in view lights up, ending in a dot, and their labels brighten. Clicking an entry scrolls to its heading, below any sticky header, and puts the heading's #id in the URL.

import { TableOfContents } from "@42-central/ui-react/table-of-contents";

<TableOfContents container="#subject" offset={64} />

It is built on useScrollSpy, and each entry is an Anchor.

Playground

Scroll the subject on the left.

TableOfContents

Loading preview

Props

Code

<TableOfContents variant="track" size="sm" />

Usage

Point container at the element that holds the headings, as a ref or a CSS selector. Without it, the whole document is read. selector picks which headings count, h2, h3 by default, so add h4 for one more level:

const article = useRef<HTMLElement>(null);

<article ref={article}>…</article>
<TableOfContents container={article} selector="h2, h3, h4" />

The headings are read once, after mount. Each one needs an id, since that is what the link points at; rehype-slug adds them to Markdown and MDX. A heading without one is skipped, with a warning in development. The component renders nothing until it has found a heading.

If the headings scroll inside an element rather than the window, pass that element as root. Every demo on this page works that way, so that it fits its card.

const root = useRef<HTMLDivElement>(null);

<div ref={root} className="h-96 overflow-auto">
  <article ref={article}>…</article>
</div>
<TableOfContents container={article} root={root} />

Variants

  • track, the default, draws a hairline that follows the nesting: one level deeper, it bends inward with an S-curve and runs beside the nested entries, then bends back out. The sections in view are the same line drawn in the accent, from the first to the last, ending in a dot. It can span several entries at once.
  • light slides a tinted pill behind the entry being read.
  • filled slides a solid pill, for a table of contents that should stand out.

color paints the lit range and its dot, or the pill. Without it, they take the text colour.

Introduction

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Common instructions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Norm

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Makefile

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Mandatory part

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Libc functions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Additional functions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Bonus part

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Submission and peer-evaluation

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

track
light
filled

Code

<TableOfContents container={article} root={root} size="xs" withTitle={false} />
<TableOfContents container={article} root={root} size="xs" withTitle={false} variant="light" color="brand" />
<TableOfContents container={article} root={root} size="xs" withTitle={false} variant="filled" color="brand" />

The lit range and the pill slide between entries. When the reader prefers reduced motion, they jump instead, and a click scrolls the page instantly instead of smoothly.

Rich labels

A label can be any content: inline Code, or text long enough to wrap onto a second line. The track is measured from the links themselves, and measured again whenever one changes size, so it stays beside the text, wrapped or not.

Tokens

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Loading the fonts

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Next.js — next/font/google

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Vite, Remix, plain React — Fontsource

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

CDN — Google Fonts <link>

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Self-hosted — @font-face

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Why two families

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Code

<TableOfContents
  data={[
    { value: "tokens", label: "Tokens", depth: 2 },
    { value: "loading", label: "Loading the fonts", depth: 2 },
    { value: "next", label: <>Next.js — <Code>next/font/google</Code></>, depth: 3 },
    { value: "vite", label: "Vite, Remix, plain React — Fontsource", depth: 3 },
    { value: "cdn", label: <>CDN — Google Fonts <Code>{"<link>"}</Code></>, depth: 3 },
    { value: "self-hosted", label: <>Self-hosted — <Code>@font-face</Code></>, depth: 3 },
    { value: "families", label: "Why two families", depth: 2 },
  ]}
/>

Title

The ready-made component shows an "On this page" title with an icon above the list, and the title names the navigation landmark. translations.label changes the text, icons.title the icon (null for none), and withTitle={false} drops the title. The nav is then named by aria-label instead.

Size

size, from xs to xl, sets the text size, the padding, and how far each level is indented. The default is sm.

Put the table of contents in a sticky aside next to the content, and set offset to the height of anything stuck to the top of the page. A heading counts as being read once it reaches that line, and a click scrolls it to just below it.

The aside sticks inside its nearest scroller. In a page whose window scrolls, that's the page itself; a wrapper with overflow: auto or hidden between the aside and the window would catch it instead.

libft

Introduction

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Common instructions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Norm

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Makefile

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Mandatory part

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Libc functions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Additional functions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Bonus part

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Submission and peer-evaluation

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Code

<header className="sticky top-0 h-12">libft</header>
<div className="grid grid-cols-[1fr_11rem] gap-6">
  <article ref={article}>…</article>
  <aside className="sticky top-16 self-start">
    <TableOfContents container={article} offset={48 + 16} color="brand" />
  </aside>
</div>

On a real page the window scrolls, so there's no root. Give the aside a max-height and overflow-y: auto, so that a long table of contents scrolls on its own instead of running off the screen:

<aside className="sticky top-16 max-h-[calc(100dvh-4rem)] self-start overflow-y-auto">
  <TableOfContents container="#subject" offset={64} />
</aside>

Entries from data

With data, the entries are yours: their labels, their order and their levels. They render on the server too, with no flash on load. Each entry's value is the heading's id, and depth its level. Without depth, the level is read from the heading after mount.

Introduction

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Common instructions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Norm

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Makefile

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Mandatory part

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Libc functions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Additional functions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Bonus part

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Submission and peer-evaluation

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Code

<TableOfContents
  variant="light"
  color="blue"
  data={[
    { value: "introduction", label: "1. Introduction", depth: 2 },
    { value: "mandatory-part", label: "2. Mandatory part", depth: 2 },
    { value: "libc-functions", label: "2.1 Libc", depth: 3 },
    { value: "additional-functions", label: "2.2 Additional", depth: 3 },
    { value: "bonus-part", label: "3. Bonus", depth: 2 },
  ]}
/>

Compound API

For markup of your own, call useScrollSpy and pass what it returns to TableOfContents.Root as spy. Item takes the heading's id as value, and its children are the label.

Introduction

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Common instructions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Norm

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Makefile

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Mandatory part

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Libc functions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Additional functions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Bonus part

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Submission and peer-evaluation

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Code

const spy = useScrollSpy({ container: article, selector: "h2" });

<TableOfContents.Root spy={spy} variant="filled" color="green">
  <TableOfContents.Title />
  <TableOfContents.Indicator />
  <TableOfContents.List>
    {spy.data.map((heading, i) => (
      <TableOfContents.Item key={heading.id} value={heading.id}>
        <span className="opacity-60">{i + 1}.</span> {heading.label}
      </TableOfContents.Item>
    ))}
  </TableOfContents.List>
</TableOfContents.Root>

Entries render through Anchor, so they go through your router's Link when the app has a LinkComponentProvider. Item passes asChild and every other prop on to its Anchor.

Right to left

The track sits on the inline-start edge, and entries indent from it. In a right-to-left page both move to the right, and the curves mirror. Nothing needs setting: the table of contents follows the dir of the page.

Anatomy

<nav data-scope="table-of-contents" data-part="root" aria-labelledby=":r1:" data-variant="track" data-testid="toc">
  <p data-part="title" id=":r1:"><span aria-hidden="true"><svg /></span>On this page</p>
  <div data-part="indicator" data-variant="track" aria-hidden="true">
    <svg>{/* the quiet track */}</svg>
    <svg>{/* the same path in the accent, clipped to the sections in view */}</svg>
    <span />{/* the dot */}
  </div>
  <ul data-part="list">
    <li data-part="item" data-depth="0" data-active data-visible>
      <a data-part="link" href="#introduction" aria-current="location" data-active data-visible>
        Introduction
      </a>
    </li>
    <li data-part="item" data-depth="1" data-visible>
      <a data-part="link" href="#norm" data-visible>Norm</a>
    </li>
  </ul>
</nav>

data-depth is the entry's level below the shallowest heading, which sits flush. data-visible marks every section in view, the range the track lights; data-active and aria-current mark only the one being read. With light and filled, the indicator is a single pill <div>. On the compound API, testId renders as data-testid on each part: the <nav>, the title, the <ul>, the indicator and, for Item, its link. With the data prop, give each entry its own testId. See Testing.

API

TableOfContents

Prop

Type

TableOfContents.Root

The <nav>. Takes variant, color, size, icons and translations as above, plus:

Prop

Type

TableOfContents.Item

An <li> around an Anchor. Every prop other than value and depth goes to the Anchor.

Prop

Type

TableOfContents.Title (a <p> with the icon and translations.label, or your children), TableOfContents.List (the <ul>) and TableOfContents.Indicator take their element's props and a testId. A mounted Title names the nav through aria-labelledby. Indicator positions itself against the root, so render it directly inside Root, and it doesn't take asChild.

Accessibility

  • The table of contents is a <nav> landmark named "On this page" (translations.label): by its visible title, or by aria-label without one.
  • Only the entry being read has aria-current="location", so screen readers announce one current location, even when the track lights several sections. The track, the dot and the pill are decorative (aria-hidden).
  • Every entry is a real link to #id: it's focusable, it follows on Enter, it opens in a new tab with a modifier key, and the hash it puts in the URL can be shared.
  • Under prefers-reduced-motion, the lit range and the pill jump instead of sliding, and a click scrolls instantly.
  • A click moves the scroll, not the focus. After a keyboard click, the next Tab still starts from the entry.

Changelog

v0.30.0
  • New TableOfContents: an on-page TOC whose track lights up the sections in view

    #485
  • New useScrollSpy hook tracks the heading being read and every section in view

    #485

On this page