Table of Contents

A navigation component that tracks and highlights the active section as users scroll through document content.

Loading...

Anatomy

<Toc.Root 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.Root>

Examples

Basic

Pass headings to items, and point scrollEl at the scrollable container so the TOC knows what to track.

Nested Headings

Read depth in your own markup to indent sub-headings. Nothing is indented for you.

Root Provider

Use useToc with Toc.RootProvider to read activeIds from outside the tree.

With Collapsible

Wrap Toc.Nav in a Collapsible to let users hide it. Here activeItems from Toc.Context drives a progress ring.

With Hover

Expand the navigation on onMouseEnter and collapse it on onMouseLeave, with a pin toggle to keep it open.

Note: Touch screens have no hover. Keep a pin button or disclosure control so the navigation stays reachable on mobile.

With Indicator

Add Toc.Indicator inside Toc.List for a marker that slides to the active item.

With Rail

Give each item a small SVG offset by depth. Where neighbouring items sit at different depths, a bezier joins the two positions so the rail steps rather than breaks.

With Tree View

Pair Toc.Root with TreeView for hierarchical navigation. onActiveChange expands the branch holding the active heading.

Guides

Items

Every entry needs value, the id of the heading element, and depth, the heading level.

const items = [
  { value: 'introduction', depth: 2 },
  { value: 'installation', depth: 2 },
  { value: 'peer-dependencies', depth: 3 },
]

value must match the heading's id exactly. The component resolves it with getElementById to track visibility, and Toc.Link targets it with href="#introduction". An item whose id is missing renders but never activates.

Ids are global to the page, so prefix them when a page holds more than one TOC.

Extra properties, such as a label for link text, are fine. TocItemData covers only value and depth, so extend it instead of using it directly:

import type { TocItemData } from '@ark-ui/react/toc'

interface Item extends TocItemData {
  label: string
}

API Reference

Props

Root

Defines the <div> element.

AttributeDefaultType
items
TocItem[]

The TOC items with `value` (slug/id) and `depth` (heading level).

activeIds
string[]

The controlled active heading ids.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
autoScrolltrue
boolean

Whether to auto-scroll the TOC container so the first active item is visible when active headings change.

defaultActiveIds
string[]

The default active heading ids when rendered. Use when you don't need to control the active headings.

id
string

The unique identifier of the machine.

ids
ElementIds

The ids of the elements in the TOC. Useful for composition.

multiplefalse
boolean

Whether every visible heading can be active at once. When `false`, only the topmost visible heading is active.

onActiveChange
(details: ActiveChangeDetails) => void

Callback when the active (visible) headings change.

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

rootMargin'-20px 0px -40% 0px'
string

The root margin for the IntersectionObserver. Controls the effective viewport area for determining active headings.

scrollBehavior'smooth'
ScrollBehavior

The default scroll behavior used when auto-scrolling the TOC container and when scrolling to a heading (via link click or `api.scrollTo`). Can be overridden per-call by passing `behavior` to `api.scrollTo`.

scrollEl
() => HTMLElement | null

Function that returns the scroll container element to observe within. Defaults to the document/viewport.

threshold0
number | number[]

The IntersectionObserver threshold. A value of `0` means the heading is active as soon as even one pixel is visible within the root margin area.

CSS VariableDescription
--topThe top position value
--leftThe left position value
--widthThe width of the element
--heightThe height of the element

Content

Defines the <article> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Indicator

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Item

Defines the <li> element.

AttributeDefaultType
item
TocItem

The TOC item

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-toc-item]<uid>
[data-value]The value of the item
[data-depth]The depth of the item
[data-active]Present when active or pressed
[data-first]
[data-last]
CSS VariableDescription
--depthThe depth value for the Item

Link

Defines the <a> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-toc-link]<uid>
[data-value]The value of the item
[data-active]Present when active or pressed

List

Defines the <ul> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Nav

Defines the <nav> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
placement
'left' | 'right'

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

RootProvider

Defines the <div> element.

AttributeDefaultType
value
UseTocReturn

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Title

Defines the <h2> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Context

API

PropertyType
activeIds
string[]

All currently active (visible) heading ids

activeItems
TocItem[]

The active (visible) TOC items

items
TocItem[]

The resolved items list

setActiveIds
(value: string[]) => void

Manually set the active heading ids

scrollTo
(value: string, details?: ScrollToDetails | undefined) => void

Scrolls to the heading with the given id.

getItemState
(props: ItemProps) => ItemState

Returns the state of a TOC item

getIndicatorState
() => IndicatorState

Returns the state of the indicator