GooseUI

Components

ScrollSpy

New

A floating navigation for long pages. It appears after the page is scrolled a bit, shows one icon per section and highlights the section in view. Hover or focus the panel and it expands: labels slide out and the active section gets a pulsing dot. The active section comes from an IntersectionObserver (the useScrollSpy hook). All animations are CSS transitions, and the items are plain anchor links.

Browser Support

interpolate-size

Labels grow from zero width to their natural width. Without it they appear without the slide.

Your browser:
:focus-within

Keyboard focus expands the panel like hover does.

Your browser:

Preview

Scroll the window and hover the panel. The page itself does not move.

Home

A studio that builds interfaces from small, honest parts.

Scroll the window and watch the panel on the left follow the section that crosses the activation line. Hover the panel to expand every label.

Services

Design systems, landing pages and product dashboards.

Scroll the window and watch the panel on the left follow the section that crosses the activation line. Hover the panel to expand every label.

Work

Selected projects for banks, retailers and open source.

Scroll the window and watch the panel on the left follow the section that crosses the activation line. Hover the panel to expand every label.

Blog

Notes on CSS, accessibility and shipping less JavaScript.

Scroll the window and watch the panel on the left follow the section that crosses the activation line. Hover the panel to expand every label.

Contact

Write to us and we answer within one working day.

Scroll the window and watch the panel on the left follow the section that crosses the activation line. Hover the panel to expand every label.

Installation

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

Usage

import { Briefcase, Home, Mail } from "lucide-react"
import { ScrollSpy } from "@/components/ui/scroll-spy"

const sections = [
  { id: "home", label: "Home", icon: <Home /> },
  { id: "work", label: "Work", icon: <Briefcase /> },
  { id: "contact", label: "Contact", icon: <Mail /> },
]

export default function Page() {
  return (
    <>
      <section id="home">…</section>
      <section id="work">…</section>
      <section id="contact">…</section>
      <ScrollSpy items={sections} />
    </>
  )
}

Examples

Whole page

Without root the component listens to the window and is fixed to the viewport. Put it once in the layout, next to sections that have ids. Hide it on narrow screens with a class.

// app/layout.tsx, next to the page content
<ScrollSpy
  items={sections}
  showAfter={200}
  offset={200}
  className="hidden lg:block"
/>

Right side, always visible

side="right" moves the panel to the right edge. With showAfter={0} it is visible from the start.

Home

A studio that builds interfaces from small, honest parts.

Services

Design systems, landing pages and product dashboards.

Work

Selected projects for banks, retailers and open source.

Blog

Notes on CSS, accessibility and shipping less JavaScript.

Contact

Write to us and we answer within one working day.

<ScrollSpy items={sections} side="right" showAfter={0} />

Inside a scroll container

Pass root and fixed={false} to track a scrollable block instead of the window. The parent needs position: relative.

const ref = useRef<HTMLDivElement>(null)

<div className="relative">
  <div ref={ref} className="h-96 overflow-y-auto">…</div>
  <ScrollSpy items={sections} root={ref} fixed={false} showAfter={60} />
</div>

Props

PropTypeDefaultDescription
itemsScrollSpyItem[]-Sections: id, label and icon
showAfternumber200Scroll distance in px after which the panel appears
side"left" | "right""left"Which edge the panel sticks to
fixedbooleantrueposition: fixed. false places it inside a relative parent
rootHTMLElement | RefObjectwindowScroll container. The window by default
offsetnumber200Activation line and scroll offset in px from the top of the root
labels{ nav?: string }-Texts: nav (aria-label of the navigation)
onNavigate(id: string) => void-Called with the id after a click
classNamestring-Classes for the panel wrapper, for example hidden lg:block

Features

  • Expands on hover: icons only at rest, labels and the active dot show up when the pointer or focus is inside
  • Accessible: nav with aria-label, aria-current on the active link, full keyboard support
  • Shared hook: section tracking comes from useScrollSpy
  • Reduced motion: transitions and the pulsing dot are turned off