Skip to content
WunderUI

useScrollSpy

Free

Tells which section of a page or a scroll container is being read.

import { useScrollSpy } from "@wunderui/react"

Playground

Overview

Plan, seats and the next invoice date of the workspace.

Billing

Payment method, billing address and the invoices of the last twelve months.

Members

Who has access, their roles and the invitations still open.

Installation

useScrollSpy is part of WunderUI Free, in the npm package and in the free source. Import it from :

tsx
import { useScrollSpy } from "@wunderui/react"

When to use

Use useScrollSpy for

  • Marking the current section in your own table of contents, tab list or sidebar.
  • A settings page whose sections scroll inside a panel, with set to that panel.
  • Any page where the reader should always see where they are.

Use something else for

  • FloatingToc for a ready-made table of contents with a gliding marker. It uses this hook when you leave out .
  • Plain anchor links for a short page where nothing needs to be marked as current.
  • An IntersectionObserver for loading or animating something once it comes into view. The hook answers which section is being read, not whether an element is visible.

Signature

Pass the ids of the section headings. The hook returns the id of the last section whose top has passed the reading line, a quarter down the window or the container, and a setter to move the marker yourself. At the end of the scroll the last section wins, so a short closing section still becomes active.

ts
useScrollSpy(ids: string[], root?: React.RefObject<HTMLElement | null> | undefined, enabled?: boolean): readonly [string | undefined, React.Dispatch<React.SetStateAction<string | undefined>>]

Parameters

ParameterTypeDefaultDescription
idsstring[]—The ids of the section elements, in the order they appear on the page. An id without an element in the page is ignored.
root optionalReact.RefObject<HTMLElement | null> | undefined—A ref to the element that scrolls the sections. Leave it out when the page itself scrolls. The sections are looked up inside this element, and the reading line is a quarter of its height from its top.
enabled optionalbooleantrueSet it to to stop listening, for example while the list is hidden. The marker keeps its last value.

Returns

A tuple, like : the id being read and a setter for it.

FieldTypeDescription
activestring | undefinedThe id of the section being read. Before the first measurement it is the first id.
setActiveReact.Dispatch<React.SetStateAction<string | undefined>>Moves the marker yourself, usually in the click handler of a list item. The next scroll measures again and takes over.

Behavior and motion

The hook measures on scroll and tells you one id.

State
The reading line sits a quarter down the container, or the window. The active section is the last one whose top has passed it. When the container is scrolled to its very end, the last section wins, so a short closing section can become active even though its top never reaches the line. The ids must be listed in page order.
When it measures
On every scroll event of the container (or the window) and on window resize, once at mount, and again when the list of ids, or changes. The scroll listener is passive and each measurement reads the top of each section once.
Elements and ids
Sections are found by id inside , or in the document without it. A missing element is skipped, so a list that includes a section which renders later still works once it appears and the page has scrolled or resized.
Server rendering
Nothing touches or during render. The first value is the first id on the server and on the first client render, and the listeners start in an effect, so there is no hydration mismatch.
Cleanup
The listeners are removed when the component unmounts, when the ids or change and when becomes . A disabled hook keeps the last id and does not reset it.
Motion
The hook itself does not animate. Scrolling to a section on click is yours; make it smooth normally and instant under reduced motion. Animate the marker with and only.
Reduced motion
Check in the click handler and scroll with when it is set, as the Playground does. The hook reports the same ids either way.

Best practices

  • Do

    List the ids in the order the sections appear on the page. The hook stops at the first section below the reading line.

  • Don’t

    Do not use it to lazy-load or to start animations. It reports the section being read, not what is visible.

  • Do

    Call the setter in the click handler, so the marker moves at once instead of after the smooth scroll has arrived.

  • Don’t

    Do not leave a list hidden in a closed drawer listening. Pass so nothing measures while nobody sees the result.

  • Do

    Mark the active item with and give a scroll container and a label, so keyboard users can reach and scroll it.

  • Don’t

    Do not pass a new ref on every render. Create it once with , or the hook restarts its listeners each time.

Notes for AI

Give your coding assistant the page or the MCP call instead of screenshots.

MCPget_component("useScrollSpy")
Importimport { useScrollSpy } from "@wunderui/react"

The full library index for assistants is at /llms.txt.