react-scroll-agent
Version:
Deadly spy for make benefit glorious nation of Scrolltopia
2 lines (1 loc) • 5.64 kB
JavaScript
;function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var React=_interopDefault(require("react")),observeRect=_interopDefault(require("@reach/observe-rect"));function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),e}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_setPrototypeOf(e,t)}function _getPrototypeOf(e){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function _objectWithoutPropertiesLoose(e,t){if(null==e)return{};var r,n,o={},i=Object.keys(e);for(n=0;n<i.length;n++)r=i[n],0<=t.indexOf(r)||(o[r]=e[r]);return o}function _objectWithoutProperties(e,t){if(null==e)return{};var r,n,o=_objectWithoutPropertiesLoose(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(n=0;n<i.length;n++)r=i[n],0<=t.indexOf(r)||Object.prototype.propertyIsEnumerable.call(e,r)&&(o[r]=e[r])}return o}function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _possibleConstructorReturn(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?_assertThisInitialized(e):t}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_nonIterableSpread()}function _arrayWithoutHoles(e){if(Array.isArray(e)){for(var t=0,r=new Array(e.length);t<e.length;t++)r[t]=e[t];return r}}function _iterableToArray(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}var REFIRE=500,TOP="top",CENTER="center",BOTTOM="bottom",ScrollAgent=function(e){function i(){var e,n;_classCallCheck(this,i);for(var t=arguments.length,r=new Array(t),o=0;o<t;o++)r[o]=arguments[o];return _defineProperty(_assertThisInitialized(_assertThisInitialized(n=_possibleConstructorReturn(this,(e=_getPrototypeOf(i)).call.apply(e,[this].concat(r))))),"_lastH",-1),_defineProperty(_assertThisInitialized(_assertThisInitialized(n)),"_lastRecalc",void 0),_defineProperty(_assertThisInitialized(_assertThisInitialized(n)),"wrapper",React.createRef()),_defineProperty(_assertThisInitialized(_assertThisInitialized(n)),"observer",void 0),_defineProperty(_assertThisInitialized(_assertThisInitialized(n)),"state",{current:-1,positions:[]}),_defineProperty(_assertThisInitialized(_assertThisInitialized(n)),"observe",function(){!n.observer&&n.wrapper.current&&(n.observer=observeRect(n.wrapper.current,n.handleChange),n.observer.observe())}),_defineProperty(_assertThisInitialized(_assertThisInitialized(n)),"handleChange",function(e){var t=e.top,r=e.height;"undefined"!=typeof window&&(n.handleScroll(t),n.wrapper.current&&0<r&&r!==n._lastH&&(n.handleRecalc(),n._lastH=r,clearTimeout(n._lastRecalc),n._lastRecalc=setTimeout(function(){return n.handleRecalc()},REFIRE)))}),_defineProperty(_assertThisInitialized(_assertThisInitialized(n)),"handleRecalc",function(){var e=[];try{e=n.wrapper.current.querySelectorAll(n.props.selector)}catch(e){console.error('⚠️ ReactScrollAgent: failed prop `selector="'.concat(n.props.selector,'"`. Must be a valid param for document.querySelectorAll(): https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll')),console.error(e)}n.setState({positions:_toConsumableArray(e).map(function(e){return e.getBoundingClientRect().top+window.scrollY}).sort(function(e,t){return e-t})})}),_defineProperty(_assertThisInitialized(_assertThisInitialized(n)),"handleScroll",function(e){n.props.detectEnd&&Math.floor(n._lastH-window.scrollY-window.innerHeight)<=1?n.setState(function(e){return{current:e.positions.length-1}}):(n.setState(function(e){return{current:_toConsumableArray(e.positions).concat([1/0]).findIndex(function(e){return 0<e-window.scrollY-n.threshold})-1}}),n._lastY=e)}),n}return _inherits(i,React.PureComponent),_createClass(i,[{key:"componentDidMount",value:function(){this.observe(),this.handleRecalc(),this.handleScroll(window.scrollY)}},{key:"componentDidUpdate",value:function(){this.observe()}},{key:"componentWillUnmount",value:function(){this.observer&&this.observer.unobserve()}},{key:"render",value:function(){var e=this.props,t=e.children,r=(e.detectEnd,e.nav),n=(e.selector,e.threshold,_objectWithoutProperties(e,["children","detectEnd","nav","selector","threshold"]));return React.createElement("div",n,r({current:this.state.current,positions:this.state.positions}),React.createElement("div",{ref:this.wrapper},t))}},{key:"threshold",get:function(){switch(this.props.threshold){case CENTER:return Math.floor(window.innerHeight/2);case BOTTOM:return window.innerHeight;case TOP:default:return 0}}}]),i}();ScrollAgent.defaultProps={children:void 0,detectEnd:!0,nav:function(){return null},threshold:TOP},module.exports=ScrollAgent;