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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@crucai/use-scroll-tracker

v1.0.8

Published

A high-performance React hook for tracking scroll position and element visibility in the viewport, enabling sophisticated scroll-based animations and interactions.

Readme

useScrollTracker

A high-performance React hook for tracking scroll position and element visibility in the viewport, enabling sophisticated scroll-based animations and interactions.

Features

  • Visibility Tracking: Precise calculations of how much of an element is visible in the viewport
  • Position Tracking: Monitor element position relative to viewport top, center, and bottom
  • Threshold Detection: Get notified when element crosses specific visibility thresholds
  • Scroll Direction: Track whether user is scrolling up or down
  • Scroll Physics: Access velocity, acceleration, and inertia metrics
  • Entry/Exit Tracking: Detect how and when elements enter or exit the viewport
  • Performance Optimized: Uses IntersectionObserver, ResizeObserver and passive event listeners
  • Zero Dependencies: Lightweight and focused implementation

Installation

npm install @crucai/use-scroll-tracker
# or
yarn add @crucai/use-scroll-tracker

Basic Usage

import React from 'react';
import { useScrollTracker } from '@crucai/use-scroll-tracker';

function MyComponent() {
  const containerRef = React.useRef(null);
  const metrics = useScrollTracker(containerRef);
  
  return (
    <div 
      ref={containerRef}
      style={{
        opacity: metrics.percentVisible,
        transform: `translateY(${(1 - metrics.percentVisible) * 20}px)`
      }}
    >
      <p>Visibility: {Math.round(metrics.percentVisible * 100)}%</p>
      <p>Position: {metrics.positionRelativeToViewport}</p>
    </div>
  );
}

Component API

The package also provides a component version using render props for a declarative approach:

import { ScrollTracker } from '@crucai/use-scroll-tracker';

function MyComponent() {
  return (
    <ScrollTracker>
      {(metrics, ref) => (
        <div 
          ref={ref}
          style={{
            opacity: metrics.percentVisible,
            transform: `translateY(${(1 - metrics.percentVisible) * 20}px)`
          }}
        >
          <p>Visibility: {Math.round(metrics.percentVisible * 100)}%</p>
        </div>
      )}
    </ScrollTracker>
  );
}

API Reference

The hook returns a metrics object with the following properties:

{
  // Visibility metrics
  isVisible: boolean;              // Is element at least partially visible
  isFullyVisible: boolean;         // Is element 100% visible
  percentVisible: number;          // 0-1 value of visibility percentage
  
  // Position metrics
  positionRelativeToViewport: 'above' | 'inView' | 'below';  // General position
  distanceFromViewportTop: number;    // Pixels from top of viewport
  distanceFromViewportCenter: number; // Pixels from center of viewport
  distanceFromViewportBottom: number; // Pixels from bottom of viewport
  
  // Threshold metrics
  thresholdsPassed: string[];      // Array of passed threshold names
  
  // Direction metrics
  scrollDirection: 'up' | 'down' | null;  // Current scroll direction
  
  // Physics metrics
  scrollVelocity: number;          // Pixels per second
  scrollAcceleration: number;      // Change in velocity
  scrollInertia: number;           // Weighted historical velocity
  
  // Entry/Exit metrics
  entryDirection: 'top' | 'bottom' | null;  // Direction element entered viewport
  exitDirection: 'top' | 'bottom' | null;   // Direction element exited viewport
  timeSpentVisible: number;        // Milliseconds element has been visible
}

Configuration Options

useScrollTracker(ref, {
  // Optional configuration
  thresholds: [0.25, 0.5, 0.75],   // Visibility thresholds to track
  throttleMs: 100,                 // Event throttling in milliseconds
  disabled: false,                 // Disable tracking when not needed
});

Browser Support

  • Modern browsers that support IntersectionObserver and ResizeObserver
  • Fallback implementations for older browsers provided via polyfills

Performance Considerations

This hook is optimized for performance through:

  • Using efficient browser APIs instead of scroll event listeners
  • Throttling calculations to prevent excessive re-renders
  • Only tracking elements when they're near the viewport
  • Passive event listeners to avoid blocking the main thread

For more details on performance optimizations, see the SPEC.md file.

Advanced Usage

For advanced examples and implementation details, please refer to:

License

MIT