Components
TOC Element

TOC Element

A table of contents element for displaying hierarchical document structure.

Installation

npx @udecode/plate-ui@latest add toc-element -r plate-ui

Examples

Table of Contents

The Table of Contents (TOC) feature allows you to create an automatically updated overview of your document's structure.
How to use the Table of Contents:
  • Type "/toc" and press Enter to create the TOC.
  • The TOC updates automatically when you modify headings in the document.


Example Content

This is an example of content that would be reflected in the Table of Contents.

Subsection

Adding or modifying headings in your document will automatically update the TOC.

Benefits of Using TOC

A Table of Contents improves document navigation and provides a quick overview of your content structure.
import { cn, withRef } from '@udecode/cn';
import {
  useTocElement,
  useTocElementState,
} from '@udecode/plate-heading/react';
import { cva } from 'class-variance-authority';
 
import { Button } from './button';
import { PlateElement } from './plate-element';
 
const headingItemVariants = cva(
  'block h-auto w-full cursor-pointer truncate rounded-none px-0.5 py-1.5 text-left font-medium text-muted-foreground underline decoration-[0.5px] underline-offset-4 hover:bg-accent hover:text-muted-foreground',
  {
    variants: {
      depth: {
        1: 'pl-0.5',
        2: 'pl-[26px]',
        3: 'pl-[50px]',
      },
    },
  }
);
 
export const TocElement = withRef<typeof PlateElement>(
  ({ children, className, ...props }, ref) => {
    const state = useTocElementState();
 
    const { props: btnProps } = useTocElement(state);
 
    const { headingList } = state;
 
    return (
      <PlateElement
        ref={ref}
        className={cn('relative mb-1 p-0', className)}
        {...props}
      >
        <nav contentEditable={false}>
          {headingList.length > 0 ? (
            headingList.map((item) => (
              <Button
                key={item.id}
                variant="ghost"
                className={cn(
                  headingItemVariants({ depth: item.depth as any })
                )}
                onClick={(e) => btnProps.onClick(e, item, 'smooth')}
                aria-current
              >
                {item.title}
              </Button>
            ))
          ) : (
            <div className="text-sm text-gray-500">
              Create a heading to display the table of contents.
            </div>
          )}
        </nav>
        {children}
      </PlateElement>
    );
  }
);

Plate Plus

The TocSideBar component in Plate Plus offers the following features:

  • Responsive design that adapts to different screen sizes
  • Dynamic highlighting of the corresponding thumbnail on the right side based on the current section
  • Hover thumbnail to see the preview of the section with smooth animation
  • Elegant transition effects when navigating between sections
  • Animated highlighting of the current section in the sidebar

Here's an example of how to use the enhanced TocSideBar component in Plate Plus: