react-fluid-toc
v0.1.1
Published
A portable React table-of-contents with a fluid animated SVG rail, range scroll-spy highlight, and a travelling dot. Zero runtime deps, themed via CSS variables.
Maintainers
Readme
react-fluid-toc
A portable React table of contents with a fluid animated SVG rail: a continuous bezier track, a range-based scroll-spy highlight, and a dot that rides the line to the active section — plus auto-scroll of its own overflow.
- 🪶 Zero runtime dependencies — React is the only peer.
- 🎨 No Tailwind, no required CSS import — themed with CSS variables.
- ⚡ SSR-friendly — renders from a prepared
itemsarray. - 🧩 Unopinionated layout — you position it (sticky, drawer, sidebar).

Install
npm install react-fluid-tocQuick start
import { FluidToc, type TocItem } from 'react-fluid-toc'
const items: TocItem[] = [
{ id: 'intro', text: 'Introduction', depth: 2 },
{ id: 'install', text: 'Install', depth: 2 },
{ id: 'options', text: 'Options', depth: 3 }
]
export function Sidebar() {
return (
<aside
style={{
position: 'sticky',
top: 96,
maxHeight: 'calc(100vh - 8rem)',
overflowY: 'auto'
}}
>
<FluidToc items={items} scrollOffset={96} />
</aside>
)
}items[].id must match the id of the heading elements on the page — those are
the anchors the entries link and scroll to.
Props
| Prop | Type | Default | Description |
| -------------- | ------------------------------------------- | ------- | ----------------------------------------------------------------------- |
| items | TocItem[] | — | The outline. { id, text, depth }. |
| scrollOffset | number | 0 | Pixels to clear for a fixed header (scroll-spy band + click scrolling). |
| smoothScroll | boolean | true | Smooth-scroll on click / auto-scroll. |
| autoScroll | boolean | true | Scroll the component's own box to keep the active entry visible. |
| onItemClick | (id: string, e: React.MouseEvent) => void | — | Override the default click-to-scroll behaviour. |
| className | string | — | Applied to the root <nav>. |
| style | React.CSSProperties | — | Merged onto the root <nav>. |
Theming
Colors come from CSS custom properties (with fallbacks), so it looks fine with no setup and is fully overridable. Set these anywhere above the component:
| Variable | Purpose | Default |
| -------------------- | ------------------- | -------------------- |
| --fluid-toc-rail | inactive track line | rgba(0, 0, 0, 0.1) |
| --fluid-toc-accent | active line + dot | currentColor |
| --fluid-toc-muted | inactive item text | #71717a |
| --fluid-toc-active | active item text | --fluid-toc-accent |
.docs-toc {
--fluid-toc-accent: #6366f1;
--fluid-toc-muted: #71717a;
}
@media (prefers-color-scheme: dark) {
.docs-toc {
--fluid-toc-rail: rgba(255, 255, 255, 0.12);
--fluid-toc-muted: #a1a1aa;
}
}Preparing items
The component is data-driven; you own how the outline is produced.
Server-side (recommended for SSR): extract headings when you render the content (from your markdown/MDX pipeline or your HTML) and pass the array down.
Client-side convenience: extractHeadings reads rendered headings from the
DOM.
import { extractHeadings } from 'react-fluid-toc'
const items = extractHeadings(document.querySelector('.article')!) // h2, h3
const custom = extractHeadings(container, 'h2, h3, h4') // custom selectorextractHeadings(root, selector?) returns { id, text, depth }[], skipping
headings without an id.
How it works
The rail is one continuous SVG path built from the rendered anchor offsets, with
bezier joins between indent levels. The accent-colored copy of that path is
revealed through an animated clip-path window, and a dot is pinned to the
leading edge — all driven by CSS variables set imperatively, so scrolling never
re-renders the SVG. Active detection is a range (first..last visible heading)
with a nearest-heading fallback so the indicator never disappears.
Credits
The animated rail is a re-implementation of the table of contents in fumadocs. The SVG technique behind it — building one path from the item positions and revealing it through an animated mask/clip — is explained by fumadocs' author in "The Art of SVG". All credit for the original idea goes there.
License
MIT © Yancey Leo
