GithubHelp home page GithubHelp logo

isabella232 / react-inview-monitor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snipsco/react-inview-monitor

0.0 0.0 0.0 3.35 MB

Declarative in-view scroll monitor for React JS

Home Page: https://snipsco.github.io/react-inview-monitor/

License: MIT License

JavaScript 100.00%

react-inview-monitor's Introduction

react-inview-monitor

NPM version MIT License

Easy to use, declarative scroll into view component for animations and more

See the demos for some example usage.

There are other ~ scoll monitor libraries for React available, but none of them solved our use cases: to be able to declaratively configure animations and other effects to be triggered when individual elements came into the view. Inspired by the wow reveal animation library we set out to get to the same ease of use but within react's code paradigm.

Note: this library is not an overly generic, comprehensive, or low-level solution to managing scrolling. If you desire to solve one of the use cases we've had in mind it should be an ease for you to get running. If you need something that doesn't quite fit this solution, or if you just you need more control, consider one of the following libraries:

Usage

IntersectionObserver notes

This library uses IntersectionObserver under the hood.

Support

Until IntersectionObserver is supported in all modern browsers (see support tables) we recommend that you use it together with a polyfill, like the one from the following cdn which only polyfills (downloads code) if necessary: https://cdn.polyfill.io/v2/polyfill.js?features=IntersectionObserver

⚠️ This polyfill currently requires that you set html, body {height: 100%} in your css for it to function correctly.

What not to use this library for

IntersectionObserver uses requestIdleCallback internally, sacrificing low-latency (more direct) pixel accurate updates for performance. This means that there's not guarantee that the in/out of view updates will happen exactly when the element goes in/out of view - they can be delayed until the browser is less busy. For regular usage this is not a problem, but if you need a very high level of accuracy, this library (and IntersectionObserver) is not for you.

Reveal animation when scrolled into view

<InViewMonitor
  classNameNotInView='vis-hidden'
  classNameInView='animated fadeInUp'
>
  <ElementToAnimateIn />
</InViewMonitor>

Note: these classes are not included. We are big fans of animate.css for simple "just add water" animations.

Send custom prop to children when scrolled into view

Can be used for example to auto play a video. Toggle prop saves GPU and battery by stopping the video when no longer in view!

<InViewMonitor
  childPropsInView={{isPlaying: true}}
  toggleChildPropsOnInView={true}
>
  <VideoPlayer />
</InViewMonitor>

See these and more working examples on demo page.

Component properties

Property Type Description
classNameInView string common use: add classes to animate in element
classNameNotInView string common use: visually hide element to be animated in.
classNameAboveView string apply class when scrolled past view, f.e. sticky header.
classNameNotAboveView string apply class when not scrolled past view.
toggleClassNameOnInView boolean Toggle between classNameInView/classNameNotInView, instead of just replacing the first time element comes into view and then removing monitoring. default: false
childPropsInView object props propagated to the child element. Can be used to start video, complex animations and more.
childPropsNotInView object default: {}
toggleChildPropsOnInView boolean Toggle between childPropsInView/childPropsNotInView instead of just add childPropsInView the first time element comes into view and then removing monitoring.. default: false
onInView function callback when in view, can be used for tracking. Receives IntersectionObserverEntry as argument.
onNotInView function callback when not in view. Receives IntersectionObserverEntry as argument.
repeatOnInView boolean Repeats callbacks for onInView/onNotInView, rather than firing just the first time. default: false
intoViewMargin string - css margin Margin added to viewport for area to consider “in view”, can be negative. Use f.e. with positive value for lazy loading content just before in view, or with negative to start fading in element just after in view. Must be px or %. Default: ‘-20%’.
useInviewMonitor func Convenient function that can be used to dynamically disable the monitor, for example for mobile devices.
threshold number Value between 0 and 1 which indicate at what percentage of the target's visibility the observer's callback should be executed. default: 0

Contributing

Please see the Contribution Guidelines.

Copyright

This component is provided by Snips as Open Source Software. See LICENSE.txt for more information.

react-inview-monitor's People

Contributors

dependabot[bot] avatar i8ramin avatar ledoux avatar pocketjoso avatar tiaanduplessis 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.