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 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-element-in-view

v1.0.3

Published

A simple React hook to track whether an element is visible in the viewport with the Intersection Observer. This API provides a native way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-leve

Downloads

54

Readme

use-element-in-view

A simple React hook to track whether an element is visible in the viewport with the Intersection Observer. This API provides a native way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.

Browser support for the Intersection Observer is incredibly wide, you can view the full list here. However, if you need to support older browsers, you can add a polyfill from here.

Install

💡 Note: Since this module uses React Hooks, you'll need to have version >=16.8.0 of react and react-dom installed in your project

npm install use-element-in-view --save-dev
# or
yarn add use-element-in-view --dev

Quick Start

import React from 'react';
import { useElementInView } from 'use-element-in-view';

function App() {
    const { inView, assignRef } = useElementInView();

    return <div ref={assignRef}>In View? {inView}</div>;
}

Using your own ref.

import React, { useRef } from 'react';
import { useElementInView } from 'use-element-in-view';

function App() {
    const ref = useRef<HTMLDivElement>(null);
    const { inView } = useElementInView({ ref });

    return <div ref={ref}>In View? {inView}</div>;
}

You can view more examples on CodeSandbox (coming soon).

API

useElementInView(options)

function useElementInView<T extends HTMLElement = HTMLElement>(
    options?: IElementInViewOptions<T> = {}
): IElementInViewResult<T>;

Arguments: IElementInViewOptions<T>

  • Note, all arguments are optional. The properties root, rootMargin and threshold are specific to the native Intersection Observer API and are forwarded along, more information can be found here

| Argument | Type | Default value | Description | | --------------------- | -------------------------------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ref | T \| RefObject<T> \| null | null | Pass in your own ref instead of using the ref callback provided. This can be useful if you already have a ref inside your component you want to observe. | | defaultInView | boolean | false | Set the default value for the inView property. | | disconnectOnceVisible | boolean | false | Will disconnect the observer once the observed element has entered the viewport. A use-case for this is for lazy-loading images. | | onChange | (entry: IntersectionObserverEntry) => void | undefined | Provide a callback that receives the full IntersectionObserverEntry as an argument that fires on each change of element intersection. | | root | Element \| null | null | The Element or Document whose bounds are used as the bounding box when testing for intersection. If no root value was passed to the constructor or its value is null, the top-level document's viewport is used | | rootMargin | string | '0px' | A string which specifies a set of offsets to add to the root's bounding box when calculating intersections, effectively shrinking or growing the root for calculation purposes. The syntax is approximately the same as that for the CSS margin property. The default is "0px 0px 0px 0px". | | threshold | number \| number[] | 0 | A list of thresholds, sorted in increasing numeric order, where each threshold is a ratio of intersection area to bounding box area of an observed target. Notifications for a target are generated when any of the thresholds are crossed for that target. If no value was passed to the constructor, 0 is used. |

Returns: IElementInViewResult<T>

| Argument | Type | Description | | ---------- | ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | inView | boolean | Value representing if the observed element is visible in the viewport or not. | | entry | IntersectionObserverEntry \| undefined | The full entry (IntersectionObserverEntry) returned from the Intersection Observer callback when invoked. | | assignRef | RefCallback<T> | A callback ref to be assigned to your element. Note, if you do not pass in your own ref, this must be added to the element you wish to observe. | | disconnect | () => void | Function that will disconnect the current observer instance shall you need to trigger this yourself. |

Contributing

You can report bugs and issues here.

Pull-requests are more than welcome if you feel like you can improve or fix something 💥

License

MIT