npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@auroratide/table-of-contents

v0.1.0

Published

Web component for auto-generating a table of contents

Downloads

4

Readme

The table-of-contents Element

The table-of-contents element represents a navigable list of headings for some content. This component is able to automatically generate links needed to navigate to all the different headings in an article.

<table-of-contents
	for="main-content"
	aria-label="Table of Contents"
></table-of-contents>

<section id="main-content">
	<h2 id="one">One</h2>
	<h3 id="one-one">One One</h3>
	<h3 id="one-two">One Two</h3>
	<h2 id="two">Two</h2>
</section>

Installation

You can import through CDN:

<script type="module" src="https://unpkg.com/@auroratide/table-of-contents/lib/define.js"></script>

Or, you may install through NPM and include it as part of your build process:

$ npm i @auroratide/table-of-contents
import '@auroratide/table-of-contents/lib/define.js'

Usage

table-of-contents is a markup element that you can use in your HTML document.

The for attribute can be set to the id of an element on the page. If you supply this, the table-of-contents will automatically generate a list of links to headings in the chosen element.

For this, your content must conform to the following:

  • Headings in your content are defined with the h1 through h6 tags.
  • Headings do not skip levels (this is good accessibility practice anyway).
  • Each heading has an id defined that uniquely identifies it.
  • The first heading can be of any level, as long as no heading afterward is higher than it.
    • For instance, if your content starts with an h2, there should not be an h1 later in the document.

The following is a example of what a good heading structure looks like:

<section id="main-content">
	<h2 id="one">One</h2>
	<h3 id="one-one">One One</h3>
	<h3 id="one-two">One Two</h3>
	<h4 id="one-two-one">One Two One</h4>
	<h2 id="two">Two</h2>
	<h3 id="two-one">Two One</h3>
	<h4 id="two-one-one">Two One One</h4>
	<h3 id="two-two">Two Two</h3>
</section>

Labelling the Table of Contents

The table of contents represents a navigation landmark. Therefore, it is good accessibility practice to label it.

This can be done with aria-label.

<table-of-contents
	for="main-content"
	aria-label="Table of Contents"
></table-of-contents>

Manually Rebuilding the List

The table-of-contents element does not automatically react to changes in your document structure.

Instead, the element provides a Javascript build() method so that you may define your own reaction logic, should it be needed. The build() method regenerates the list of links for the identified section.

const toc = document.querySelector("table-of-contents")

toc.build()

Fallback List

Children of the table-of-contents may serve as a fallback in case Javascript is disabled, fails, or takes a long time to execute. This is optional, but enhances the accessibility of your page.

Once the list generation occurs, the visible content is replaced with the new list. The fallback list will continue to exist in the DOM.

<table-of-contents for="main-content" aria-label="Contents">
	<ol>
		<li><a href="#one">One</a></li>
		<li><a href="#two">Two</a></li>
	</ol>
</table-of-contents>

CSS Customization

Since these are native custom elements, they can be styled the same way as regular HTML elements.

The anchor part exposes the links generated by the component.

table-of-contents::part(anchor) {
	color: red;
}

Accessibility

This custom element is built with accessibility in mind! A table of contents is a navigational landmark, and therefore the table-of-contents element gets granted the navigation role.

The best practice is to label your table of contents so readers know the content it is for. You may use aria-label or aria-labelledby for this.