shadcn.io is an independent project built on shadcn/ui
On this page9 sections
React Aria does not include this primitive, so this page uses the Ark UI component. The visuals match the Ark UI version.
A table of contents lists the headings of a long page beside it, marks the section on screen as people scroll, and jumps to a heading when one is picked. Put it next to documentation, a long article or a policy page.
For a hierarchy people open and close, with no tie to the scroll position, use a Tree View. Every version runs on Ark UI's toc machine and imports nothing from React Aria, so installing it adds @ark-ui/react to your project.
Installation
Anatomy

Usage
import {
Toc, TocIndicator, TocItem, TocLink, TocList, TocNav, TocTitle,
} from "@/components/ui/toc";Each item's value is the id of a heading already on the page, and depth is its level. label is yours, for the link text. With no scrollEl, the headings are watched as the page itself scrolls.
const items = [
{ depth: 2, label: "Introduction", value: "introduction" },
{ depth: 2, label: "Installation", value: "installation" },
{ depth: 3, label: "Requirements", value: "requirements" },
{ depth: 2, label: "Usage", value: "usage" },
];
export const PageOutline = () => (
<Toc items={items}>
<TocNav>
<TocTitle>On this page</TocTitle>
<TocList>
<TocIndicator />
{items.map((item) => (
<TocItem item={item} key={item.value}>
<TocLink href={`#${item.value}`}>{item.label}</TocLink>
</TocItem>
))}
</TocList>
</TocNav>
</Toc>
);Accessibility
TocNav is a nav landmark named by TocTitle, which renders an h2, and the active link carries aria-current="location", so a screen reader announces it as the current location. The links are ordinary anchors in the tab order, with no arrow keys of their own. Keep a TocTitle in every TocNav, since the landmark takes its name from it. A link scrolls to its heading but leaves focus on the link, so the next Tab continues through the outline, not into the section.
- Tab
- Moves focus to the next link in the outline, then on through the page.
- Shift+Tab
- Moves focus to the previous link.
- Enter
- On a link, scrolls straight to its heading, with no animation. A
scrollElcontainer other thanTocContentscrolls the wayscrollBehaviorsays, smoothly by default. Focus stays on the link.
Demos
Nested
Eight headings on two levels: Integrations has Free Blocks and Configuration under it, and API Reference has Hooks and Components. Each item's depth is 2 or 3, TocItem indents the third level, and the article renders those headings as h3, so the outline and the page agree. Use it when a flat list would hide which subsection belongs to which section.
Root provider
useToc creates the state in the component above the outline, and TocRootProvider hands it to the article and the list. A line above the article prints the active ids as you scroll. Use it when something outside the outline, a reading progress bar or a page header, follows the same heading. Otherwise the Toc root holds the state itself.
With collapsible
The outline folds into a button above the article that names the current heading and shows how far through the five sections you are, in a small Circular Progress. Opening it shows the numbered links. Use it above the article on a narrow screen, and keep a sidebar outline where the width is there.
With hover
The outline sits at the article's edge as five short ticks, one per heading from Real-time Analytics to Custom PDF Reports, and widens to show the labels while the pointer is over it. The links exist only while the outline is open, so a keyboard can't reach them: open it on focus as well before you use the pattern.
With indicator
Five status sections, two of them far longer than the rest, with TocIndicator inside TocList. The bar slides along the list's edge to the active link and stretches over two links when both of their headings are on screen. Use it when the list is long enough that a bolder link alone is easy to miss.
With rail
Nine headings on three levels, each drawn on a thin rail that steps inward at every level, with a curve where the depth changes. The rail is an SVG inside each TocLink, and the active link's segment turns the primary color, so there's no TocIndicator. Use it for docs with third and fourth level headings, where indentation alone leaves a ragged edge.
With select
The outline as a Native Select above the article, labelled On this page. Its value follows the active heading as you scroll, and picking a section calls scrollTo from the Table of Contents context, which scrolls there smoothly. Use it where a sidebar won't fit and the outline should take as little room as possible.
With tree view
Three sections, Guides, Core Concepts and Advanced, each with two or three subsections, shown in a Tree View. A branch opens by itself when one of its headings becomes active, and clicking a leaf scrolls the article to it. Use it for long docs where people should be able to fold away the parts they aren't reading.
FAQ
How do I build a table of contents from the headings on a page?
Pass items to Toc, one per heading: value is the heading's id and depth its level, and any other field, such as a label for the link text, is yours. Render a TocItem and a TocLink for each, with the link's href set to # and the id.
Every heading needs an id that matches its item. A heading the Table of Contents can't find by that id is never marked active, and its link doesn't scroll. Build the list from the same source as the headings, such as your Markdown or MDX, so the two can't drift apart.
How do I use the Table of Contents in a scrolling container?
Give TocContent a height, such as h-80, put the headings inside it, and pass scrollEl to Toc as a function that returns that element, as the top demo does with a ref. The headings are then watched inside the container, and the links scroll it instead of the page.
For an outline that follows the page's own scroll, leave out scrollEl and keep the article outside TocContent. The links, the scroll-to-top button and the first and last heading's highlight all treat TocContent as the scroller whenever it is there. A container of your own, such as a dialog body, works as scrollEl too, and its links scroll it the way scrollBehavior says, but the scroll-to-top button and that highlight follow only TocContent or the page.
How does the Table of Contents decide which heading is active?
An IntersectionObserver watches the headings, and a heading is active while it sits in a band from 20px below the top of the scroller to 40% above its bottom. Several can be active at once. Near the very top, the first item is marked, and within 48px of the end, the last one is, so a short final section still lights up.
Change the band with rootMargin and threshold on Toc. Active items carry data-active, and onActiveChange receives activeIds and activeItems whenever the set changes.
How do I show nested headings in the Table of Contents?
Give each item its depth, 2 for an h2 and 3 for an h3, and TocItem indents levels 3, 4 and 5 by a step each, as the Nested demo does. Render the headings in the article at the same levels, so the outline matches what a screen reader lists.
Each TocItem also carries data-depth and a --depth CSS variable for a style of your own. The With rail demo goes further and draws a stepped line for each link from its item's depth.
How do I add a moving indicator to the Table of Contents?
Put a TocIndicator inside TocList, before the items. It's a 2px bar at the list's start edge that Ark UI places from --top and --height, measured from the first to the last active item, so it slides as the reader scrolls and stretches when more than one heading is active.
The With indicator demo shows it on sections of very different lengths. The slide takes 150 milliseconds and is turned off for people who ask for reduced motion.
Why does a sticky header cover the heading after I click a Table of Contents link?
The link scrolls the heading to the very top of the page, where a sticky header sits over it. Give the headings a scroll margin as tall as the header, such as scroll-mt-20, and the jump stops that far short.
The links read the heading's own scroll margin when they scroll, and so does scrollTo from the Table of Contents context. Every demo on this page gives its headings scroll-mt-4 for a little space above them.
How do I show the Table of Contents on mobile?
Keep the sidebar outline on wide screens and swap it for something that fits above the article on narrow ones: a select whose value follows the active heading, as in With select, or a collapsible button that names the current section, as in With collapsible.
Both read the same state through the Table of Contents context, so the active heading is right whichever one shows. Hide the other with a responsive class.
Should I use a Table of Contents or a Tree View for page navigation?
Use a Table of Contents when the list mirrors the headings of the page people are reading. It follows the scroll, marks where they are and jumps within the page.
A Tree View is for a hierarchy people open and close on purpose, such as files or the sections of a whole docs site, with no tie to the scroll. The two combine when a long page has deep sections, as the With tree view demo does.
API Reference
Toc
<div> data-slot="toc"
itemsTocItem[]
The TOC items with value (slug/id) and depth (heading level).
activeIds?string[]
The controlled active heading ids.
autoScroll?boolean
Whether to auto-scroll the TOC container so the first active item is visible when active headings change.
- Default
true
defaultActiveIds?string[]
The default active heading ids when rendered. Use when you don't need to control the active headings.
dir?"ltr" | "rtl"
The document's text/writing direction.
- Type
"ltr" | "rtl"- Default
"ltr"
id?string
The unique identifier of the machine.
ids?object
The ids of the elements in the TOC. Useful for composition.
- Type
{ root?: string; title?: string; list?: string; item?: (value: string) → string; link?: (value: string) → string; indicator?: string }
onActiveChange?(details: ActiveChangeDetails) → void
Callback when the active (visible) headings change.
- Type
(details: ActiveChangeDetails) → void- Parameters
details– { activeIds: string[]; activeItems: TocItem[] }- Returns
void
rootMargin?string
The root margin for the IntersectionObserver. Controls the effective viewport area for determining active headings.
- Default
"-20px 0px -40% 0px"
scrollBehavior?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.
- Default
"smooth"
scrollEl?() → HTMLElement | null
Function that returns the scroll container element to observe within. Defaults to the document/viewport.
- Type
() → HTMLElement | null- Returns
HTMLElement | null
threshold?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.
- Type
number | number[]- Default
0
asChild?boolean
Use the provided child element as the default rendered element, combining their props and behavior.
className?string
Merged with the part's classes.
--heightstring
The height of the element.
--leftstring
The left position value.
--topstring
The top position value.
--widthstring
The width of the element.
TocTitle
<h2> data-slot="toc-title"
asChild?boolean
Use the provided child element as the default rendered element, combining their props and behavior.
className?string
Merged with the part's classes.
TocList
<ul> data-slot="toc-list"
asChild?boolean
Use the provided child element as the default rendered element, combining their props and behavior.
className?string
Merged with the part's classes.
TocItem
<li> data-slot="toc-item"
itemobject
The TOC item.
- Type
{ value: string; depth: number }
asChild?boolean
Use the provided child element as the default rendered element, combining their props and behavior.
className?string
Merged with the part's classes.
data-active?string
Present when active or pressed.
data-depthstring
The depth of the item.
data-first?string
Present when first is true.
data-last?string
Present when last is true.
data-valuestring
The value of the item.
--depthstring
The depth value for the Item.
TocLink
<a> data-slot="toc-link"
asChild?boolean
Use the provided child element as the default rendered element, combining their props and behavior.
className?string
Merged with the part's classes.
data-active?string
Present when active or pressed.
data-valuestring
The value of the item.
TocIndicator
<div> data-slot="toc-indicator"
asChild?boolean
Use the provided child element as the default rendered element, combining their props and behavior.
className?string
Merged with the part's classes.
--heightstring
The measured height, for animating open and closed.
--topstringTocContent
<article> data-slot="toc-content"
asChild?boolean
Use the provided child element as the default rendered element, combining their props and behavior.
className?string
Merged with the part's classes.
TocNav
<nav> data-slot="toc-nav"
placement?"left" | "right"
The side of the content the outline sits on.
- Type
"left" | "right"
scrollToTop?boolean
Shows a button under the outline that scrolls back to the top.
- Default
false
asChild?boolean
Use the provided child element as the default rendered element, combining their props and behavior.
className?string
Merged with the part's classes.
TocScrollToTop
<button> data-slot="toc-scroll-to-top"
offset?number
How far the page scrolls before the button shows, in px.
- Default
180
className?string
Merged with the part's classes.
TocRootProvider
<div> data-slot="toc"
valueUseTocReturn
What useToc() returns: the state and API, made outside the component to control it.
asChild?boolean
Use the provided child element as the default rendered element, combining their props and behavior.
className?string
Merged with the part's classes.
--heightstring
The measured height, for animating open and closed.
--leftstring--topstring--widthstring
The measured width, for animating open and closed.
useToc
itemsTocItem[]
The TOC items with value (slug/id) and depth (heading level).
activeIds?string[]
The controlled active heading ids.
autoScroll?boolean
Whether to auto-scroll the TOC container so the first active item is visible when active headings change.
- Default
true
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?object
The ids of the elements in the TOC. Useful for composition.
- Type
{ root?: string; title?: string; list?: string; item?: (value: string) → string; link?: (value: string) → string; indicator?: string }
onActiveChange?(details: ActiveChangeDetails) → void
Callback when the active (visible) headings change.
- Type
(details: ActiveChangeDetails) → void- Parameters
details– { activeIds: string[]; activeItems: TocItem[] }- Returns
void
rootMargin?string
The root margin for the IntersectionObserver. Controls the effective viewport area for determining active headings.
- Default
"-20px 0px -40% 0px"
scrollBehavior?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.
- Default
"smooth"
scrollEl?() → HTMLElement | null
Function that returns the scroll container element to observe within. Defaults to the document/viewport.
- Type
() → HTMLElement | null- Returns
HTMLElement | null
threshold?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.
- Type
number | number[]- Default
0
TocContext
activeIdsstring[]
All currently active (visible) heading ids.
activeItemsTocItem[]
The active (visible) TOC items.
getItemState(props: ItemProps) → ItemState
Returns the state of a TOC item.
- Parameters
props– { item: TocItem }- Returns
ItemState
itemsTocItem[]
The resolved items list.
scrollTo(value: string, details?: ScrollToDetails) → void
Scrolls to the heading with the given id.
- Type
(value: string, details?: ScrollToDetails) → void- Parameters
value– stringdetails?– { behavior?: ScrollBehavior }- Returns
void
setActiveIds(value: string[]) → void
Manually set the active heading ids.
- Parameters
value– string[]- Returns
void












