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-scroll-listener

v0.8.0

Published

listen for and handle scroll events efficiently in React applications

Downloads

994

Readme

react-scroll-listener (+ mixin)

A scroll-event listener class for React apps.


usage:

npm install --save react-scroll-listener

var ScrollListener = require('react-scroll-listener');

// the mixin
var ScrollListenerMixin = ScrollListener.Mixin;
// with es6
import { ScrollListenerMixin } from 'react-scroll-listener';


// you can extend a class:
class myClass extends ScrollListener {

	constructor(){
		// can pass config object to constructor
		super({
			host	: window 	// default host
			delay   : 300 		// default scroll-end timeout
		});
	}
}

// or just create a default instance:
var scrollListener = new ScrollListener();


// create handlers:
var myScrollStartHandler = function( event ){
	console.log( 'logs on every scroll move' );
};

var myScrollEndHandler = function( event ){
	console.log( 'logs only when scrolling has stopped (default 300ms delay)' );
};

// and add handlers after window is loaded
window.onLoad = function(){
	scrollListener.addScrollHandler('some-id', myScrollStartHandler, myScrollEndHandler );
};

// or in a React class:
componentDidMount: function(){
	scrollListener.addScrollHandler('some-id', myScrollStartHandler, myScrollEndHandler );
}

###as mixin:

var MyComponent = React.createClass({

	// call as a function, give an id for efficient reuse in other components
	mixins: [ ScrollListenerMixin('my-component') ],

	// mixin adds onScrollStart and onScrollEnd to the context, so you can use them like this:
	onScrollStart: function( event ){
		// you could re-render on each onscrollstart event (inhibit in child components shouldComponentUpdate with (! this.onScrolling) for performance)
		this.forceUpdate();
	},

	onScrollEnd: function( event ){
		console.log('logs when no scroll-events occurred in the last 300ms(default)');
	},

	render: function(){
		console.log('logs every scrollstart event due to this.onScrollStart handler');
		return <ChildComponent />;
	}
});


var ChildComponent = React.createClass({

	// call as a function, give an id for efficient reuse in other components
	mixins: [ ScrollListenerMixin('my-component') ],

	// inhibit re-rendering during scrollstart events
	shouldComponentUpdate: function(){
		return ! this.scrollListener.isScrolling;
	},

	render: function(){
		console.log('this component will not re-render during scrollstart events');
		console.log('but will re-render after 300ms(default) timeout for scrollend event');
		return null;
	}
});

###methods and props

//
// after creation/initialization the following properties are available in context
//
this.scrollHost				= {}
this.scrollStartHandlers	= {}
this.scrollEndHandlers		= {}
this.scrollTop				= 0
this.isScrolling			= false
this.scrollTimeoutDelay		= 300 // ms
this.scrollListenerSet		= false

// and the following methods:

this.addScrollEventListener();
this.removeScrollEventListener();
this.addScrollHandler( <string/number> id, <function> handler, <boolean> onScrollEnd );
this.addScrollStartHandler( <string/number> id, <function> handler )
this.addScrollEndHandler( <string/number> id, <function> handler )
this.removeScrollStartHandler( <string/number> id )
this.removeScrollEndHandler( <string/number> id )
this.removeScrollHandlers()
this.getScrollListener( <string/number> id )

// internals, don't use
this._scrollListeners
this._scrollTimeout
this._onHostScroll
this._onHostScrollEnd

##Change log:

0.6.0

  • moved from node-uuid v1 to uuid v4
  • fixed typo in readme

0.5.0

  • made available the actual scroll event as argument on onScrollStart and onScrollEnd
  • now using node-uuid for generating unique id's

0.4.2

changed license to MIT


0.4.0

removes ViewportMetrics dependency


##License

MIT