GooseUI

Components

Table of Contents

New

The table of contents of a page, with the current section highlighted as you scroll. The list is rendered on the server as plain anchor links, so navigation works without JavaScript. A small client island adds the rest: it highlights exactly one current section, scrolls smoothly with an offset and updates the hash without a jump. Nested headings render as nested lists.

Browser Support

scrollend

Tracks when scrolling ends

Your browser:

Preview

Scroll the article inside the box.

Getting started

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

Install the CLI

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

Sign in

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

Your first project

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

Folder structure

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

Deploy

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

What next

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

Installation

npx shadcn@latest add @gooseui/toc-scrollspy
pnpm dlx shadcn@latest add @gooseui/toc-scrollspy
yarn dlx shadcn@latest add @gooseui/toc-scrollspy
bunx --bun shadcn@latest add @gooseui/toc-scrollspy

Usage

import { TocScrollspy } from "@/components/ui/toc-scrollspy"

const items = [
  { id: "intro", title: "Introduction", level: 2 },
  { id: "install", title: "Installation", level: 2 },
  { id: "cli", title: "Using the CLI", level: 3 },
  { id: "faq", title: "FAQ", level: 2 },
]

export default function Article() {
  return (
    <div className="grid grid-cols-[1fr_14rem] gap-10">
      <article>{/* headings with matching ids */}</article>
      <TocScrollspy items={items} offset={80} />
    </div>
  )
}

Examples

Collect headings from a container

Skip items and pass selector instead: headings are read from container, and missing ids are generated from the text. In this mode the island renders a flat list after hydration, so it needs JavaScript.

Overview

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

Pricing

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

Security

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

Support

Short paragraph of placeholder text. Scroll the article and watch the marker on the right follow the section you are reading.

const articleRef = useRef<HTMLDivElement>(null)

<article ref={articleRef}>…</article>
<TocScrollspy selector="h2, h3" container={articleRef} title={null} />

Page with a sticky header

With the window as the scroll root, set offset to the header height. It moves the activation line and the scroll target, and also sets the sticky top.

// Header is 64px tall, keep 16px of air
<TocScrollspy
  items={items}
  offset={80}
  labels={{ nav: "Article contents", title: "Contents" }}
  onNavigate={(id) => console.log("go to", id)}
/>

Props

PropTypeDefaultDescription
itemsTocScrollspyItem[]-Items: id, title and level. Deeper levels are nested
selectorstring-CSS selector for headings when items are not passed
containerHTMLElement | RefObjectdocumentWhere to look for headings. The document by default
rootHTMLElement | RefObjectwindowScroll container. The window by default
offsetnumber16Space above the target in px: sticky top, scroll offset and activation line
stickybooleantrueStick to the top of the scroll area
updateHashbooleantrueWrite #id to the URL on click via history.replaceState
titleReactNode | null"On this page"Heading above the list. null hides it
listClassNamestring-Classes for the list wrapper, e.g. max-h-[70vh] overflow-y-auto
footerReactNode-Extra content under the list, e.g. an edit link
labels{ nav?: string; title?: string }-Texts: nav (aria-label) and title
onNavigate(id: string) => void-Called with the id after a click
classNamestring-Additional CSS classes

Features

  • One active item: exactly one link is highlighted at a time, bold and in the foreground color
  • No jump on click: smooth scroll, the hash is updated with replaceState and focus moves to the heading
  • Nested levels: h3 under h2 are rendered as a nested list
  • Works without JS: with items passed, the server renders regular anchor links