Nhatcapdang
Scroll

Scrollspy

A lightweight scrollspy container that syncs active anchors with section positions and URL hash updates.

Usage

Horizontal Usage

Installation

npx shadcn@latest add @nhatcapdang/scrollspy
yarn dlx shadcn@latest add @nhatcapdang/scrollspy
pnpm dlx shadcn@latest add @nhatcapdang/scrollspy

Props

PropTypeDefaultDescription
childrenReactNode-Navigation anchor elements that include data-scrollspy-anchor
targetRefRefObject<HTMLElement | HTMLDivElement | Document | null | undefined>-Scroll container reference to observe
onUpdate(id: string) => void-Callback fired when active section changes
offsetnumber0Default top offset used when calculating active sections
smoothbooleantrueEnables smooth scroll behavior for anchor clicks
classNamestring-Additional classes for the wrapper
dataAttributestring'scrollspy'Custom data attribute namespace for anchors and offsets
historybooleantrueToggles URL hash updates via history.replaceState
throttleTimenumber-Reserved prop for throttling support

Features

  • Active state syncing: Applies data-active="true" on the currently matched anchor
  • Hash-aware navigation: Supports initial hash navigation and optional history updates
  • Container flexibility: Works with custom scrollable containers and viewport wrappers
  • Offset controls: Supports global offset and per-anchor data-scrollspy-offset

On this page