|Kit

useScrollSpy

Tracks which heading of a page is being read, and scrolls to one below a sticky header — the engine behind TableOfContents.

useScrollSpy reads the headings of a page and tracks the one being read as it scrolls. TableOfContents is built on it. Reach for the hook when the table of contents is your own markup, or when something else should follow the reading position: a progress label, a header that shows the current section.

import { useScrollSpy } from '@42-central/ui-react';

const spy = useScrollSpy({ container: '#subject', selector: 'h2, h3', offset: 64 });

spy.data.map((heading) => (
  <a
    key={heading.id}
    href={`#${heading.id}`}
    aria-current={heading.id === spy.active ? 'location' : undefined}
    onClick={(event) => {
      if (spy.scrollTo(heading.id)) event.preventDefault();
    }}
  >
    {heading.label}
  </a>
));

Usage

Scroll the box, or use the button, which calls scrollTo.

Reading: nothing yet

Introduction

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Common instructions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Norm

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Makefile

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Mandatory part

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Libc functions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Additional functions

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Bonus part

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Submission and peer-evaluation

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Write each function in its own file, keep it under 25 lines, and protect every allocation. Your work is graded by your peers first, then by the Moulinette, so read every line of this section before you start.

Code

const root = useRef(null);
const article = useRef(null);
const spy = useScrollSpy({ container: article, root, selector: 'h2, h3', offset: 16 });
const active = spy.data.find((heading) => heading.id === spy.active);

<p>
  Reading: {active ? active.label : 'nothing yet'}
  <button onClick={() => spy.scrollTo('bonus-part')}>Jump to the bonus</button>
</p>
<div ref={root} className="h-80 overflow-auto">
  <article ref={article}>…</article>
</div>

Which heading is active

A heading becomes active once its top reaches offset, the height of whatever is stuck to the top of the scroller, and stays active until the next heading gets there. Before the first one reaches it, the first heading is active.

At the bottom of the page, the last sections are often too short for their headings to reach the line. So once the scroll can't go further, the last heading still in view wins, and the last entry of a table of contents can light up.

Which sections are in view

visible lists the id of every section in view, in order: from the one being read to the last heading above the bottom of the scroller. A section counts from its heading to the next one, so the section being read is always in the list, even when its heading has scrolled away. TableOfContents' track lights this range, and keeps aria-current for active alone.

Scrolling to a heading

scrollTo(id) scrolls so the heading sits just below offset, and returns false when the heading isn't on the page. It scrolls by the distance between the heading and that line (scrollBy), never to an absolute scrollTop. So the same call works for the window and any nested scroller, and in either writing direction.

While its scroll runs, the target stays active, while visible follows the scroll. The headings it passes don't flash by, and it hands back to the reading position once the scroll has been still for a moment. It scrolls smoothly, or instantly when the reader prefers reduced motion; pass { behavior } to choose.

scrollTo doesn't touch the URL. TableOfContents pushes the #id itself, after a click.

Headings you already know

Pass data to watch those headings instead of reading the page. They are returned as data straight away, so a server render already has them. A missing label or depth is filled from the heading after mount.

const spy = useScrollSpy({
  data: [
    { id: 'introduction', label: 'Introduction', depth: 2 },
    { id: 'mandatory-part', label: 'Mandatory part', depth: 2 },
  ],
});

When the content changes

The headings are read after mount, and again when container, selector or data change. If the content under a fixed container changes later, for example after a fetch, call reinitialize().

A heading without an id is skipped, since nothing could link to it, with a warning in development.

API

Prop

Type

Returns:

Prop

Type

active is null on the server and on the first client render, then the real heading right after mount, like the other hooks that read the page. See SSR-safe first render.

On this page