<commit_before>/* eslint-disable no-undef */
import React from 'react';
import ReactDOM from 'react-dom';

function debounce(func) {
  let timeout;
  return function(...args) {
    const context = this;

    const lastCall = () => {
      timeout = null;
      func.apply(context, args);
    };

    clearTimeout(timeout);
    timeout = setTimeout(lastCall , 100);
  }
}

const IsScrollingHoC = TheComponent =>
  class IsScrollingComponent extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        isScrolling: false,
        lastScrollTop: null,
        direction: null,
      };
    }

    DOMElement = window;

    setScrollOn = () => {
      const { isScrolling, lastScrollTop } = this.state;

      if (!isScrolling) {
        this.setState({
          isScrolling: true,
          lastScrollTop: this.DOMElement.document.body.scrollTop,
        });
      }

      if (lastScrollTop) {
        this.detectDirection(lastScrollTop, this.DOMElement.document.body.scrollTop);
        this.setState({ lastScrollTop: null });
      }
       this.setScrollOff();
    };

    detectDirection(lastScrollTop, nextScrollTop) {
      if (lastScrollTop < nextScrollTop) {
        this.setState({ direction: 'down' });
        return;
      }
      this.setState({ direction: 'up' });
    }

    componentDidMount() {
      if (this.DOMElement) {
        this.DOMElement.addEventListener('scroll', this.setScrollOn, true);
      }
    }

    componentWillUnmount() {
      if (this.DOMElement) {
        this.DOMElement.removeEventListener('scroll', this.setScrollOn, true);
      }
    }

    setScrollOff = debounce(() => {
      if (this.state.isScrolling) {
        this.setState({ isScrolling: false });
      }
    });

    render() {
      return (
        <TheComponent
          {...this.props}
          isScrolling={this.state.isScrolling}
          isScrollingDown={this.state.direction === 'down'}
          isScrollingUp={this.state.direction === 'up'}
        />
      );
    }
  };

export default IsScrollingHoC;
<commit_msg>fix firefox bug with using available scrollTop<commit_after>/* eslint-disable no-undef */
import React from 'react';
import ReactDOM from 'react-dom';

function debounce(func) {
  let timeout;
  return function(...args) {
    const context = this;

    const lastCall = () => {
      timeout = null;
      func.apply(context, args);
    };

    clearTimeout(timeout);
    timeout = setTimeout(lastCall , 100);
  }
}

function getBrowserScrollTop() {
  return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
}

const IsScrollingHoC = TheComponent =>
  class IsScrollingComponent extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        isScrolling: false,
        lastScrollTop: null,
        direction: null,
      };
    }

    DOMElement = window;

    setScrollOn = () => {
      const { isScrolling, lastScrollTop } = this.state;

      if (!isScrolling) {
        this.setState({
          isScrolling: true,
          lastScrollTop: getBrowserScrollTop(),
        });
      }

      if (lastScrollTop) {
        this.detectDirection(lastScrollTop, getBrowserScrollTop());
        this.setState({ lastScrollTop: null });
      }
       this.setScrollOff();
    };

    detectDirection(lastScrollTop, nextScrollTop) {
      if (lastScrollTop < nextScrollTop) {
        this.setState({ direction: 'down' });
        return;
      }
      this.setState({ direction: 'up' });
    }

    componentDidMount() {
      if (this.DOMElement) {
        this.DOMElement.addEventListener('scroll', this.setScrollOn, true);
      }
    }

    componentWillUnmount() {
      if (this.DOMElement) {
        this.DOMElement.removeEventListener('scroll', this.setScrollOn, true);
      }
    }

    setScrollOff = debounce(() => {
      if (this.state.isScrolling) {
        this.setState({ isScrolling: false, direction: null, lastScrollTop: null });
      }
    });

    render() {
      return (
        <TheComponent
          {...this.props}
          isScrolling={this.state.isScrolling}
          isScrollingDown={this.state.direction === 'down'}
          isScrollingUp={this.state.direction === 'up'}
        />
      );
    }
  };

export default IsScrollingHoC;

{'fraction_non_alphanumeric': 0.12824074074074074, 'fraction_numerical': 0.0016203703703703703, 'mean_word_length': 2.3418406805877803, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '306752', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1262, 'n_words': 239}