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

react-observed

v1.0.3

Published

React component using the browser's Intersection Observer API to watch for when an element is within (or intersecting with) the viewport.

Downloads

142

Readme

React Observed

npm version Build Status codecov

React component using the browser's Intersection Observer API to watch for when an element is within (or intersecting with) the viewport.

Example

Storybook Demos

Install

npm install react-observed --save

or with Yarn

yarn add react-observed

Note: For complete browser support you must also provide an Intersection Observer polyfill.

npm install intersection-observer --save

or

yarn add intersection-observer

Usage

<Observed> takes a function as a child which gives you access to the isInView and mapRef properties.

mapRef is the ref function callback that must be declared on the observed target element like <div ref={mapRef} />. Then isInView will give you the current state of the observed element.

Here's an example:

<Observed>
    {({ isInView, mapRef }) => (
        <div ref={mapRef}>
            {isInView ? (
                <span>I'm in view!</span>
            ) : (
                <span>I'm not in view</span>
            )}
        </div>
    )}
</Observed>

<Observed> Props

<Observed> takes the props as shown by the following example:

<Observed
    initialViewState // the initial view state state; defaults to `false`
    intersectionRatio={0.5} // target's visibility must pass the 50% threshold to be considered visible
    once // discontinue observing the target once it's become visible
    onChange={isInView => {}} // handler called with the current `isInView` state whenever it changes
    onEnter={() => {}} // handler called when the observed element enters
    onExit={() => {}} // handler called when the observed element exits
    onIntersect={entry => {}} // handler called when each threshold is met with the entry data
    options={{
        // IntersectionObserver constuctor options
        root: null,
        rootMargin: "0px",
        threshold: 0.5
    }}
/>

| Name | Type | Default | Description | | --------------------- | :--------: | :---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | | initialViewState | Boolean | false | Optionally sets the initial isInView state. By default this is false until the observer updates the state. | | intersectionRatio | Number | 0 | The intersection ratio (a value between 0–1) that when >= to the target's intersect ratio and not equal to 0 will be considered in view. | | once | Boolean | false | If once is true the observer will disconnect after the target element has entered the view. Useful for triggering something when in view for a single time. | | onChange | Function | | Handler to be called with the current isInView state whenever it changes. | | onEnter | Function | | Handler to be called when the isInView state changes to true. | | onExit | Function | | Handler to be called when the isInView state changes to false. | | onIntersect | Function | | Handler to be called when each threshold is met with the current entry data. | | options | Object | *see below | Options passed to the IntersectionObserver constructor. |

*IntersectionObserver Options

These are the options passed to the IntersectionObserver constructor. The default options to the <Observed> component are:

options: {
    root: null,
    rootMargin: '0px',
    threshold: [0],
}

The following is as described on MDN:

| Name | Type | Default | Description | | -------------- | :-----------------: | :------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | root | Element | null | The DOM element that is used as the viewport for checking visiblity of the target. Must be the ancestor of the target. Defaults to the browser viewport if not specified or if null. | | rootMargin | String | 0px | Margin around the root. Can have values similar to the CSS margin property, e.g. 10px 20px 30px 40px (top, right, bottom, left). If the root element is specified, the values can be percentages. This set of values serves to grow or shrink each side of the root element's bounding box before computing intersections. Defaults to all zeros. | | threshold | Number or Array | [0] | Either a single Number or an Array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. If you only want to detect when visibility passes the 50% mark, you can use a value of 0.5. If you want the callback run every time visibility passes another 25%, you would specify the array [0, 0.25, 0.5, 0.75, 1]. The default is 0 (meaning as soon as even one pixel is visible, the callback will be run). A value of 1.0 means that the threshold isn't considered passed until every pixel is visible. |

Render Callback

The child function takes one object parameter like such { isInView, mapRef }. It's required to map a ref function to a DOM element otherwise <Observed> will throw an error.

Example:

<Observed>{({ mapRef }) => <div ref={mapRef} />}</Observed>

The keys of which are:

| Name | Type | Description | | ------------ | :--------: | --------------------------------------------------------------------------------------------------------------------------------------- | | isInView | Boolean | This is true when the observed element's intersection ratio is >= to the intersectionRatio prop. | | mapRef | Function | This a function that is declared as the ref of the DOM element to be observed. Note that this is required for the observer to work. |

Browser Support

Intersection Observer is pretty well supported by major browsers, with the exception of Safari/iOS Safari. There's also not been much movement by the Safari team to add support. This is unfortunate but adding a good polyfill will work great for adding support to Safari or IE11. You can track Safari's lack of progress here.