useScrollSpy takes a list of section ids and returns the one under the activation line. It runs on IntersectionObserver, works with the window or any scroll container, makes the last section active at the very bottom and picks up sections that mount later.
Preview
Scroll inside the box.
Introduction
Scroll this box. The highlighted pill above follows the section whose top has crossed the activation line.
Setup
Scroll this box. The highlighted pill above follows the section whose top has crossed the activation line.
Configuration
Scroll this box. The highlighted pill above follows the section whose top has crossed the activation line.
Deployment
Scroll this box. The highlighted pill above follows the section whose top has crossed the activation line.
FAQ
Scroll this box. The highlighted pill above follows the section whose top has crossed the activation line.
Installation
Usage
import { useScrollSpy } from "@/hooks/use-scroll-spy"
const sections = ["intro", "setup", "faq"]
export function Nav() {
const active = useScrollSpy(sections, { offset: 80 })
return (
<nav>
{sections.map((id) => (
<a key={id} href={`#${id}`} aria-current={active === id ? "location" : undefined}>
{id}
</a>
))}
</nav>
)
}Examples
Activation line in percent
offset accepts pixels or a percentage of the root height. Here a section becomes active when its top passes 40% of the box.
Introduction
Setup
Configuration
Deployment
FAQ
const ref = useRef<HTMLDivElement>(null)
const active = useScrollSpy(ids, { root: ref, offset: "40%" })
<div ref={ref} className="h-72 overflow-y-auto">…</div>Sections that appear later
The hook watches the DOM, so sections added after the first render are tracked without extra code.
nullSection 1
Section 2
const ids = sections.map((s) => s.id)
const active = useScrollSpy(ids, { root: ref, offset: 24 })Window and sticky header
Without root the hook listens to the window. Set offset to the header height so a section counts as active right below it.
// Header is 64px tall
const active = useScrollSpy(["pricing", "features", "faq"], {
offset: 64,
fallbackToFirst: false,
})Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| ids | readonly string[] | - | Section ids in document order |
| options.root | HTMLElement | RefObject | window | Scroll container: element or ref. The window by default |
| options.offset | number | string ("40%") | 0 | Activation line from the top of the root: px or percent |
| options.rootMargin | string | - | Custom rootMargin for the observer. By default a 1px band at offset |
| options.bottomThreshold | number | 4 | Distance to the bottom (px) at which the last section becomes active |
| options.fallbackToFirst | boolean | true | Return the first id while no section has reached the line |
| options.watchDom | boolean | true | Re-scan when sections are added or removed |
| options.enabled | boolean | true | Turn tracking off without unmounting |
Returns
string | null: the id of the active section. null before mount, when no section is found, or when fallbackToFirst is false and nothing has reached the line yet.
Features
- IntersectionObserver: plus a light scroll handler for fast jumps with Home and End
- Bottom of the page: the last section is activated even if it is too short to reach the line
- Dynamic content: a MutationObserver re-attaches the observer when sections change
- Cleanup: observers, listeners and animation frames are released on unmount