Code Monkey home page Code Monkey logo

react-waypoint's Introduction

React Waypoint

npm version Build Status

A React component to execute a function whenever you scroll to an element. Works in all containers that can scroll, including the window.

React Waypoint can be used to build features like lazy loading content, infinite scroll, or docking elements to the viewport on scroll.

Inspired by Waypoints, except this little library grooves the React way.

Demo

Demo of React Waypoint in action

View demo page

Installation

npm

npm install react-waypoint --save

Usage

var Waypoint = require('react-waypoint');
<Waypoint
  onEnter={this._handleWaypointEnter}
  onLeave={this._handleWaypointLeave}
/>

A waypoint normally fires onEnter and onLeave as you are scrolling, but it can fire because of other events too:

  • When the window is resized
  • When it is mounted (fires onEnter if it's visible on the page)
  • When it is updated/re-rendered by its parent

Callbacks will only fire if the new position changed from the last known position. Sometimes it's useful to have a waypoint that fires onEnter every time it is updated as long as it stays visible (e.g. for infinite scroll). You can then use a key prop to control when a waypoint is reused vs. re-created.

<Waypoint
  key={cursor}
  onEnter={this._loadMoreContent}
/>

Alternatively, you can also use an onPositionChange event to just get notified when the waypoint's position (e.g. inside the viewport, above or below) has changed.

<Waypoint
  onPositionChange={this._handlePositionChange}
/>

Prop types

  propTypes: {

    /**
     * Function called when waypoint enters viewport
     */
    onEnter: PropTypes.func,

    /**
     * Function called when waypoint leaves viewport
     */
    onLeave: PropTypes.func,

    /**
     * Function called when waypoint position changes
     */
    onPositionChange: PropTypes.func,

    /**
     * Threshold - a percentage of the height of the visible
     * part of the scrollable parent (e.g. 0.1)
     */
    threshold: PropTypes.number,

    /**
     * Scrollable Ancestor - A custom ancestor to determine if the
     * target is visible in it. This is useful in cases where
     * you do not want the immediate scrollable ancestor to be
     * the container. For example, when your target is in a div
     * that has overflow auto but you are detecting onEnter based
     * on the window.
     */
    scrollableAncestor: PropTypes.any,

    /**
     * fireOnRapidScroll - if the onEnter/onLeave events are to be fired
     * on rapid scrolling. This has no effect on onPositionChange -- it will
     * fire anyway.
     */
    fireOnRapidScroll: PropTypes.bool,
  },

All callbacks (onEnter/onLeave/onPositionChange) receive an object as the only argument. That object has the following properties:

  • currentPosition - the position that the waypoint has at the moment. One of Waypoint.below, Waypoint.above, Waypoint.inside, and Waypoint.invisible.
  • previousPosition - the position that the waypoint had before. Also one of Waypoint.below, Waypoint.above, Waypoint.inside, and Waypoint.invisible.
  • event - the native scroll event that triggered the callback. May be missing if the callback wasn't triggered as the result of a scroll.

If you use es6 object destructuring, this means that you can use waypoints in the following way:

<Waypoint onEnter={({ previousPosition, currentPosition, event }) => {
    // do something useful!
  }}
/>

If you are more familiar with plain old js functions, you'll do something like this:

<Waypoint onEnter={function(props) {
    // here you can use `props.currentPosition`, `props.previousPosition`, and
    // `props.event`
  }}
/>

Limitations

In this component we make a few assumptions that we believe are generally safe, but in some situations might present limitations.

  • We determine the scrollable-ness of a node by inspecting its computed overflow-y or overflow property and nothing else. This could mean that a container with this style but that does not actually currently scroll will be considered when performing visibility calculations.
  • We assume that waypoint is rendered within at most one scrollable container. If you render a waypoint in multiple nested scrollable containers, the visibility calculations will likely not be accurate.
  • We also base the visibility calculations on the scroll position of the scrollable container (or window if no scrollable container is found). This means that if your scrollable container has a height that is greater than the window, it might trigger onEnter unexpectedly.

Credits

Credit to trotzig and lencioni for writing this component, and the Brigade team for open sourcing it.

Thanks to the creator of the original Waypoints library, imakewebthings.

License

MIT

react-waypoint's People

Contributors

akshayakrsh-mck avatar alex-ionochkin avatar flarnie avatar janpaul123 avatar jordaaash avatar kamui avatar kencoder avatar khankuan avatar lencioni avatar mordeaux avatar neezer avatar nickclaw avatar poteto avatar tomdcc avatar trotzig avatar zaygraveyard avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.