sv-toc is a simple Svelte library for reactive tables of contents, including scroll tracking, automatic heading detection and updates, and (if you want) ID generation for headings.
- Automatic heading detection
- Customizable heading selector (even non non-
<h*>elements should work) - Customizable selector for where to search for headings
- DOM watching for heading changes
- Scroll tracking to mark headings as active
- Customizable selector for a scroll container instead of
window - Ability to specify top/bottom offsets
- Three modes:
firstandlasthighlight only one heading as active at a time, whileallhighlights all sections that are currently in view
- Customizable selector for a scroll container instead of
- ID generation for headings
npm install sv-toc
yarn add sv-toc
pnpm add sv-toc
bun add sv-toc<script lang="ts">
import { Toc } from "sv-toc";
const toc = new Toc({
headingSelector: "h2, h3, h4, h5, h6",
});
</script>
<!-- Now do whatever you want with toc.current -->
{#each toc.current as heading, index (index)}
...
{/each}You can find the full documentation here.