@papit/table-of-content
v0.0.5
Published
A page-aware table of contents that builds a tree from the page's headings and highlights the ones currently in view. Clicking an item jumps to its section via the URL hash.
Maintainers
Readme
@papit/table-of-content
A page-aware table of contents that builds a tree from the page's headings and highlights the ones currently in view. Clicking an item jumps to its section via the URL hash.
Installation
npm install @papit/table-of-contentHTML
<script type="module" defer>
import "@papit/table-of-content";
</script>
<pap-table-of-content></pap-table-of-content>JavaScript / TypeScript
import "@papit/table-of-content";Usage
Place <pap-table-of-content> anywhere on the page. By default it scans the root node (document or shadow root) for all heading elements and role="heading" elements, excluding those with aria-hidden or data-skipped.
<pap-table-of-content></pap-table-of-content>
<main>
<h1>Introduction</h1>
<h2>Getting started</h2>
<h3>Prerequisites</h3>
<h2>API reference</h2>
</main>As the user scrolls, the items corresponding to visible headings receive the marked class and the host gains the :state(scrolling) custom state.
Custom query
Override which elements are observed by setting the query attribute or property:
<!-- Only observe h2 and h3 -->
<pap-table-of-content query="h2, h3"></pap-table-of-content>Icons
Add a data-icon attribute to a heading to render a pap-icon beside its label in the tree:
<h2 data-icon="star">Highlights</h2>Reading the active set
const toc = document.querySelector("pap-table-of-content");
toc.addEventListener("change", () => {
console.log(toc.value); // [{ href: '#...', name: '...' }, ...]
});Properties
| Property | Attribute | Type | Default | Description |
| -------- | --------- | ---------------------------------- | --------------------- | --------------------------------------------------------------------- |
| query | query | string | All heading selectors | CSS selector used to discover headings. Reflected from the attribute. |
| links | — | Link[] | [] | Internal list of discovered links; not reflected. |
| value | — | { href: string; name: string }[] | — | Read-only. Returns the subset of links currently marked as visible. |
Events
| Event | Detail | Description |
| -------- | ------ | ---------------------------------------------------------------------------------------- |
| change | — | Fired whenever the set of visible headings changes (throttled via IntersectionObserver). |
CSS Parts
| Part | Element | Description |
| -------- | -------------------- | --------------------------------- |
| tree | pap-treeview | The root tree element. |
| item | pap-treeitem | Each individual tree item. |
| anchor | <a> inside an item | The anchor link inside each item. |
Custom States
| State | Description |
| ------------------- | --------------------------------------------------------------------------- |
| :state(scrolling) | Present while the page is scrolling; removed ~500 ms after scrolling stops. |
| :state(active) | Present while the inner pap-treeview is focused / active. |
License
Licensed under the @Papit License 1.0 — Copyright (c) 2024 Henry Pap (@onkelhoy)
Key points:
- ✅ Free to use in commercial projects
- ✅ Free to modify and distribute
- ✅ Attribution required
- ❌ Cannot resell the component itself as a standalone product
See the LICENSE file for full details.
Related packages
- @papit/web-component — Core utilities, decorators, and base component class
- @papit/treeview — Tree view and tree item components used for rendering
