Table of Contents
components/demo.tsx
'use client';
import React from 'react';
import { Plate } from '@udecode/plate/react';
import { editorPlugins } from '@/components/editor/plugins/editor-plugins';
import { useCreateEditor } from '@/components/editor/use-create-editor';
import { Editor, EditorContainer } from '@/components/plate-ui/editor';
import { DEMO_VALUES } from './values/demo-values';
export default function Demo({ id }: { id: string }) {
const editor = useCreateEditor({
plugins: [...editorPlugins],
value: DEMO_VALUES[id],
});
return (
<Plate editor={editor}>
<EditorContainer variant="demo">
<Editor />
</EditorContainer>
</Plate>
);
}
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: {
topOffset: 80,
},
}),
];
const components = {
// ...otherComponents,
[TocPlugin.key]: TocElement,
};
See also:
Scroll container
If your scrolling element is:
- EditorContainer - no additional setup needed
- Editor container - use
useEditorContainerRef()
:
// Below <Plate> component
function EditorContainer({ children }: { children: React.ReactNode }) {
const containerRef = useEditorContainerRef();
return <div ref={containerRef}>{children}</div>;
}
- Ancestor of editor container - use
useEditorScrollRef()
:
// Below <Plate> component
function Layout() {
const scrollRef = useEditorScrollRef();
return (
<main ref={scrollRef}>
<EditorContainer>
<PlateContent />
</EditorContainer>
</main>
);
}
Components
Examples
Plate UI
Refer to the preview above.
Plate Plus
Plugins
TocPlugin
Transforms
insertToc
Hooks
useTocElementState
useTocElement
useTocSideBarState
Manage TOC sidebar state.
useTocSideBar
Handle TOC sidebar interactions.