npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@n8tb1t/use-scroll-position

v4.0.0

Published

React hook to calculate scroll position

Readme

use-scroll-position

NPM Version Downloads License

Screenshot

use-scroll-position is a lightweight, tree-shakable React hook library for detecting and tracking scroll position — either of the window or a specific element. Designed for performance-sensitive use cases.

⚡️ Version 4.0.0 is a complete rewrite of the library.
For older versions, see the legacy docs.


⚡️ Quickstart

Track the global window scroll position in just a few lines:

import { useWindowScrollPosition } from '@n8tb1t/use-scroll-position'

useWindowScrollPosition(({ currPos }) => {
  console.log('Current scroll position:', currPos.y)
})

📖 Documentation

👉 Full Docs Site


✨ Features

  • ✅ React 19 support
  • ✅ SSR-ready
  • ✅ LLM-friendly docs
  • ✅ Native TypeScript

📦 Installation

Using NPM:

npm install @n8tb1t/use-scroll-position

Using PNPM:

pnpm add @n8tb1t/use-scroll-position

🚀 Usage

Track window scroll position

useWindowScrollPosition(({ prevPos, currPos, top, bottom }) => {
  console.log(prevPos, currPos, top, bottom)
})
useBodyScrollPosition(({ prevPos, currPos, top, bottom }) => {
  console.log(prevPos, currPos, top, bottom)
})

Track element position in the viewport (window)

const setElementRef = useWindowScrollPosition(({ prevPos, currPos, top, bottom }) => {
  console.log(prevPos, currPos, top, bottom)
})

return <div ref={setElementRef} />
const setElementRef = useBodyScrollPosition(({ prevPos, currPos, top, bottom }) => {
  console.log(prevPos, currPos, top, bottom)
})

return <div ref={setElementRef} />

Track scroll position in an overflow container

const [setRootRef] = useOverflowScrollPosition(({ prevPos, currPos, top, bottom }) => {
  console.log(prevPos, currPos, top, bottom) // root container scroll
})

return (
  <div ref={setRootRef} style={{ height: '100px', width: '300px', overflow: 'auto' }} />
)

Track element scroll position inside an overflow container

const [setRootRef, setTargetRef] = useOverflowScrollPosition(({ prevPos, currPos, top, bottom }) => {
  console.log(prevPos, currPos, top, bottom) // target element scroll
})

return (
  <div ref={setRootRef} style={{ height: '100px', width: '300px', overflow: 'auto' }}>
    <div ref={setTargetRef} />
  </div>
)

💡 Use Cases

Here are some common ways developers use use-scroll-position:

  • Sticky Headers & Navbars – hide, reveal, or shrink navigation bars depending on scroll direction.
  • Infinite Scrolling – detect when the user reaches the bottom of a container or the window to load more data.
  • Scroll-Based Animations – trigger animations or transitions as elements enter the viewport.
  • Active Section Highlighting – update navigation menus (scrollspy effect) as the user scrolls through sections.
  • Custom Parallax Effects – adjust element positions or backgrounds smoothly based on scroll position.
  • Performance Monitoring – track scroll behavior for analytics or user experience optimization.

🤝 Contributing

See the Contributing Rules.