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-awesome-waypoint

v0.1.13

Published

Componentized Intersection Observer in React

Downloads

16

Readme

react-awesome-waypoint

Componentized Intersection Observer API in React.

Demo

Link

Installation

yarn

yarn add react-awesome-waypoint

npm

npm install react-awesome-waypoint

API

| Paramters | Default | Description | Required | |------------ | --------- |---------- | --------- | | children: React.ReactNode | undefined | The actual target to be rendered. If you don't pass 'children' props, then an empty 'span' element will be rendered instead. | X | | onEnter: () => void | undefined | Callback executed when entered(exposed) on a viewport. | X | | onLeave: () => void | undefined | Callback executed when leaved on a viewport. | X | | root: Element or Document or null or undefined | null | Specifies the element object (root element) to be used instead of the viewport to check the visibility of the target. If not specified or null, the browser's viewport is used by default. | X | | threshold: number or number[] or undefined | [0] | The percentage of how much visibility of the target is required for the observer to run. When set to 0 or [0], the observer runs as soon as the edge pixel of the target crosses the root range. | X | | topOffset: string | '0px' | Use the outer top margin to expand or contract the Root range. | X | | bottomOffset: string | '0px' | Use the outer bottom margin to expand or contract the Root range. | X | | leftOffset: string | '0px' | Use the outer left margin to expand or contract the Root range. | X | | rightOffset: string | '0px' | Use the outer right margin to expand or contract the Root range. | X |

Usage

📢 Recommend to check the Demo.

import { useState } from 'react';
import styled from 'styled-components';
import Waypoint from 'react-awesome-waypoint';

import { getCards } from '../apis/fetch';
import type { Card } from '../apis/fetch';

import CardComp from './Card';

const Cards = () => {
  const [data, setData] = useState<Card[]>([]);

  const handleFetch = async () => {
    const res = await getCards();

    setData(curr => ([
      ...curr,
      ...res
    ]));
  };

  return (
    <Wrapper>
      {data.map(({ id, title, description }, index) => (
        <CardComp
          key={id}
          title={`${title}-${index}`}
          description={description}
        />
      ))}
      <Waypoint onEnter={handleFetch}>
        <div>Loading...</div>
      </Waypoint>
    </Wrapper>
  );
};

export default Cards;

const Wrapper = styled.ul``;

FAQ

1. What is the difference between threshold and offset?

'threshold' refers to how much visibility of the target is needed to trigger observer, as explained by the API. If you set it to 0.3, the observer will be executed when 30% of the target is entered to the viewport.

threshold example

Source: https://heropy.blog/2019/10/27/intersection-observer/

'offset' is same as the rootMargin of the Intersection Observer API. For example, if the bottomOffset is given a value of more than zero, the observer will run before the target wrapped in Waypoint is entered to the viewport when scrolling.

offset example

Source: https://heropy.blog/2019/10/27/intersection-observer/

2. Does it support all features of the Intersection Observer API?

No, it's not. Onlt the features(props) that I think are most useful are added first. If you lack or need any features, please create an issue or post a PR.

3. What should I do if I want to pass components instead of HTML elements?

Please wrap your component with React.forwardRef() like this.

import React, { useState } from 'react';
import Waypoint from 'react-awesome-waypoint';

const Loading = React.forwardRef((_, ref) => (
  <div ref={ref as React.LegacyRef<HTMLDivElement>}>
    Loading...
  </div>
));

const Comp = () => {
  return (
    <Waypoint onEnter={() => {}}>
      <Loading />
    </Waypoint>
  );
};

export default Comp;

4. Does it support react v18.x?

Not yet.