vamsiy78

Table Of Contents

Navigation

About

An "on this page" navigation list that auto-collects headings (or takes an explicit list), highlights the current section with a scroll spy, slides an indicator to it, and smooth-scrolls below a sticky header on click.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Table Of Contents to your project and fits it to your theme.

Code

How Table Of Contents is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client";
import * as React from "react";
import TableOfContents from "@/components/ui/table-of-contents";
const sections = [
  ["Overview", ["What this covers", "Who it is for"]],
  ["Installation", ["Requirements", "Add the package"]],
  ["Usage", ["Basic example", "Options"]],
  ["Accessibility", []],
] as const;
export default function TableOfContentsDemo() {
  const scroller = React.useRef<HTMLDivElement>(null);
  return (
    <div className="grid w-full max-w-2xl gap-6 rounded-xl border bg-card p-4 shadow-sm sm:grid-cols-[1fr_12rem]">
      <div
        ref={scroller}
        tabIndex={0}
        aria-label="Guide"
        className="h-72 overflow-auto pe-2 outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50"
      >
        <article className="grid gap-3 text-sm leading-relaxed text-muted-foreground">
          {sections.map(([title, subs]) => (
            <section key={title} className="grid gap-3">
              <h2 className="pt-2 text-lg font-semibold tracking-tight text-foreground">
                {title}
              </h2>
              <p>
                Short introduction to {title.toLowerCase()}. Enough text that
                the section has real height and scrolling feels natural.
              </p>
              {subs.map((s) => (
                <React.Fragment key={s}>
                  <h3 className="pt-1 text-base font-medium text-foreground">
                    {s}
                  </h3>
                  <p>
                    Details about {s.toLowerCase()}, written as plain paragraphs
                    for the demo.
                  </p>
                </React.Fragment>
              ))}
              <p className="pb-10">
                More detail follows here so the next heading is not in view yet.
              </p>
            </section>
          ))}
        </article>
      </div>
      <TableOfContents
        headingsFrom={scroller}
        container={scroller}
        offset={12}
        className="max-sm:hidden"
      />
    </div>
  );
}

Dependencies

More by vamsiy78

View all
  • Segmented Control

    Segmented Control

    8
  • Agent Roster

    Agent Roster

    4
  • API Key Field

    API Key Field

    1
  • Section Tabs

    Section Tabs

  • FAQ Accordion Section

    FAQ Accordion Section

  • Signature Pad

    Signature Pad

  • HUD

    HUD

  • Border Beam

    Border Beam

More in Navigation

View all
  • Footer with Big Wordmark

    Footer with Big Wordmark

    81
  • Liquid Glass Expandable Sidebar

    Liquid Glass Expandable Sidebar

    255
  • Button Breadcrumb

    Button Breadcrumb

    36
  • Sidebar

    Sidebar

    414
  • Astryx Breadcrumb Item

    Astryx Breadcrumb Item

  • Menu

    Menu

    27
  • Progress Bar Stepper

    Progress Bar Stepper

    64
  • Pagination

    Pagination

    14