Overview
A TOC lists the sections of a long page and links to each one. It has a head with a label and the page title, and a lined Drawer that holds the links. The script builds the links from the page’s headings, or reads a list you write.
Pick another component when:
Markup
Auto-built List
The TOC sits in a sticky Side Info column next to the article it lists. Preview opens it in a panel, where the article can scroll.
Manual List
You write the links. Nest a <ul> inside an <li> for each level.
Annual Report 2025
Behavior
Auto-built List
data-toc-source on .nds-toc names the container to read, as a CSS selector. At init, the script replaces the list with a link to each heading in it, nested by level. data-toc-levels picks the levels, such as h2 for a flat list. The script builds the list once. After the headings change, call NDS.Toc.destroy(toc), then NDS.Toc.create(toc). A heading with no id gets one made from its text, Arabic included, so its link works. Until the list fills in, it keeps the height of --toc-skeleton-rows links, so the content under it does not move. A TOC that finds no headings at load stays empty, and NDS.init.refresh() builds it once the headings are in the page.
Manual List
Without data-toc-source, the script reads the links you write. Pick it for some sections only, for anchors that are not headings, or for a shorter label when a heading is too long for the column. Each link’s href must match an id on the page, or the link is left out of the tracking.
Built-in Features
-
Auto-initialization
Every
.nds-tocon the page starts by itself, before the page shows. -
Heading Ids
A heading with no
idgets one from its text. Two headings with the same text get two ids, such assetupandsetup-2, so each link goes to its own heading. -
Active-Section Tracking
As the reader scrolls, the link to the last section that passed under the main navigation shows as active. No link is active until the first section reaches the top. When the article scrolls in a panel or a modal, the TOC tracks and scrolls that box instead of the page.
-
Click-to-Scroll
A click scrolls the heading to just below the main navigation and updates the URL hash, with no new history entry. The scroll is instant when the reader asks for reduced motion.
-
Nested Levels
Each nested list is indented, with a line along it, so the reader sees each section's level at a glance.
-
JavaScript API
Start or stop a TOC from code, and check the active link again after the layout changes.
Best Practices
- Use a TOC on long pages: policies, guides and reports. On a short page that fits on the screen, leave it out.
- Use the auto-built list when the links match the headings. It lists every heading at load, so a new section needs no TOC edit.
- Give every heading its own short
id. An id made from the text changes when the text changes, and breaks the links people shared. - Keep the list to three levels or fewer. A deeper list is hard to scan, and often means the page should be split. Set
data-toc-levels="h2,h3"when the page usesh4for small labels, not sections. - Set
--toc-skeleton-rowson.nds-tocto the number of links the page has. On a phone, the TOC sits above the article, so a wrong count moves the article when the list fills in. - Put the TOC in a sticky
.nds-sideinfo, so it stays in view. The column is 300px wide for a TOC:--nds-sideinfo-widthchanges it. - Give the
<nav>anaria-label, so screen reader users can tell it from the page’s other navigation. - Let the script mark the active link. It sets
data-state="active"from the scroll position, and clears any the markup carries at init.
API
Classes
| Class | Element | Effect |
|---|---|---|
nds-toc |
<nav> |
The TOC. Holds the head and the drawer |
nds-toc-head |
<div> |
The head: a label and the title. Optional |
nds-toc-title |
the heading in the head | The page title, in larger semibold type |
nds-lined |
.nds-drawer |
Required. The line along each nested list |
nds-loading |
.nds-toc |
The link labels show as pulsing bars. Use it while the page loads the content |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-toc-source |
.nds-toc |
A CSS selector for the container whose headings build the list. It replaces the list you write |
data-toc-levels |
.nds-toc |
The heading levels to list, comma-separated. Default h2,h3,h4 |
data-state="loading" |
.nds-toc |
The same as nds-loading, for a script that already sets states |
data-state="active" |
<li> and its link |
Set by the script on the link to the current section. It also writes aria-current="true" on that link, and removes it when the section changes |
CSS Custom Properties
| Property | Default | Controls |
|---|---|---|
--toc-skeleton-rows |
6 |
Links an auto-built list reserves height for before it fills in, at 36px each. Set it on .nds-toc |
--drawer-lined-width |
2px |
Width of the line along nested lists. The TOC sets it on .nds-toc |
--drawer-lined-block |
0px |
Space cut from the top and the bottom of that line. The TOC sets it on .nds-toc |
--drawer-indicator-width |
3px |
Width of the active link’s line. The TOC sets it on its .nds-drawer, so set a new value there |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_toc.scss.
| Token | Preview | Value |
|---|---|---|
--text-displayRead by color, --btn-color | var(--colors-neutral-800)Dark mode var(--colors-neutral-100) | |
--text-primary-paragraphRead by color | var(--colors-neutral-700)Dark mode var(--colors-neutral-300) | |
--button-indicator-markRead by --btn-indicator-color, --btn-indicator-selected | var(--background-primary)Dark mode var(--colors-primary-500) |
JavaScript
| Method | Effect |
|---|---|
NDS.Toc.init() |
Starts every TOC on the page that has not started yet |
NDS.Toc.create(toc) |
Starts one TOC and returns its instance. Returns the running one when toc already has one, and null when no link points at an element on the page |
NDS.Toc.destroy(toc) |
Removes the listeners and the active mark of the link. NDS.init.destroy() calls it for every TOC in the container it releases |
instance.refresh() |
Picks the active link again from the scroll position. Call it after the layout above the headings changes |
The TOC fires no events.
The full API is in the banner of _js/nds-toc.js.