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

use-in-viewport-react

v1.0.8

Published

A React hook to detect if an element is in the viewport

Readme

use-in-viewport-react

Live Demo - Click Here

A lightweight and performant React hook to check if an element is in the viewport using the Intersection Observer API. This hook allows you to easily track an element’s visibility and trigger UI updates, animations, lazy loading, or other effects when an element enters or leaves the viewport.

🚀 Features

Tiny & Fast – Uses the native IntersectionObserver API for efficient performance.
Customizable – Supports root, rootMargin, and threshold options for precise control.
Simple API – Just pass a ref, and get a boolean indicating visibility.
No Dependencies – Pure React, works with React 16.8+ (including React 19).
SSR Compatible – Works well with frameworks like Next.js.

Installation

To install the package, run:

npm install use-in-viewport-react

Usage

Example Usage in a Functional Component

import { useRef } from 'react';
import { useInViewportReact } from 'use-in-viewport-react';

function App() {
  const ref = useRef<HTMLDivElement>(null);
  const isInViewport = useInViewportReact(ref, {
    threshold: 1,
  });

  return (
    <div>
      <h1>Testing useInViewportReact Hook</h1>
      <div style={{ height: '100vh', background: '#f0f0f0' }}>
        <p>
          Scroll down to see the change in color when the box comes into view.
        </p>
      </div>

      <div
        ref={ref}
        style={{
          height: '200px',
          background: isInViewport ? 'green' : 'red',
          transition: 'background-color 0.3s ease',
        }}
      >
        This element is {isInViewport ? 'in' : 'out of'} the viewport.
      </div>

      <div style={{ height: '100vh', background: '#f0f0f0' }}>
        <p>Scroll further to test the visibility change.</p>
      </div>
    </div>
  );
}

export default App;

📌 Use Cases

🔹 Lazy Load Images & Components – Load content only when it becomes visible to optimize performance.
🔹 Infinite Scrolling – Detect when users reach the bottom of a list and fetch more content dynamically.
🔹 Trigger Animations – Animate elements when they appear in the viewport for a better UX.
🔹 Sticky Elements – Change styles or behavior when an element enters/leaves the viewport.
🔹 Ad Tracking & Analytics – Track when specific content is visible to users.

⚙️ Options

| Option | Type | Default | Description | | ------------ | -------------------- | ------- | ------------------------------------------------------------------------ | | root | Element \| null | null | The element used as the viewport. Defaults to null (browser viewport). | | rootMargin | string | "0" | Margin around the root element (e.g., "10px"). | | threshold | number \| number[] | 0.1 | Visibility percentage that triggers the observer. |

🛠️ Compatibility

React 16.8+ (Hooks API)
React 17, 18, and 19
ES5 & ES6 compatible
✅ Works with Next.js, Gatsby, Vite, and Create React App

🌟 Why use use-in-viewport-react?

There are several similar hooks available, but use-in-viewport-react is optimized for performance, simplicity, and flexibility. It helps you build smooth scrolling experiences, lazy load content, and create engaging animations with minimal effort.