New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

@zachleat/table-of-contents

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@zachleat/table-of-contents

A zero-dependency web component that progressively enhances (or optionally client renders) a table of contents from the headings on the page and highlights visible sections.

latest
Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
53
26.19%
Maintainers
1
Weekly downloads
 
Created
Source

<table-of-contents>

A zero-dependency web component that progressively enhances (or optionally client renders) a table of contents from the headings on the page and highlights visible sections. This component includes no styles (all visuals are app-code controlled).

Installation

npm install @zachleat/table-of-contents

Usage

<script type="module" src="table-of-contents.js"></script>

<nav aria-labelledby="toc-title">
	<table-of-contents>
		<h2 id="toc-title">Table of Contents</h2>
	</table-of-contents>
</nav>

Child content is kept and the list is added after it. Wrap it in a labeled <nav> so assistive technology can find it. The element is hidden when no headings match.

Entries for headings hidden by CSS (e.g. display: none on an ancestor) get the hidden attribute and update when visibility changes. The element is hidden when every heading is hidden.

A child <ol> or <ul> (for example, server-rendered) is used instead of generating one, and its # links are highlighted for headings matching selector.

Add ?nodefine to skip auto-registration and define it yourself:

import { TableOfContents } from "./table-of-contents.js?nodefine";

TableOfContents.define("my-toc");

?nodefine reads import.meta.url, so it only works with unbundled ES modules.

Attributes

  • selector: headings to include (default main :is(h2, h3, h4, h5, h6)). Headings without an id are listed without a link.

Styling

No styles are included. Links for sections in view get the toc-in-view class.

Changelog

  • v2.0.0 tracks display of heading elements (and hides links when display is toggled off)

Keywords

web-components

FAQs

Package last updated on 24 Sep 2026

Related posts