Table of Contents Node
A comprehensive table of contents (TOC) component system for Tiptap editors. This package provides both an inline TOC node that can be embedded within the editor content and a floating sidebar component for document navigation with progress indicators.
Installation
Add the component via the Tiptap CLI:
npx @tiptap/cli@latest add toc-nodeUsage
Register TableOfContents to collect headings and TocNode to insert a table of contents in the document. Wrap the editor in TocProvider before calling useToc. In the complete example, the extension sends each update to the provider so the sidebar and node view share the same headings.
Components
TocNode extension
The extension registers a draggable block that lists headings from the provider. The node view updates when those headings change.
Usage
import { TocNode } from '@/components/tiptap-node/toc-node'
export const tocNode = TocNode.configure({
topOffset: 80,
maxShowCount: 20,
showTitle: true,
HTMLAttributes: { class: 'document-toc' },
})Configuration options
| Option | Type | Default | Description |
|---|---|---|---|
topOffset | number | 0 | Offset from the top of the viewport when scrolling to a heading (in pixels) |
maxShowCount | number | 20 | Maximum number of headings to display in the TOC |
showTitle | boolean | true | Whether to show the "Table of contents" title |
HTMLAttributes | Record<string, any> | {} | HTML attributes to add to the TOC node element |
Commands
Call editor.commands.insertTocNode() to insert a table of contents at the current selection. Pass maxShowCount, topOffset, or showTitle to override the defaults on that node.
<TocSidebar />
Render TocSidebar inside the same TocProvider as the editor. It displays a heading list and a progress rail. On navigation, it updates the URL hash so the reader can link to that heading.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
maxShowCount | number | 20 | Maximum number of headings to show in the TOC |
topOffset | number | 0 | Offset from the top of the editor container (in pixels) |
className | string | - | Additional CSS class names |
| All standard HTML div props | - | - | Supports all HTMLAttributes<HTMLDivElement> |
Active heading
The sidebar highlights the last heading the reader clicked. When scrolling clears that choice, it uses the last heading the extension marks as active. It falls back to the first heading.
<TocShowTitleButton />
This button toggles the title of the selected table of contents. Set hideWhenUnavailable to hide it when the action is unavailable.
Usage
'use client'
import { TocShowTitleButton } from '@/components/tiptap-node/toc-node'
function MyToolbar() {
return (
<div className="toolbar">
<TocShowTitleButton
text="Show Title"
hideWhenUnavailable={true}
onToggle={() => console.log('Toggled the title')}
/>
</div>
)
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
editor | Editor | null | - | The TipTap editor instance |
text | string | - | Optional text to display alongside the icon |
hideWhenUnavailable | boolean | false | Hide the button when no TOC node is selected |
onToggle | () => void | - | Callback fired when the toggle state changes |
| All standard button props | - | - | Supports all ButtonProps |
Custom title button
For custom button implementations, use the useTocShowTitle hook:
'use client'
import { useTocShowTitle } from '@/components/tiptap-node/toc-node'
function CustomShowTitleButton() {
const { isVisible, isActive, canToggle, handleToggle, label, Icon } = useTocShowTitle({
hideWhenUnavailable: true,
onToggle: () => console.log('Toggled the title'),
})
if (!isVisible) return null
return (
<button onClick={handleToggle} disabled={!canToggle} aria-label={label} data-active={isActive}>
<Icon /> {label}
</button>
)
}The hook returns:
isVisible: boolean- Whether the button should be shownisActive: boolean- Whether the title is currently visiblecanToggle: boolean- Whether the toggle action can be performedhandleToggle: () => boolean- Execute the toggle actionlabel: string- UI label for the buttonIcon: ComponentType- Icon component for the button
Context and hooks
TocProvider supplies the table of contents to its descendants. Call useToc() inside a descendant to access the following values.
| Name | Use |
|---|---|
tocContent | Current heading entries, or null before the first update. |
setTocContent | Pass the entries from the extension's onUpdate callback to the provider. |
navigateToHeading | Pass a heading entry to scroll to it and update the editor selection and URL hash. The optional second argument accepts topOffset and behavior. |
normalizeHeadingDepths | Convert heading levels into indentation depths. Levels two, four, and three have depths one, two, and two. |
Complete example
StarterKit supplies the headings. The editor updates both the inline node and the sidebar through the same provider.
'use client'
import { useEditor, EditorContent, EditorContext } from '@tiptap/react'
import { StarterKit } from '@tiptap/starter-kit'
import { TableOfContents } from '@tiptap/extension-table-of-contents'
import { TocProvider, useToc } from '@/components/tiptap-node/toc-node/context/toc-context'
import { TocNode, TocSidebar, TocShowTitleButton } from '@/components/tiptap-node/toc-node'
import '@/components/tiptap-node/toc-node/toc-node.scss'
import '@/components/tiptap-node/toc-node/ui/toc-sidebar/toc-sidebar.scss'
function EditorComponent() {
const { setTocContent } = useToc()
const editor = useEditor({
immediatelyRender: false,
extensions: [
StarterKit,
TableOfContents.configure({
onUpdate: (content) => {
setTocContent(content)
},
}),
TocNode.configure({
topOffset: 80,
maxShowCount: 20,
showTitle: true,
}),
],
content: `
<h1 id="introduction">Introduction</h1>
<p>Welcome to this comprehensive document about table of contents...</p>
<h2 id="getting-started">Getting Started</h2>
<p>Let's begin by installing the required packages...</p>
<h3 id="installation">Installation</h3>
<p>Run the following command to install...</p>
<h3 id="configuration">Configuration</h3>
<p>Configure your editor with these options...</p>
<h2 id="usage">Usage</h2>
<p>Here's how to use the TOC components...</p>
<h3 id="inline-toc">Inline TOC Node</h3>
<p>Insert a TOC node directly in your document...</p>
<h3 id="sidebar-toc">Sidebar TOC</h3>
<p>Add a floating sidebar for navigation...</p>
<h2 id="advanced">Advanced Features</h2>
<p>Explore advanced functionality...</p>
`,
})
return (
<EditorContext.Provider value={{ editor }}>
<div className="editor-wrapper">
{/* Toolbar */}
<div className="toolbar">
<button
onClick={() => editor?.commands.insertTocNode()}
disabled={!editor?.can().insertTocNode()}
>
Insert TOC
</button>
<TocShowTitleButton editor={editor} text="Toggle Title" hideWhenUnavailable={true} />
</div>
{/* Editor with Sidebar */}
<div className="editor-content-wrapper">
<EditorContent editor={editor} className="editor-content" />
<TocSidebar maxShowCount={20} topOffset={80} className="editor-toc-sidebar" />
</div>
</div>
</EditorContext.Provider>
)
}
export default function App() {
return (
<TocProvider>
<EditorComponent />
</TocProvider>
)
}Styling
The TOC components include default styles that can be customized using CSS or SCSS.
Required stylesheets
import '@/components/tiptap-node/toc-node/toc-node.scss'
import '@/components/tiptap-node/toc-node/ui/toc-sidebar/toc-sidebar.scss'Indentation and colors
The node and sidebar stylesheets use the normalized heading depth for indentation. Adjust --toc-indent or --toc-depth-factor on the corresponding container to change the spacing. The sidebar uses --tt-toc-sidebar-text-active-color for the active heading and --tt-toc-sidebar-line-bg-active for its progress indicator.
Dependencies
Use the Table of Contents extension to collect entries. The example uses the Heading extension included in StarterKit.