useScrollSpy
Tells which section of a page or a scroll container is being read.
import { useScrollSpy } from "@wunderui/react"Playground
Installation
useScrollSpy is part of WunderUI Free, in the npm package and in the free source. Import it from :
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.
useScrollSpy(ids: string[], root?: React.RefObject<HTMLElement | null> | undefined, enabled?: boolean): readonly [string | undefined, React.Dispatch<React.SetStateAction<string | undefined>>]Parameters
Returns
A tuple, like : the id being read and a setter for it.
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.
get_component("useScrollSpy")import { useScrollSpy } from "@wunderui/react"The full library index for assistants is at /llms.txt.