Skip to content
Focsd homeFocsd homeFocsd
Forms

Table of contents

A list of links to the headings on the page that marks the section you are reading.

  • Tag: focsd-table-of-contents
  • Pattern: Nav of links with aria-current="location"
  • Category: Navigation

Example

Install

Add the package to your project.

With npm

Run npm install @focsd-dev/components.

Usage

Write the elements in your HTML.

HTML
<focsd-table-of-contents for="#guide"></focsd-table-of-contents>
<article id="guide">
<h2>Install</h2>
<p>Add the package to your project.</p>
<h3>With npm</h3>
<p>Run npm install @focsd-dev/components.</p>
<h2>Usage</h2>
<p>Write the elements in your HTML.</p>
</article>

Usage

Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.

<script type="module">
import '@focsd-dev/components/focsd-table-of-contents.js';
</script>
<focsd-table-of-contents for="#guide"></focsd-table-of-contents>

API

Attributes

AttributePropertyTypeDefault
forforstringmain, focsd-app-shell
levelslevelsstring2,3
labellabelstring—

Slots

SlotDescription
(default)Optional links (a[href^="#"]) that replace the generated list; their targets are tracked.

CSS parts

Style the inside of the component with ::part(), for example focsd-table-of-contents::part(nav).

PartDescription
navThe navigation landmark.
listEach list of links; a nested list is inside the item of its parent heading.
linkEach generated link.

Accessibility

  • It renders a navigation landmark named "On this page"; set label when a page has more than one.
  • It lists the h2 and h3 headings of the first element matching for (by default main, or focsd-app-shell). Set levels to choose others, such as levels="2". An h3 is nested in a list under the h2 before it.
  • A heading without an id gets one made from its text, so its link and the address bar point at it.
  • The link of the section in view has aria-current="location", which screen readers read as "current location".
  • Following a link scrolls to the heading (without animation when reduced motion is preferred), moves focus to it and adds its #id to the address, so Back returns to where you were.
  • Put your own links (a[href^="#"]) inside to replace the generated list; the current section is still marked on them.

Keyboard

KeyAction
TabMove between links
EnterGo to the heading and focus it