Navigation
TableOfContents & Affix
Links to the sections of a long page with the current one highlighted as you scroll, and Affix to keep it in view.
import { TableOfContents, Affix } from '@reactloom/ui';Examples
Scroll-spy in a sticky column
Affix keeps the list in view; offset accounts for a sticky header.
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.
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
| Prop | Type | Default |
|---|---|---|
itemsrequiredSections, in page order. id is the target element’s id. | { id, label, level? }[] | — |
titleVisible heading above the list. | ReactNode | — |
aria-labelName of the navigation landmark. | string | 'On this page' |
spyHighlight the section in view. | boolean | true |
offsetDistance from the top at which a section counts as current (px). | number | 80 |
activeId · onActiveChangeControlled current section. | string · (id) => void | — |
| Prop | Type | Default |
|---|---|---|
offsetDistance from the edge while stuck. | number | string | 0 |
positionEdge 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
| Variable | Default |
|---|---|
--pl-toc-active-color | var(--pl-color-accent-solid) · var(--pl-color-accent-fg) |
--pl-toc-indent | var(--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
| Variable | Default |
|---|---|
--pl-affix-z | var(--pl-z-sticky) |
Accessibility
- A
navlandmark with a name; the current section’s link hasaria-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.
| Key | Action |
|---|---|
| Tab | Move between the section links |
| Enter | Go 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.