Table of Contents

A table of contents for React built with the Toc components. It highlights the current section, marks the sections on the screen, and shows nested headings.

Examples

Dot Markers

Overview

These examples build tables of contents with the Ariakit UI Toc components. A table of contents lists the headings of a page and shows where the reader is. The link of the section being read is current, and a bar on the guide covers the rows whose sections are on the screen. The links stay plain links, so the browser scrolls to the heading, updates the URL, and moves the focus starting point.

Each example is a full page that uses the Shell components. The gallery starts with a documentation page, with the table of contents in a sidebar beside the article. Then it shows a long list that scrolls by itself, a phone layout with a popover, numbers and dots on the rows, other guide styles, and marked parent links. In these examples, the sidebar stays at every width, and the article text gets smaller in a narrow frame, so the table of contents stays in view. On a real phone, keep the text readable and put the table of contents in a popover, as the Current Section Popover example does.

Components

Toc renders a navigation landmark with a list of TocLink rows. A TocLink renders its own list item. For nested rows, put the link and a TocList in a TocItem. The link in a TocItem renders only the link, and the links in the nested TocList render their own list items again. So a recursive component can map a tree of headings. The label prop shows a title above the rows and names the landmark with it. $size sets the size of the label and the rows. It defaults to sm. Set $itemPadding on the Toc to change the padding of all rows, such as $itemPadding={3}, and the label lines up with them. It defaults to 1.5.

One guide runs along the rows and bends into each nested list. The indent and the bends stop at the third level, and deeper rows line up with the third level. Set $guide="diagonal" for slanted bends, $guide="rail" for one straight line, or $guide={false} for the indent only. Without a guide, there is no bar, and only the current link shows. The bar is 1px wide by default. Set $barWidth to change it, such as $barWidth={2} for 2px or $barWidth="0.125rem".

The current link gets a lighter surface and full ink, and keeps the normal weight, so long labels don’t wrap differently while the page scrolls. Set $selectedBold on the links to also show it in bold. $parentInk and $parentBold mark the links whose nested rows hold the current link.

Rows have no markers by default. Set ordered on the Toc to number the top-level rows with markers on the guide, or set ordered on a nested TocList to number only its rows. The rows keep their distance from the guide, so a number overlaps the start of the surface of the current row. A thin outline in the color of the surface around the rows separates the number from that surface. The guide and the bar stop one gap before each marker, so the surface around the rows shows between them. Without a guide, the numbers sit as near to the rows as on a guide. A number fills with the bar color where the bar passes. Set $markerHighlight="ring" on the Toc to light up a ring around the number instead, which takes the width of the bar, or $markerHighlight={false} to leave the number as it is. Set $marker="bullet" on a nested TocList to draw a dot at each of its rows instead. A dot shows that the bar covers it in the same way as a number, and the list stays unordered. The numbers and the dots are the markers of a List, so a number has the size and the surface of the counter in an ordered list. A dot at rest keeps the color of the guide.

The current link is the last heading above the activation line: where a click on its link puts the heading. CSS sets this line, with the scroll-padding-top of the scroll container and the scroll-margin-top of the heading. The Shell components set the scroll margin of the headings below the sticky headers, so a heading becomes current where it lands. Near the end of the page, the line slides down to the bottom, so short final sections become current in turn. So a click on a heading near the end can make a later one current. Before the first heading reaches the line, no link is current. Headings side by side, such as the titles of cards in a grid row, reach the line together. The first one stays current, unless the URL hash points to another one, such as after a click on its link.

The table of contents watches the nearest scroll container around its first heading, or the page. When the list is taller than its own scroll box, as in the Auto-Scrolling List example, the box scrolls by itself to keep the rows of the sections on screen in view, and the page keeps its place. When those rows don’t fit, the current link stays in view. At the first link, the box also shows the label above the list. The box keeps its scroll-padding around the rows, so a sticky label above them needs a matching scroll-padding-top. onCurrentChange receives the href of the current link, or null. The Current Section Popover example shows the current section in its button with it.

Accessibility

Give each Toc a name with label, aria-label, or aria-labelledby, such as “On this page”, because a page can have more than one navigation landmark. Inside a Nav landmark or a ShellSidebar with landmark="nav", a Toc without a name renders a div. When that landmark renders outside the React tree, such as an Astro sidebar around an island, set landmark={false}.

The current link has aria-current="location". The guide, the bar, the numbers, and the dots are hidden from assistive technology, and an ordered Toc or TocList gives the order with an ordered list. The bar over the sections on the screen is visual only, and changes of the current link aren’t announced, so they don’t interrupt reading. Links keep the native focus behavior: the Toc moves no focus and adds no tabindex to the headings.