Navigation
Table of contents
An “On this page” list for long pages such as docs and articles: a link to each section, and a thumb on the rail that slides to the section being read as the page scrolls.
Installation
npx sinaui add tocCopy src/lib/ui/Toc.svelte and tokens.css. No dependencies.
Examples
This page's sections
The items are this page's own headings, so scrolling the page moves the highlight. Pass offset when a sticky header covers the top of the page.
<script lang="ts">
import Toc from '#lib/ui/Toc.svelte';
// This page's own sections: scroll it and the highlight follows.
const items = [
{ id: 'installation', label: 'Installation' },
{ id: 'examples', label: 'Examples' },
{ id: 'props', label: 'Props' },
{ id: 'accessibility', label: 'Accessibility' }
];
</script>
<Toc {items} offset={64} />Props
| Prop | Type | Default |
|---|---|---|
items | { id, label, level?: 2 | 3 }[] (required): each heading's id and text | |
label | string: the title and the navigation's name | 'On this page' |
offset | number: px covered by a fixed header, so a section counts once it clears it | 0 |
current | string, bindable: the section being read |
Accessibility
- A
navnamed by its title, with the links in an ordered list. - The section being read has
aria-current="location"; the thumb is decoration and snaps instead of sliding when the reader asks for less motion. - Give the headings
scroll-margin-topequal to your header's height so a jump doesn't hide the heading under it. - Renders nothing when there are no items, so a short page doesn't get an empty box.