Examples

Scroll-spy in a sticky column

Affix keeps the list in view; offset accounts for a sticky header.

Preview

Overview

Scroll the page: the current section is marked in the list. Selecting a link moves focus to its section.

Installation

Scroll the page: the current section is marked in the list. Selecting a link moves focus to its section.

Configuration

Scroll the page: the current section is marked in the list. Selecting a link moves focus to its section.

Usage

Scroll the page: the current section is marked in the list. Selecting a link moves focus to its section.

Example.tsx
import { Affix, Heading, TableOfContents, Text } from '@reactloom/ui';

const sections = [
  { id: 'toc-demo-overview', label: 'Overview' },
  { id: 'toc-demo-install', label: 'Installation' },
  { id: 'toc-demo-config', label: 'Configuration', level: 2 },
  { id: 'toc-demo-usage', label: 'Usage' },
];

export default function Example() {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: 'minmax(0, 1fr) 12rem', gap: 24, width: '100%' }}>
      <div>
        {sections.map((s) => (
          <section key={s.id} style={{ minHeight: 260 }}>
            <Heading level={s.level === 2 ? 4 : 3} id={s.id}>
              {s.label}
            </Heading>
            <Text>Scroll the page: the current section is marked in the list. Selecting a link moves focus to its section.</Text>
          </section>
        ))}
      </div>
      <Affix offset={80}>
        <TableOfContents title="On this example" aria-label="Example contents" items={sections} />
      </Affix>
    </div>
  );
}

API reference

TableOfContents props
PropTypeDefault
itemsrequired
Sections, in page order. id is the target element’s id.
{ id, label, level? }[]—
title
Visible heading above the list.
ReactNode—
aria-label
Name of the navigation landmark.
string'On this page'
spy
Highlight the section in view.
booleantrue
offset
Distance from the top at which a section counts as current (px).
number80
activeId · onActiveChange
Controlled current section.
string · (id) => void—
Affix props
PropTypeDefault
offset
Distance from the edge while stuck.
number | string0
position
Edge to stick to.
'top' | 'bottom''top'

Customization

Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.

TableOfContents

Slot class names

Add classes to inner parts with classNames (className is shorthand for root): root, title, list, item, link.

CSS variables

VariableDefault
--pl-toc-active-colorvar(--pl-color-accent-solid) · var(--pl-color-accent-fg)
--pl-toc-indentvar(--pl-space-3)

Theme tokens it reads

--pl-color-accent-fg --pl-color-accent-solid --pl-color-border-default --pl-color-fg-default --pl-color-fg-muted --pl-focus-ring-color --pl-focus-ring-width --pl-font-size-sm --pl-radius-xs

Affix

CSS variables

VariableDefault
--pl-affix-zvar(--pl-z-sticky)

Accessibility

  • A nav landmark with a name; the current section’s link has aria-current="location".
  • Following a link scrolls to the section and moves focus there, and the address updates without adding history entries.
  • Respects reduced motion: no smooth scrolling.
Keyboard interactions
KeyAction
TabMove between the section links
EnterGo to the section and move focus to it

Usage guidelines

Do

  • Use it on long pages with three or more sections.

Don’t

  • Don’t list every heading level; one or two levels read best.