current theme: placeholder

component :: Table of Content

The .toc component is an “Astro only” client-side rendered component. It creates a table of content from the headings on the page it’s on:

By default, the TOC includes H2s and H3s (customizable via props) that have an id attribute and links to them.

It’s possible to add multiple TOC on the same page, but this will duplicate Javascript and it’s not recommended. If you do need to include more than one TOC, you’ll need to add a unique id to each instance of the component.

File NameclassSource
component.toc.css.tocGithub

HTML

This component’s main functionality is to create a table of content via JavaScript. It just renders a nested ul and it’s most useful as an Astro component.

Custom properties

The following custom properties are available in the default theme:

PropertyColor
--toc-colorLinks color.
--toc-color-hoverLinks hover color.
--toc-spacingLinks padding.
--toc-icon-sizeIcon size.
--toc-indentationNested lists indentation.

Astro component

PropTypeDefaultDescription
idstringtocNeeded to add more than one TOC.
selectorstringmainHTML element to scan for headings.
headingsarray['h2','h3']Heading levels to include.
baseUrlstring''Optional URL to prepend #id with.
scrollOffsetinteger0Scroll position offset from the top..
descriptionstringundefinedText above the TOC.
iconstring''Optinal inline SVG. (See example.)
extraClassstringundefinedAdditional CSS, useful for helper classes.

Examples