Table of Contents
Renders a table of contents element with clickable links to headings in the document.
Installation
npm install @udecode/plate-heading @udecode/plate-node-id
Usage
import { TocPlugin, HeadingPlugin } from '@udecode/plate-heading/react';
import { NodeIdPlugin } from '@udecode/plate-node-id';
const plugins = [
// ...otherPlugins,
HeadingPlugin,
NodeIdPlugin,
TocPlugin.configure({
options: {
scrollContainerSelector: `#your-scroll-container-id`,
topOffset: 80,
},
}),
];
const components = {
// ...otherComponents,
[TocPlugin.key]: TocElement,
};
Set scroll container
The TocPlugin
requires a scroll container selector to function properly. This selector is used to identify the scrollable area containing your content.
TocPlugin.configure({
options: {
scrollContainerSelector: '#your-scroll-container-id',
},
})
You can use any valid CSS selector:
- ID:
#scrollable-container
- Class:
.content-wrapper
- Nested selector:
body #main-content
Apply this selector to your <Editor />
component or a wrapper <div>
around it, depending on your layout.
For consistency, use the same container for both TocPlugin
and BlockSelectionPlugin
.
Components
Examples
Plate UI
Refer to the preview above.
Plate Plus
- Sticky TOC sidebar
- Hover-to-expand: Opens automatically when you move your mouse over it
- Interactive navigation: Click on items to smoothly scroll to the corresponding heading
- Visual feedback: Highlights the current section in the sidebar
- Beautifully crafted UI
Plugins
TocPlugin
Options
- Default:
true
- Default:
80
- Default:
'#scroll_container'
Whether to use scrolling behavior.
The top offset to apply when scrolling to a heading.
A custom function to query headings from the editor.
The CSS selector for the scrollable container. Required to use scrolling behavior.
Transforms
insertToc
Inserts a table of contents element into the editor.
Parameters
The editor instance.
Options for inserting the TOC node.
Hooks
useTocElementState
Returns
The editor instance.
An array of heading objects in the document.
A function to handle scrolling to a specific heading.
useTocElement
Parameters
The editor instance.
The scroll handler function from useTocElementState
.
Returns
useTocSideBarState
This hook manages the state for the TOC sidebar.
Parameters
- Default:
true
- Default:
'0px 0px 0px 0px'
- Default:
0
Whether the TOC sidebar is open.
Root margin for the Intersection Observer.
Top offset for scrolling.
Returns
ID of the currently active content section.
The editor instance.
List of headings in the document.
Whether the mouse is currently over the TOC.
Whether the TOC sidebar is open.
Function to set the observation state.
Function to set whether the mouse is over the TOC.
Ref for the TOC element.
Function to handle content scrolling.
useTocSideBar
This hook provides props and handlers for the TOC sidebar component.
Parameters
The editor instance.
Whether the mouse is currently over the TOC.
Whether the TOC sidebar is open.
Function to set the observation state.
Function to set whether the mouse is over the TOC.
Ref for the TOC element.
Function to handle content scrolling.
Returns
Props for the TOC navigation element.
Handler for clicking on a TOC item.
Customizable and extensible.