Renders a table of contents element with clickable links to headings in the document.
Enter and SpaceTOC tracks headings with editor-scoped NodeKey values. It does not require
persisted element IDs or install ElementIdPlugin.
The fastest way to add table of contents functionality is with the TocKit, which includes pre-configured TocPlugin with the Plate UI component.
'use client';
import { cva } from 'class-variance-authority';
import type { NodeKey } from 'platejs';
import { BaseDetailsPlugin } from 'platejs/details';
import {
type EditorElementProps,
NavigationFeedbackPlugin,
EditorElement,
useEditor,
useEditorSelector,
useElementSelected,
} from 'platejs/react';
import { TocPlugin } from 'platejs/toc/react';
import * as React from 'react';
import { Button } from '@/components/ui/button';
const headingItemVariants = cva(
'block h-auto w-full cursor-pointer truncate rounded-none px-0.5 py-1.5 text-start font-medium text-muted-foreground underline decoration-[0.5px] underline-offset-4 hover:bg-accent hover:text-foreground',
{
variants: {
level: {
1: 'ps-0.5',
2: 'ps-[26px]',
3: 'ps-[50px]',
4: 'ps-[74px]',
5: 'ps-[98px]',
6: 'ps-[122px]',
},
},
}
);
export function TocElement({
isScroll = true,
topOffset = 80,
...props
}: EditorElementProps<typeof TocPlugin> & {
isScroll?: boolean;
topOffset?: number;
}) {
const editor = useEditor();
const navigation = useEditor().plugin(NavigationFeedbackPlugin);
const details = editor.plugin(BaseDetailsPlugin);
const openDetailsHolding = (key: NodeKey) => {
const path = (details.installed && editor.read.nodes.path(key)) || [];
const closedKeys = path.flatMap((childIndex, depth) => {
const ancestor = path.slice(0, depth);
const isTitle = childIndex === 0;
const detailsKey =
depth > 0 &&
!isTitle &&
editor.read.nodes.get(ancestor, { type: BaseDetailsPlugin })
? editor.key(ancestor)
: undefined;
return detailsKey && !details.api.isOpen(detailsKey) ? [detailsKey] : [];
});
for (const detailsKey of closedKeys) {
details.api.setOpen(detailsKey, true);
}
return closedKeys.length > 0;
};
const selected = useElementSelected({ mode: 'text' });
const headingList = useEditorSelector(
(innerEditor) => innerEditor.plugin(TocPlugin).read.headings(),
{
equalityFn: (previous, next) =>
previous.length === next.length &&
previous.every((heading, index) => {
const nextHeading = next[index];
return (
heading.key === nextHeading?.key &&
heading.level === nextHeading.level &&
heading.title === nextHeading.title
);
}),
shouldUpdate: (change) => !change || change.changed.hasAny('document'),
}
);
return (
<EditorElement {...props} className="mb-1 p-0">
<div
className="rounded-sm data-[selected=true]:bg-primary/10"
contentEditable={false}
data-selected={selected}
>
{headingList.length > 0 ? (
headingList.map((item) => (
<Button
key={item.key}
variant="ghost"
className={headingItemVariants({ level: item.level })}
onClick={(event) => {
event.preventDefault();
const navigate = () => {
const entry = editor.read.nodes.get(item.key);
if (!entry) return;
const element = editor.api.dom.resolveDOMNode(entry[0]);
if (!element) return;
element.style.scrollMarginTop = `${topOffset}px`;
if (isScroll) {
editor.api.dom.scrollIntoView(entry[1], {
behavior: window.matchMedia(
'(prefers-reduced-motion: reduce)'
).matches
? 'auto'
: 'smooth',
block: 'start',
scrollMode: 'always',
});
}
navigation.api.flashTarget({
key: item.key,
attributes: {
className: 'rounded-md bg-(--color-highlight)',
},
});
};
if (openDetailsHolding(item.key)) {
requestAnimationFrame(navigate);
} else {
navigate();
}
}}
>
{item.title}
</Button>
))
) : (
<div className="text-sm text-muted-foreground">
Create a heading to display the table of contents.
</div>
)}
</div>
{props.children}
</EditorElement>
);
}
export const TocKit = [
TocPlugin.configure({
component: TocElement,
}),
];'use client';
import { cva } from 'class-variance-authority';
import type { NodeKey } from 'platejs';
import { BaseDetailsPlugin } from 'platejs/details';
import {
type EditorElementProps,
NavigationFeedbackPlugin,
EditorElement,
useEditor,
useEditorSelector,
useElementSelected,
} from 'platejs/react';
import { TocPlugin } from 'platejs/toc/react';
import * as React from 'react';
import { Button } from '@/components/ui/button';
TocElement: Renders table of contents elements.Add the kit to your plugins:
import { createEditor } from 'platejs/react';
import { TocKit } from '@/components/editor/toc';
const editor = createEditor({
plugins: [
// ...otherPlugins,
...TocKit,
],
});import { createEditor } from 'platejs/react';
import { TocKit } from '@/components/editor/toc';
const editor = createEditor({
Include TocPlugin and HeadingPlugin in your Plate plugins array when creating the editor.
import { TocPlugin } from 'platejs/toc/react';
import { HeadingPlugin } from 'platejs/react';
import { createEditor } from 'platejs/react';
const editor = createEditor({
plugins: [
// ...otherPlugins,
HeadingPlugin,
TocPlugin,
],
});import { TocPlugin } from 'platejs/toc/react';
import { HeadingPlugin }
Configure the TocPlugin with custom component and scroll options.
import { TocPlugin } from 'platejs/toc/react';
import { HeadingPlugin } from 'platejs/react';
import { createEditor } from 'platejs/react';
import { TocElement } from '@/components/editor/toc';
import { HeadingElement } from '@/components/editor/heading';
const editor = createEditor({
plugins: [
// ...otherPlugins,
HeadingPlugin.configure({ component: HeadingElement }),
TocPlugin.configure({
component: (props) => <TocElement {...props} topOffset={80} isScroll
.configure({ component }): Assigns TocElement to render table of contents elements.TocElement.topOffset: Sets the top offset when scrolling to headings.TocElement.isScroll: Enables scrolling behavior to headings.You can add this item to the Insert Toolbar Button to insert table of contents elements:
{
icon: <TableOfContentsIcon />,
label: 'Table of contents',
value: PLUGINS.toc,
}{
icon: <TableOfContentsIcon />,
label: 'Table of contents',
value: PLUGINS.toc,
}Use EditorContainer for the editor's scrolling region.
For a custom layout, render EditorContainer inside <EditorRoot> and apply your
scrolling styles to it. EditorContainer registers that region with the editor.
import { EditorContainer, EditorContent } from 'platejs/react';
function Layout() {
return (
<EditorContainer className="h-[600px] overflow-y-auto">
<EditorContent />
</EditorContainer>
);
}import { EditorContainer, EditorContent } from 'platejs/react';
function Layout() {
return (
The copied TocElement, the static TocElementStatic and Word export all
render the plugin's read.headings(). To
change which headings they list, extend the plugin's read and filter
readTocHeadings, the plugin's own heading finder. One app
plugin then serves the live kit, the static kit and Word export. Keep it in a
module that imports only platejs/toc, so the server-rendered static kit can
import it.
import { BaseTocPlugin, readTocHeadings } from 'platejs/toc';
export const AppTocPlugin = BaseTocPlugin.extend({
read: ({ editor, state }) => ({
headings: () =>
readTocHeadings({ editor, state }).filter((heading) => heading.level <= 3),
}),
});import { BaseTocPlugin, readTocHeadings } from 'platejs/toc';
export const AppTocPlugin = BaseTocPlugin.extend({
read: ({ editor, state }) => ({
headings: () =>
readTocHeadings({ editor, state }).filter((heading) => heading.level <= 3),
}),
});Install it in place of TocPlugin in the client toc file:
import { toReactPlugin } from 'platejs/react';
import { AppTocPlugin } from '@/components/editor/app-toc-plugin';
export const TocKit = [
toReactPlugin(AppTocPlugin).configure({ component: TocElement }),
];import { toReactPlugin } from 'platejs/react';
import { AppTocPlugin } from '@/components/editor/app-toc-plugin';
export const TocKit = [
toReactPlugin(AppTocPlugin).configure({ component: TocElement }),
];Install it in place of BaseTocPlugin in toc-static:
import { AppTocPlugin } from '@/components/editor/app-toc-plugin';
export const BaseTocKit = [
AppTocPlugin.configure({ component: TocElementStatic }),
];import { AppTocPlugin } from '@/components/editor/app-toc-plugin';
export const BaseTocKit = [
AppTocPlugin.configure({ component: TocElementStatic }),
];Pass the read's own editor and state to readTocHeadings, so a static
render or a document view lists its own headings. The finder walks state but
reads each level through editor's heading plugin, so both must come from the
same read.
Plugin for the table of contents element. It has no options; its
read.headings() lists the headings, and
a read override changes them.
Scroll options belong to the copied view. The heading list belongs to TocPlugin.
Lists the document's headings in document order. The copied TOC renders this
list, and the static TOC and Word export read it through BaseTocPlugin.
The plugin's own heading finder, exported so a read override can filter it. It
returns [] when no heading plugin is installed and skips headings with no
text.
import { readTocHeadings } from 'platejs/toc';import { readTocHeadings } from 'platejs/toc';One table of contents entry.
Insert table of contents element.
import { BaseTocPlugin } from 'platejs/toc';
editor.plugin(BaseTocPlugin).update.insert();import { BaseTocPlugin } from 'platejs/toc';
editor.plugin(BaseTocPlugin).update.insert();The copied toc component owns scrolling and
navigation feedback. Keep those product-facing interaction choices beside the
renderer; the package plugin owns heading discovery through
editor.plugin(TocPlugin).read.headings().
import { TocPlugin } from 'platejs/toc/react';
import { HeadingPlugin } from 'platejs/react';
import { createEditor } from 'platejs/react';
import { TocElement } from '@/components/editor/toc';
import { HeadingElement } from '@/components/editor/heading';
const editor = createEditor({
plugins: [
// ...otherPlugins,
HeadingPlugin.configure({ component: HeadingElement }),
TocPlugin.configure({
component: (props) => <TocElement {...props} topOffset={80} isScroll />,
}),
],
});