Provide a component to wrap your react component and check if there are visible on the screen. You can use this component to trigger an entrance animation for instance !
The component simply passes a isVisible
props to your component.
- Don't rely on findDOMNode. So it can be used with a stateless component.
- Transfer all the props to the wrapped component
- Visibility can be tracked only once
- Event listeners are implemented with throttle to avoid memory leaks or performance issues
View the demo.
Using npm :
$ npm install --save react-on-screen
A UMD build is also available :
<script src="./dist/ReactOnScreen.min.js"></script>
import React from 'react';
import TrackVisibility from 'react-on-screen'; // CommonJs : require('react-on-screen').default
const ComponentToTrack = (props) => {
const style = {
background: props.isVisible ? 'red' : 'blue'
};
return <div style={style}>Hello</div>;
}
const YourApp = () => {
return (
{/* Some Stuff */}
<TrackVisibility>
<ComponentToTrack />
</TrackVisibility>
{/* Some Stuff */}
);
}
props | type | default | description |
---|---|---|---|
once | bool | false | If set to true track the visibility only once and remove the event listeners |
throttleInterval | int | 150 | Tweak the event listeners. See David Corbacho's article |
children | React Components | - | Can be on or many react components |
style | object | - | Style attributes |
className | string | - | Css classes |
offset | number | 0 | Allows you to specify how far left or above of the viewport you want to set isVisible to true |
- Check for partial visibility
Any contributions is welcome !
- Lint :
$ npm run lint
- Test :
$ npm run test
- Build :
$ npm run build // will lint and run test before
Licensed under MIT