reheaded
Version:
[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![downloads][downloads-badge]][npmcharts] [![version][version-badge]][package] [![MIT License][license-badge]][license]
3 lines (2 loc) • 7.67 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("preact")):"function"==typeof define&&define.amd?define(["preact"],t):e.Reheaded=t(e.preact)}(this,function(e){"use strict";var t=function(e,t,n,i){var o=n?n.call(i,e,t):void 0;if(void 0!==o)return!!o;if(e===t)return!0;if("object"!=typeof e||!e||"object"!=typeof t||!t)return!1;var r=Object.keys(e),c=Object.keys(t);if(r.length!==c.length)return!1;for(var l=Object.prototype.hasOwnProperty.bind(t),s=0;s<r.length;s++){var a=r[s];if(!l(a))return!1;var u=e[a],f=t[a];if(!1===(o=n?n.call(i,u,f,a):void 0)||void 0===o&&u!==f)return!1}return!0},n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};for(var i,o=(function(e){(function(){var t,n,i,o,r,c;"undefined"!=typeof performance&&null!==performance&&performance.now?e.exports=function(){return performance.now()}:"undefined"!=typeof process&&null!==process&&process.hrtime?(e.exports=function(){return(t()-r)/1e6},n=process.hrtime,o=(t=function(){var e;return 1e9*(e=n())[0]+e[1]})(),c=1e9*process.uptime(),r=o-c):Date.now?(e.exports=function(){return Date.now()-i},i=Date.now()):(e.exports=function(){return(new Date).getTime()-i},i=(new Date).getTime())}).call(n)}(i={exports:{}},i.exports),i.exports),r="undefined"==typeof window?n:window,c=["moz","webkit"],l="AnimationFrame",s=r["request"+l],a=r["cancel"+l]||r["cancelRequest"+l],u=0;!s&&u<c.length;u++)s=r[c[u]+"Request"+l],a=r[c[u]+"Cancel"+l]||r[c[u]+"CancelRequest"+l];if(!s||!a){var f=0,d=0,p=[];s=function(e){if(0===p.length){var t=o(),n=Math.max(0,1e3/60-(t-f));f=n+t,setTimeout(function(){var e=p.slice(0);p.length=0;for(var t=0;t<e.length;t++)if(!e[t].cancelled)try{e[t].callback(f)}catch(e){setTimeout(function(){throw e},0)}},Math.round(n))}return p.push({handle:++d,callback:e,cancelled:!1}),d},a=function(e){for(var t=0;t<p.length;t++)p[t].handle===e&&(p[t].cancelled=!0)}}var h=function(e){return s.call(r,e)};function g(){}h.cancel=function(){a.apply(r,arguments)},h.polyfill=function(e){e||(e=r),e.requestAnimationFrame=s,e.cancelAnimationFrame=a},h.polyfill();var m=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},v=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},w=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},y=function(e){function n(t){m(this,n);var i=w(this,e.call(this,t));return i.setRef=function(e){i.inner=e},i.setHeightOffset=function(){i.setState(function(e){return e.height===i.inner.offsetHeight?null:{height:i.inner.offsetHeight}}),i.resizeTicking=!1},i.getScrollY=function(){var e=i.props.parent();return void 0!==e.pageYOffset?e.pageYOffset:void 0!==e.scrollTop?e.scrollTop:(document.documentElement||document.body.parentNode||document.body).scrollTop},i.getViewportHeight=function(){return window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight},i.getDocumentHeight=function(){var e=document,t=e.body,n=e.documentElement;return Math.max(t.scrollHeight,n.scrollHeight,t.offsetHeight,n.offsetHeight,t.clientHeight,n.clientHeight)},i.getElementPhysicalHeight=function(e){return Math.max(e.offsetHeight,e.clientHeight)},i.getElementHeight=function(e){return Math.max(e.scrollHeight,e.offsetHeight,e.clientHeight)},i.getScrollerPhysicalHeight=function(){var e=i.props.parent();return e===window||e===document.body?i.getViewportHeight():i.getElementPhysicalHeight(e)},i.getScrollerHeight=function(){var e=i.props.parent();return e===window||e===document.body?i.getDocumentHeight():i.getElementHeight(e)},i.isOutOfBound=function(e){var t=i.getScrollerPhysicalHeight(),n=i.getScrollerHeight();return e<0||e+t>n},i.handleScroll=function(){i.scrollTicking||(i.scrollTicking=!0,window.requestAnimationFrame(i.update))},i.handleResize=function(){i.resizeTicking||(i.resizeTicking=!0,window.requestAnimationFrame(i.setHeightOffset))},i.setNewState=function(e){i.setState(function(t){return{state:e,shouldAnimate:"unfixed"!==t.state}})},i.unpin=function(){i.props.onUnpin(),i.setNewState("unpinned")},i.pin=function(){i.props.onPin(),i.setNewState("pinned")},i.unfix=function(){i.props.onUnfix(),i.setNewState("unfixed")},i.update=function(){if(i.currentScrollY=i.getScrollY(),!i.isOutOfBound(i.currentScrollY)){var e=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},o=arguments[4],r=t>=e?"down":"up",c=Math.abs(t-e);return n.disabled?{action:"none",scrollDirection:r,distanceScrolled:c}:t<=n.pinStart&&"unfixed"!==i.state?{action:"unfix",scrollDirection:r,distanceScrolled:c}:t<=o&&"down"===r&&"unfixed"===i.state?{action:"none",scrollDirection:r,distanceScrolled:c}:"down"===r&&["pinned","unfixed"].indexOf(i.state)>=0&&t>o+n.pinStart&&c>n.downTolerance?{action:"unpin",scrollDirection:r,distanceScrolled:c}:"up"===r&&c>n.upTolerance&&["pinned","unfixed"].indexOf(i.state)<0?{action:"pin",scrollDirection:r,distanceScrolled:c}:"up"===r&&t<=o&&["pinned","unfixed"].indexOf(i.state)<0?{action:"pin",scrollDirection:r,distanceScrolled:c}:{action:"none",scrollDirection:r,distanceScrolled:c}}(i.lastKnownScrollY,i.currentScrollY,i.props,i.state,i.state.height).action;"pin"===e?i.pin():"unpin"===e?i.unpin():"unfix"===e&&i.unfix()}i.lastKnownScrollY=i.currentScrollY,i.scrollTicking=!1},i.currentScrollY=0,i.lastKnownScrollY=0,i.scrollTicking=!1,i.resizeTicking=!1,i.state={state:t.forcePin?"pinned":"unfixed",shouldAnimate:!1,height:0},i}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(n,e),n.prototype.componentDidMount=function(){var e=this.props,t=e.disabled,n=e.forcePin,i=e.parent,o=e.calcHeightOnResize;this.setHeightOffset();var r=i();t||(n||r.addEventListener("scroll",this.handleScroll),o&&r.addEventListener("resize",this.handleResize))},n.prototype.shouldComponentUpdate=function(e,n){return!t(this.props,e)||!t(this.state,n)},n.prototype.componentDidUpdate=function(e){var t=this.props.parent();this.props.forcePin&&!e.forcePin?(this.pin(),t.removeEventListener("scroll",this.handleScroll)):!this.props.forcePin&&e.forcePin&&t.addEventListener("scroll",this.handleScroll),this.props.disabled&&!e.disabled?(this.unfix(),t.removeEventListener("scroll",this.handleScroll),t.removeEventListener("resize",this.handleResize)):!this.props.disabled&&e.disabled&&(t.addEventListener("scroll",this.handleScroll),this.props.calcHeightOnResize&&t.addEventListener("resize",this.handleResize))},n.prototype.componentWillUnmount=function(){var e=this.props.parent();e.removeEventListener("scroll",this.handleScroll),e!==window&&window.removeEventListener("scroll",this.handleScroll),e.removeEventListener("resize",this.handleResize)},n.prototype.render=function(){var e,t,n;return(e=this.props.children,t=g,!(n=Array.isArray(e)?e[0]:e)&&t?t:n)(v({setRef:this.setRef},this.state))},n}(e.Component);return y.defaultProps={parent:function(){return window},forcePin:!1,disabled:!1,upTolerance:5,downTolerance:5,onPin:g,onUnpin:g,onUnfix:g,pinStart:0,calcHeightOnResize:!0},y});
//# sourceMappingURL=reheaded.umd.min.js.map