All files / src/components/Sticky index.jsx

0% Statements 0/80
0% Branches 0/47
0% Functions 0/20
0% Lines 0/41
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85                                                                                                                                                                         
import styles from './style.postcss';
 
import React, { PureComponent } from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
 
const CHECK_SAME_HEIGHT_MAX = 5;
 
export class Sticky extends PureComponent {
  constructor(props) {
    super(props);
    this.state = { sticky: false };
    this._onWheel = this._onWheel.bind(this);
    this._checkHeight = this._checkHeight.bind(this);
  }
 
  componentDidMount() {
    window.addEventListener('wheel', this._onWheel);
    window.addEventListener('touchmove', this._onWheel);
    window.addEventListener('scroll', this._onWheel);
  }
 
  componentDidUpdate() {
    this._startPeriodicCheck();
  }
 
  componentWillUnmount() {
    this._stopPeriodicCheck();
    window.removeEventListener('wheel', this._onWheel);
    window.removeEventListener('touchmove', this._onWheel);
    window.removeEventListener('scroll', this._onWheel);
  }
 
  _onWheel() {
    const shouldBeSticky = this._container.getBoundingClientRect().top < 0;
    if (! this.state.sticky && shouldBeSticky) {
      this.setState({ sticky: true });
    } else if (this.state.sticky && ! shouldBeSticky) {
      this.setState({ sticky: false });
    }
  }
 
  _startPeriodicCheck() {
    this._stopPeriodicCheck();
    this._sameHeightCount = 0;
    this._periodicCheckId = setInterval(this._checkHeight, 25);
  }
 
  _stopPeriodicCheck() {
    clearInterval(this._periodicCheckId);
  }
 
  _checkHeight() {
    if (this._sameHeightCount > CHECK_SAME_HEIGHT_MAX) {
      return this._stopPeriodicCheck();
    }
 
    if (this.state.height === this._bar.offsetHeight) {
      this._sameHeightCount += 1;
      return;
    }
 
    this.setState({ height: this._bar.offsetHeight }, this._onWheel);
  }
 
  render() {
    const classes = classnames(styles.Sticky, this.props.className, {
      [styles.__sticky]: this.state.sticky,
    });
    return <div className={classes} ref={(n) => { this._container = n; }}>
      <div className={styles.Sticky_wrapper} ref={(n) => { this._bar = n; }}>
        {this.props.children}
      </div>
      <div className={styles.Sticky_placeholder} style={{ height: this.state.height }} />
    </div>;
  }
}
 
Sticky.propTypes = {
  className: PropTypes.string,
  children: PropTypes.node,
};
 
export default Sticky;