Skip to content

Commit

Permalink
Merge pull request #396 from Automattic/fix/sticky-panel-update-routine
Browse files Browse the repository at this point in the history
Sticky panel: add event listener for resizing to update measurements.
  • Loading branch information
mtias committed Nov 23, 2015
2 parents 29b02dd + 73029cc commit 3151deb
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion client/components/sticky-panel/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
var React = require( 'react' ),
throttle = require( 'lodash/function/throttle' ),
raf = require( 'raf' ),
classNames = require( 'classnames' );

Expand All @@ -25,25 +26,35 @@ module.exports = React.createClass( {
// Determine and cache vertical threshold from rendered element's
// offset relative the document
this.threshold = React.findDOMNode( this ).offsetTop;
this.throttleOnResize = throttle( this.onWindowResize, 200 );

window.addEventListener( 'scroll', this.onWindowScroll );
window.addEventListener( 'resize', this.throttleOnResize );
this.updateIsSticky();
},

componentWillUnmount: function() {
window.removeEventListener( 'scroll', this.onWindowScroll );
window.removeEventListener( 'resize', this.throttleOnResize );
raf.cancel( this.rafHandle );
},

onWindowScroll: function() {
this.rafHandle = raf( this.updateIsSticky );
},

onWindowResize: function() {
this.setState( {
spacerHeight: this.state.isSticky ? React.findDOMNode( this ).clientHeight : 0,
blockWidth: this.state.isSticky ? React.findDOMNode( this ).clientWidth : 0
} );
},

updateIsSticky: function() {
var isSticky = window.pageYOffset > this.threshold;

if ( viewport.isMobile() ) {
return;
return this.setState( { isSticky: false } );
}

if ( isSticky !== this.state.isSticky ) {
Expand Down

0 comments on commit 3151deb

Please sign in to comment.