Skip to content

Commit

Permalink
Add calc fix & friendlier error message
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Aug 10, 2018
1 parent 9035f23 commit adf544a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"rules": {
"arrow-parens": 0,
"import/no-extraneous-dependencies": 0,
"no-underscore-dangle": 0,
"react/jsx-filename-extension": 0,
"react/destructuring-assignment": 0
}
Expand Down
42 changes: 42 additions & 0 deletions packages/babel-plugin-bridge/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { existsSync, mkdirSync } = require('fs');
const { resolve } = require('path');
const { ncp } = require('ncp');
const { declare } = require('@babel/helper-plugin-utils');

module.exports = declare((api, opts) => {
api.assertVersion(7);

const modules = opts.modules || 'node_modules';
const out = opts.out || 'dist/modules';

return {
visitor: {
ImportDeclaration({
node: {
source: { value },
},
}) {
const outputDir = resolve(process.cwd(), out);
if (!existsSync(outputDir)) mkdirSync(outputDir);

const isLocalModule = value[0] === '.';
if (isLocalModule) {
const pathToModule = resolve(__dirname, value);
const outputPath = resolve(process.cwd(), out, value);
console.log(pathToModule, outputPath);
if (!existsSync(outputPath)) {
ncp(pathToModule, outputPath, err => console.error(err));
}
return;
}

const pathToModule = resolve(process.cwd(), modules, value);
const outputPath = resolve(process.cwd(), out, value);
console.log(pathToModule, outputPath);
if (!existsSync(pathToModule)) {
ncp(pathToModule, outputPath, err => console.error(err));
}
},
},
};
});
26 changes: 25 additions & 1 deletion src/ScrollAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import React from 'react';
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)

// Values
const TOP = 'top';
const CENTER = 'center';
const BOTTOM = 'bottom';
Expand All @@ -13,6 +17,9 @@ class ScrollAgent extends React.PureComponent {
// Memoized container height, to prevent unnecessary recalcs
_lastH = -1;

// setTimeout placeholder
_lastRecalc = undefined;

// Ref for scrollspy
wrapper = React.createRef();

Expand Down Expand Up @@ -68,13 +75,30 @@ class ScrollAgent extends React.PureComponent {
if (height > 0 && height !== this._lastH) {
this.handleRecalc();
this._lastH = height;

// After last recalculation, wait 500ms and re-fire.
// This fixes calc issues on longer pages when animationFrame skips.
clearTimeout(this._lastRecalc);
this._lastRecalc = setTimeout(() => this.handleRecalc(), REFIRE);
}
};

// Handle height recalculation
handleRecalc = () => {
let elements = [];

try {
elements = this.wrapper.current.querySelectorAll(this.props.selector);
} catch (err) {
console.error(
`⚠️ ReactScrollAgent: failed prop \`selector="${
this.props.selector
}"\`. Must be a valid param for document.querySelectorAll(): https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll`
);
console.error(err);
}
this.setState({
positions: [...this.wrapper.current.querySelectorAll(this.props.selector)]
positions: [...elements]
.map(node => node.getBoundingClientRect().top + window.scrollY)
.sort((a, b) => a - b),
});
Expand Down

0 comments on commit adf544a

Please sign in to comment.