Homelogologo
Sponsor

Table of Contents

A navigation component that dynamically highlights the current section as users scroll through document content, improving usability and visual guidance.

Anatomy

anatomy
<Toc items={items} scrollEl={() => scrollContainer}> <Toc.Content /> <Toc.Nav> <Toc.Title /> <Toc.List> <Toc.Indicator /> <Toc.Item item={item}> <Toc.Link /> </Toc.Item> </Toc.List> </Toc.Nav> </Toc>

Examples

Basic

Define the items prop by associating headings with their respective content sections, and ensure the scrollEl function points to the scrollable container

Nested Headings

To achieve nested headings, leverage your existing HTML markup by manually indenting sub-headings. This approach ensures full control over the visual hierarchy without relying on automatic indentation.

With Collapsible

Wrap the Toc.Nav component inside a Collapsible component to allow users to hide the navigation. Utilize the Toc.Context to access activeItems.

With Indicator

Add the Toc.Indicator component inside the Toc.List to create a visual marker that smoothly slides to the currently active item.

API Reference

Toc

The root component that renders the table of contents.

PropTypeDefault
itemsTocItem[]undefined
scrollEl() => HTMLElementundefined
activeIdsstring[]undefined
autoScrollbooleantrue
defaultActiveIdsstring[]undefined
onActiveChange(details: ActiveChangeDetails) => voidundefined
rootMarginstring-20px 0px -40% 0px
scrollBehaviorScrollBehaviorsmooth
thresoldnumber0

TocContent

The content component that renders the table of contents.

PropTypeDefault
asChildbooleanfalse

TocNav

The navigation component that renders the table of contents.

PropTypeDefault
asChildbooleanfalse
placementleft | rightundefined

TocTitle

The title component that renders the table of contents title.

PropTypeDefault
asChildbooleanfalse

TocList

The list component that renders the table of contents.

PropTypeDefault
asChildbooleanfalse

TocIndicator

The indicator component that highlights the active item in the table of contents.

PropTypeDefault
asChildbooleanfalse

TocItem

The item component that renders a single table of contents item.

PropTypeDefault
asChildbooleanfalse
itemTocItemundefined

The link component that renders a single table of contents item.

PropTypeDefault
asChildbooleanfalse
hrefstringundefined