UNPKG

react-scroll-agent

Version:

Deadly spy for make benefit glorious nation of Scrolltopia

2 lines (1 loc) 4.37 kB
var ScrollAgent=function(a,u){"use strict";function o(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 s(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function f(e){return(f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function c(e,t){return(c=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function l(e,t){if(null==e)return{};var r,n,o=function(e,t){if(null==e)return{};var r,n,o={},c=Object.keys(e);for(n=0;n<c.length;n++)r=c[n],0<=t.indexOf(r)||(o[r]=e[r]);return o}(e,t);if(Object.getOwnPropertySymbols){var c=Object.getOwnPropertySymbols(e);for(n=0;n<c.length;n++)r=c[n],0<=t.indexOf(r)||Object.prototype.propertyIsEnumerable.call(e,r)&&(o[r]=e[r])}return o}function p(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function d(e){return function(e){if(Array.isArray(e)){for(var t=0,r=new Array(e.length);t<e.length;t++)r[t]=e[t];return r}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}a=a&&a.hasOwnProperty("default")?a.default:a,u=u&&u.hasOwnProperty("default")?u.default:u;var e=function(e){function i(){var e,n,t,r;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,i);for(var o=arguments.length,c=new Array(o),l=0;l<o;l++)c[l]=arguments[l];return t=this,r=(e=f(i)).call.apply(e,[this].concat(c)),s(p(p(n=!r||"object"!=typeof r&&"function"!=typeof r?p(t):r)),"_lastH",-1),s(p(p(n)),"_lastRecalc",void 0),s(p(p(n)),"wrapper",a.createRef()),s(p(p(n)),"observer",void 0),s(p(p(n)),"state",{current:-1,positions:[]}),s(p(p(n)),"observe",function(){!n.observer&&n.wrapper.current&&(n.observer=u(n.wrapper.current,n.handleChange),n.observer.observe())}),s(p(p(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()},500)))}),s(p(p(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:d(e).map(function(e){return e.getBoundingClientRect().top+window.scrollY}).sort(function(e,t){return e-t})})}),s(p(p(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:d(e.positions).concat([1/0]).findIndex(function(e){return 0<e-window.scrollY-n.threshold})-1}}),n._lastY=e)}),n}var t,r,n;return function(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&&c(e,t)}(i,a.PureComponent),t=i,(r=[{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,l(e,["children","detectEnd","nav","selector","threshold"]));return a.createElement("div",n,r({current:this.state.current,positions:this.state.positions}),a.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}}}])&&o(t.prototype,r),n&&o(t,n),i}();return e.defaultProps={children:void 0,detectEnd:!0,nav:function(){return null},threshold:"top"},e}(React,observeRect);