Command palette

@yasinsensoy/web

Web · Components

TableOfContents

In-page table of contents: links to headings and highlights the section being read (scroll spy).

7 exports

Live example

Preview

Introduction

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

Installation

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

With npm

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

With pnpm

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

Usage

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

Theme

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

FAQ

This section is sample text. As you scroll, the section being read is highlighted in the list on the right; clicking a link scrolls smoothly to the section.

Code

import { TableOfContents } from "@yasinsensoy/web";

<TableOfContents container="#post" className="sticky top-20 w-56" updateHash={false} />

Events

  1. Events appear here when you interact with the preview.

Import

import { TableOfContents, useScrollSpy } from "@yasinsensoy/web";

TableOfContents

componentClient component

In-page table of contents: links to headings and highlights the section being read (scroll spy). For docs, blog posts, long settings pages.

  • Automatic: if items is omitted, the h2, h3 headings inside container are collected; a heading without an id gets one from its text. To leave a heading out use data-toc-ignore, for different text in the list data-toc-label.
  • Active section: found with useScrollSpy while scrolling; in the line look the marker slides smoothly. When the page reaches the end, the last section is active.
  • Jumping: clicking scrolls smoothly to the heading (instantly with reduced motion), leaves offset of space, updates the #id in the address and moves keyboard focus to the heading (the screen reader reads from there). The clicked heading stays active until the reader scrolls again, and the jump lands exactly even if the content above it changes height on the way.
  • Long lists: give the list a height (e.g. max-h-[calc(100dvh-8rem)] overflow-y-auto) and the active link is kept in view inside it.
  • Mobile: the list collapses into an "On this page: Installation" row; it opens on tap and closes on selection.

It is a client component; the content can be rendered on the server, this only renders the list.

TableOfContents(props: TableOfContentsProps): JSX.Element | null

Examples

<div className="flex gap-8">
  <article id="post">…</article>
  <TableOfContents container="#post" offset={64} className="sticky top-20 w-56" />
</div>

<TableOfContents items={[
  { id: "intro", label: "Introduction" },
  { id: "install", label: "Installation" },
  { id: "npm", label: "With npm", level: 3 },
]} />

useScrollSpy

function

Tracks which section of the page is being read: from the elements with the given ids, returns the id of the last section whose heading is above the reading line (offset from the top + threshold fraction of the screen). When the page is scrolled to the end, the last section becomes active (so short last sections can be selected too). Used by TableOfContents; can also be used for your own navigation menu.

The scroll event is handled once per frame (requestAnimationFrame); it stays light even with hundreds of sections.

useScrollSpy(ids: readonly string[], options?: ScrollSpyOptions): string | undefined

Examples

const active = useScrollSpy(["intro", "install", "usage"], { offset: 64 });
<a href="#install" aria-current={active === "install" ? "location" : undefined}>Installation</a>

TocItem

interface

A heading inside TableOfContents.

NameTypeDefaultDescription
idstringId of the heading element (id); the link becomes #id.
labelReactNodeText shown in the list.
level?number2Level: 2 (h2), 3 (h3)… The smallest level isn't indented.

Classes that can be passed to the inner parts of TableOfContents.

NameTypeDescription
title?stringTitle ("On this page").
list?stringList.
link?stringEach link.

Props of the TableOfContents component.

NameTypeDefaultDescription
items?readonly TocItem[]Headings. If omitted, they are collected automatically from the selector elements inside container (a heading without an id gets one from its text: "Getting Started" → getting-started).
container?HTMLElement | RefObject<HTMLElement | null> | stringArea to collect headings from: an element, a ref or a CSS selector (e.g. "article"). If omitted, the page's main element, otherwise body.
selector?string"h2, h3"Selector of headings to collect automatically.
scrollRoot?ScrollRootThe scrolled container; pass it if the page doesn't scroll (e.g. the content scrolls in a panel).
offset?number0Height of a sticky top bar (px). When jumping to a heading it doesn't end up under the bar; the active section is computed accordingly too.
title?ReactNode"Bu sayfada"` (`messages.tableOfContents`)Title of the list. null removes the title.
variant?"line" | "plain" | CustomVariant<"tableOfContents">"line"Look: line (a thin line on the left with a sliding active marker), plain (text color only). Classes of a custom style added with defineAppearance are added to the list (laid out like line unless extends is given). If omitted, UIProvider appearance.defaults.tableOfContents.variant.
effect?EffectPropEffect or effects of the links (e.g. "border", "shine"). Also plays on the link of the active (being read) heading. If omitted, UIProvider appearance.defaults.tableOfContents.effect. See BuiltinEffect.
updateHash?booleantrueWhether clicking a link updates the #id in the address bar. (No new history entry is added; the back button leaves the page.)
collapseOnMobile?booleantrueWhether on small screens (below 640 px) the list turns into a collapsible row showing the current section.
onActiveChange?(id: string | undefined) => voidCalled with its id when the active section changes.
className?stringClasses added to the container (e.g. sticky top-20).
classNames?TableOfContentsClassNamesClasses added to the inner parts.

Scroll container: an element, an element ref or a CSS selector. If omitted, the window.

type ScrollRoot = HTMLElement | RefObject<HTMLElement | null> | string | null | undefined

Options of useScrollSpy.

NameTypeDefaultDescription
offset?number0This many pixels below the top of the visible area count as the "reading line". If there is a sticky top bar, pass its height.
threshold?number0.25How far below the reading line a section may reach to count as active (fraction of the visible area height). 0.3: a section becomes active when its heading enters the top third of the screen.
root?ScrollRootThe scrolled container. If omitted, the page (window). See ScrollRoot.
enabled?booleantrueTracking stops when false.