Table of Contents
Allows you to quickly navigate the hierarchy of headings for the current page.
import { TableOfContents, tocCrawler } from '@skeletonlabs/skeleton';
Demo
How It Works
Heading IDs
Each heading on the page requires a unique ID that acts as the scroll target for inner-page navigation.
<h2 class="h2" id="how-it-works">How it Works</h2>
Anchor Links
The Table of Contents list of links will point to each target ID as shown below. Note the use of #
. When
clicked, the browser will automatically scroll the page to this unique element ID on the page.
<a href="#how-it-works">How It Works</a>
Settings
Automatic IDs
Set mode: generate
for tocCrawler
to automatically generate and set unique IDs based
on the text content of the heading. If an ID has already been set manually, it will be used instead. If this setting is not enabled,
then you are responsible for implementing all heading IDs.
<div use:tocCrawler={{ mode: 'generate' }}>
Ignore Headings
To ignore a heading in the target region, simply append a data-toc-ignore
attribute.
<h2 class="h2" data-toc-ignore>Ignore Me</h2>
Append Headings
Use the Tailwind-provided Screen Reader .sr-only
class to append an invisible heading.
<h2 class="sr-only">Include Me!</h2>
Styling
Smooth Scrolling
Use Tailwind's scroll behavior styles to enable smooth scrolling on the scrollable element.
<!-- If using the App Shell: -->
<AppShell regionPage="scroll-smooth"></AppShell>
<!-- If NOT using the App Shell: -->
<body class="scroll-smooth"></body>
Scroll Margin Offset
Use Tailwind's scroll margin if you need to offset and account for sticky headers
NOTE: this is not currently supported for Skeleton's App Shell.
<body class="scroll-mt-4"></body>
Sticky Positioning
Use Tailwind's sticky positioning styles to keep the Table of Contents component visible while scrolling.
<TableOfContents class="sticky top-10" />