Skip to content

Commit

Permalink
Improve scroll calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Aug 20, 2018
1 parent b6cf98d commit f5937c0
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"parser": "babel-eslint",
"extends": ["react", "airbnb", "prettier/react"],
"plugins": ["react", "prettier", "import", "jsx-a11y"],
"extends": ["prettier/react", "react", "airbnb"],
"plugins": ["prettier", "react", "import", "jsx-a11y"],
"rules": {
"arrow-parens": 0,
"import/no-extraneous-dependencies": 0,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-scroll-agent",
"version": "0.0.3",
"version": "0.1.0",
"description": "Deadly spy for make benefit glorious nation of Scrolltopia",
"main": "dist/index.js",
"scripts": {
Expand Down
42 changes: 0 additions & 42 deletions packages/babel-plugin-bridge/index.js

This file was deleted.

16 changes: 4 additions & 12 deletions src/ScrollAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ import PropTypes from 'prop-types';
import observeRect from '@reach/observe-rect';

// Settings
const REFIRE = 500; // Time in ms to refire calc (takes care of reflow that happens after final animationFrame firing)
const REFIRE = 200; // Time in ms to refire calc (takes care of reflow that happens after final animationFrame firing)

// Values
const TOP = 'top';
const CENTER = 'center';
const BOTTOM = 'bottom';

class ScrollAgent extends React.PureComponent {
// Memoized scroll position, to prevent unnecessary scroll firings
_lastY = -1;

// Memoized container height, to prevent unnecessary recalcs
_lastH = -1;

Expand Down Expand Up @@ -68,9 +65,7 @@ class ScrollAgent extends React.PureComponent {
// Fires on every observation change. Determines what should update.
handleChange = ({ top, height }) => {
if (typeof window === 'undefined') return;
if (top !== this._lastY) {
this.handleScroll(top);
}
this.handleScroll(top);
if (!this.wrapper.current) return;
if (height > 0 && height !== this._lastH) {
this.handleRecalc();
Expand Down Expand Up @@ -111,18 +106,15 @@ class ScrollAgent extends React.PureComponent {
this.props.detectEnd &&
Math.floor(this._lastH - window.scrollY - window.innerHeight) <= 1
) {
this.setState(({ positions }) => ({
current: positions.length - 1,
}));
this.setState(({ positions }) => ({ current: positions.length - 1 }));
return;
}
// Find first section that is “too far,” then step back one.
// Infinity is added at the end so you can step back to the last index.
const threshold = top + window.scrollY + this.threshold;
this.setState(({ positions }) => ({
current:
[...positions, Infinity].findIndex(
y => Math.floor(y - window.scrollY) > threshold
y => y - window.scrollY - this.threshold > 0
) - 1,
}));
this._lastY = top;
Expand Down

0 comments on commit f5937c0

Please sign in to comment.