UNPKG

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.76 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["react","prop-types"],t):e.Reheaded=t(e.React,e.PropTypes)}(this,function(e,t){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t;var n=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},i="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};for(var o,r=(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(i)}(o={exports:{}},o.exports),o.exports),c="undefined"==typeof window?i:window,l=["moz","webkit"],s="AnimationFrame",a=c["request"+s],u=c["cancel"+s]||c["cancelRequest"+s],f=0;!a&&f<l.length;f++)a=c[l[f]+"Request"+s],u=c[l[f]+"Cancel"+s]||c[l[f]+"CancelRequest"+s];if(!a||!u){var p=0,d=0,h=[];a=function(e){if(0===h.length){var t=r(),n=Math.max(0,1e3/60-(t-p));p=n+t,setTimeout(function(){var e=h.slice(0);h.length=0;for(var t=0;t<e.length;t++)if(!e[t].cancelled)try{e[t].callback(p)}catch(e){setTimeout(function(){throw e},0)}},Math.round(n))}return h.push({handle:++d,callback:e,cancelled:!1}),d},u=function(e){for(var t=0;t<h.length;t++)h[t].handle===e&&(h[t].cancelled=!0)}}var g=function(e){return a.call(c,e)};function m(){}g.cancel=function(){u.apply(c,arguments)},g.polyfill=function(e){e||(e=c),e.requestAnimationFrame=a,e.cancelAnimationFrame=u},g.polyfill();var v=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},w=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},y=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},S=function(e){function t(n){v(this,t);var i=y(this,e.call(this,n));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:n.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)}(t,e),t.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))},t.prototype.shouldComponentUpdate=function(e,t){return!n(this.props,e)||!n(this.state,t)},t.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))},t.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)},t.prototype.render=function(){var e,t,n;return(e=this.props.children,t=m,!(n=Array.isArray(e)?e[0]:e)&&t?t:n)(w({setRef:this.setRef},this.state))},t}(e.Component);return S.defaultProps={parent:function(){return window},forcePin:!1,disabled:!1,upTolerance:5,downTolerance:5,onPin:m,onUnpin:m,onUnfix:m,pinStart:0,calcHeightOnResize:!0},S}); //# sourceMappingURL=reheaded.umd.min.js.map