Navigation

Table of contents

An "On this page" list of the headings of a part of the page, as links indented by level. The link of the section being read is highlighted as the reader scrolls, and clicking a link scrolls to its heading.

The list is built in the browser from the headings already on the page, so the tag takes no items: it reads target.

Install

Requirements

Usage

Blade

Lists the h2 and h3 with an id inside <main>, the h3 one step in. Headings without an id are left out. Positioning is up to your classes: sticky keeps the list in view while the page scrolls.

Another area and other levels #

target is a CSS selector for the area to read, and levels the heading levels to list. When the headings scroll inside a box, the toc follows the scroll of that box, and clicking a link scrolls the box only.

Install

Requirements

Usage

Blade

data-toc-label on a heading replaces its text in the list; data-toc-skip leaves it out. Without data-toc-label, the link shows the heading's text without the heading's link to itself (the # of a tk:heading anchor, a markdown permalink).

Version 3.0: the big rewrite

Unreleased notes

Blade

Under a sticky header #

The current section is the last heading that reached the top of the scrolling area, below the heading's scroll-margin-top and the scroller's scroll-padding-top. Give the headings (or the page, with scroll-pt-20 on <html>) the height of a sticky header: the browser uses the same values when it scrolls to the heading, so the link turns current exactly when its heading stops under the header.

Handbook

Values

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Hiring

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Remote work

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Time off

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Blade

Label #

label replaces the "On this page" heading above the list; false hides it, and the <nav> keeps "On this page" as its accessible name.

Blade

Size #

size scales the text like tk:nav.

Ingredients

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Preparation

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Serving

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Blade

Changed event #

changed fires on the toc with the id of the new current heading, when the reader scrolls or clicks a link.

Data we collect

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

How we use it

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Your rights

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Reading:

Blade

Livewire updates #

The list is rebuilt when the target changes, after a Livewire update or wire:navigate. Without headings the toc renders nothing: remove every section to see it go.

Overview

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Setup

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Deploy

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Blade

Customization #

Classes go to the <nav> (make it sticky there); heading:, list: and item: reach the label, the list and each link. Each link has data-level and, when current, data-current.

Authentication

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Tokens

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Errors

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Rate limits

Every section of this guide holds a few paragraphs of text, so the box scrolls and the list beside it follows the section you are reading.

Blade

Reference

tk:toc

tk:toc

Prop Type Default Description
target string main CSS selector of the area whose headings are listed. When nothing matches, the whole document is read.
levels string | array 2,3 The heading levels to list: "2,3", "h2,h3" or [2, 3], in any order. Values outside 1 to 6 are ignored. The lowest level is not indented; each next level is one step in.
label string | slot | bool "On this page" The text above the list, translated. false hides it.
size string md xs, sm, md, lg, xl, 2xl or 3xl. Sets the font size and spacing, like tk:nav.

On the headings of the target:

Attribute Description
id Required: a heading without one is not listed. The link points to #id.
data-toc-label The text of the link, in place of the heading's text. Without it, the text is the heading's, minus a link inside it to the heading itself (<a href="#id">).
data-toc-skip Leaves the heading out of the list. On an element around headings, inside the area read, it leaves them all out: a tk:preview has it, so the headings of an example stay out of the page's toc. A toc reading inside that element still lists them.

Slots

Slot Description
label The content above the list, in place of the label prop.

Events

Event Detail When
changed { id } The current section changed, by scrolling or by a click on a link. id is the heading's id, or null when none of the listed headings is left visible (removed or hidden). It also fires once when the toc first finds headings, and an x-on:changed on the tag receives that one too.

Prefixes

Prefix Goes to
heading: The label above the list.
list: The <ul> of the links.
item: Each link.

Events

Dispatched on the toc's own element; it does not bubble.

With Alpine:

blade

Calling a Livewire action:

blade

Customization

Classes and attributes on the tag go to the <nav>. To reach the parts, prefix the attribute (listed in the Reference above).

Each link has data-level (the heading level, 2 for an h2) and, when current, data-current, for styles of your own. Its indentation is padding-inline-start: calc(var(--tallkit-toc-depth) * 1em), where --tallkit-toc-depth is 0 for the first level, 1 for the next...

blade

Accessibility

A <nav> landmark named by its label (aria-labelledby), or by "On this page" (translated) when the label is hidden. The links are in a list, so screen readers announce how many there are. The current link has aria-current="location".

Clicking a link (or pressing Enter on it) scrolls to the heading and moves the focus there, as a native anchor link does, so the next Tab continues from that section: a heading with no tabindex gets tabindex="-1" for that. With prefers-reduced-motion the scroll is instant instead of smooth.

Key Action
Tab Moves through the links.
Enter Scrolls to the heading and moves the focus to it.

Behavior notes

  • Until the script finds headings the toc is hidden, and it stays hidden (the hidden attribute, no empty box) when the target has none.
  • The list is rebuilt when the target changes: a Livewire update, an Alpine template, a script adding or renaming headings (their id, data-toc-label and data-toc-skip are watched), and after wire:navigate. A toc inside a persisted element reads the new page's target.
  • Headings inside the toc itself are never listed. Hidden headings (display: none, a closed tab) are listed but never become the current one.
  • When the scroll reaches the end, the last heading in view becomes current: the last sections may be too short for their heading to reach the top.
  • A click updates the URL hash with history.replaceState, without a new history entry, so Back still leaves the page (a new entry would make Livewire restore the page on Back). Ctrl, Cmd, Shift, Alt or a middle click do what the browser does with a link.
  • After a click the clicked link stays current until the scroll ends, even when its heading cannot reach the top.
  • When the headings scroll inside a box (an element with overflow-y: auto or scroll that overflows), the click scrolls that box only, not the page around it.