Skip to main content
Copy for LLM

On-Page TOC

The canonical wiring for an in-page TOC alongside long-form content — articles, docs pages, change logs, anything with five or more headings. WireKit ships two primitives that map onto the two common shapes:

  • Sidebar TOC (<x-wirekit::reading-spine>) — right-edge sticky vertical sidebar, hover-to-expand, narrow at rest, the shape many documentation sites use. Recommended for docs / articles.
  • Horizontal strip TOC (<x-wirekit::reading-toc>) — sticky strip across the top of the viewport. Right shape for flat marketing pages with 3-4 anchored sections. See the marketing-landing-toc recipe.

This recipe covers the sidebar pattern.

The canonical wiring

Three primitives compose:

  1. <x-wirekit::reading-progress> — the top-edge progress bar (full-page, not scoped to article). Optional; drop if your layout doesn't want one.
  2. <x-wirekit::reading-spine> — the right-edge sidebar TOC. Reads <main> headings at the H2 / H3 levels. expand="hover" keeps the spine narrow at rest; expands to show labels on hover.
  3. <main> content — the spine reads headings from this element. Use target="main" (the prop default null resolves to 'main, article' first-match wins).

Each example below wraps the page in <x-wirekit::container> and the article in <x-wirekit::prose> for its typography, and gives the article the wk-reading-content class, which keeps it clear of the spine from md up. The code under each preview is what the preview renders.

On-page TOC with sticky sidebar spine

CSS variables

The reading-spine reads these CSS variables; override in the wrapping element's style="…" or in :root {} for a global preset:

Variable Default Purpose
--reading-spine-width-collapsed 1.5rem Narrow-at-rest width (visible as a tick column)
--reading-spine-width-expanded 16rem Expanded width on hover / expand="always"
--reading-spine-offset-top 1rem boundary="container" only — pins the spine below the scroll-container top. Not used in this viewport-pinned recipe (the spine is vertically centered).
--reading-spine-padding-y 0.875rem Vertical padding inside the spine column
--reading-spine-color-active var(--color-wk-text) Active-link color

For the full token reference, see the reading spine's tokens.

Integration with a sticky header

If your layout has a sticky header (e.g. <x-wirekit::brand-bar sticky>), keep the spine's active-link line below it with the :offset prop. The viewport-pinned spine is vertically centered, so it never sits behind the bar — but the active link is the last heading at or above a line :offset below the top of the page (6rem unless you set one). A bar taller than that hides the line, and a section highlights only after its heading has scrolled up under the bar. Give :offset at least the bar's height.

On-page TOC under a sticky brand-bar

Composing with a breadcrumb

If your layout also carries a <x-wirekit::breadcrumb>, the breadcrumb's active node and the spine's active link should reference the same logical position — both reflect "where the reader is". The breadcrumb is page-level (which page in the docs tree?); the spine is in-page (which section on the current page?).

The breadcrumb stays static for the whole page; the spine's active link updates as the reader scrolls.

On-page TOC with breadcrumb above the article

The rail beside the article

When the page has a column to spare, layout="rail" puts the outline into it, open at all times: every title on a line that bends with the indent, and a band on that line at the section the reader is in. It stands in your grid, sticks to the top of its column from md and scrolls on its own when the outline is taller than the window. The reading page shows it live, with its code.

Three things make the layout:

  1. A grid with two columns from md: the article, and a 14.375rem column for the rail, such as <x-wirekit::grid cols="1" template="md:[minmax(0,1fr)_14.375rem]">.
  2. The rail first in the markup, placed with md:col-start-2 md:row-start-1, so a phone that shows it (hide-below="none") reads the outline before the article.
  3. The article in the first column with md:col-start-1 md:row-start-1, and without wk-reading-content: that class reserves a gutter for the default spine, which the rail does not need.

Under a sticky header, set --reading-spine-offset-top to the header's height, so the rail sticks below it, and pass the same height as offset, so a section counts as current once its heading comes out from under the header.

Tips

  • Use expand="always" on dedicated docs layouts (where readers expect the TOC) instead of the default expand="hover" — saves a hover-discovery moment.
  • Mobile users see no spine by default. The narrow-viewport experience drops the TOC; readers scroll the article without it. The progress bar at the top still works on mobile.
  • For flat content (3-4 sections, all <h2>), prefer the horizontal <x-wirekit::reading-toc> strip — see the marketing-landing-toc recipe.
  • For very long articles (50+ headings), the spine works but readers benefit from the <x-wirekit::reading-minimap> variant — a reflection of the article's structure as a vertical ruler.

See also