GooseUI

Components

useScrollSpy

New

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.

IntroductionSetupConfigurationDeploymentFAQ

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

npx shadcn@latest add @gooseui/use-scroll-spy
pnpm dlx shadcn@latest add @gooseui/use-scroll-spy
yarn dlx shadcn@latest add @gooseui/use-scroll-spy
bunx --bun shadcn@latest add @gooseui/use-scroll-spy

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.

IntroductionSetupConfigurationDeploymentFAQ

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.

active: null
Section 1Section 2

Section 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

NameTypeDefaultDescription
idsreadonly string[]-Section ids in document order
options.rootHTMLElement | RefObjectwindowScroll container: element or ref. The window by default
options.offsetnumber | string ("40%")0Activation line from the top of the root: px or percent
options.rootMarginstring-Custom rootMargin for the observer. By default a 1px band at offset
options.bottomThresholdnumber4Distance to the bottom (px) at which the last section becomes active
options.fallbackToFirstbooleantrueReturn the first id while no section has reached the line
options.watchDombooleantrueRe-scan when sections are added or removed
options.enabledbooleantrueTurn 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