UNPKG

react-visibility-sensor-v2

Version:

Sensor component for React that notifies you when it goes in or out of the window viewport forked by joshwnj.

1 lines 8.44 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react"),require("react-dom")):"function"==typeof define&&define.amd?define(["React","ReactDOM"],t):"object"==typeof exports?exports["react-visibility-sensor"]=t(require("react"),require("react-dom")):e["react-visibility-sensor"]=t(e.React,e.ReactDOM)}(this,function(o,n){return a={},r.m=i=[function(e,t,o){e.exports=o(5)()},function(e,t){e.exports=o},function(e,t){e.exports=n},function(e,t){e.exports=function(e,t,o){var n=e.direction,r=e.value;switch(n){case"top":return o.top+r<t.top&&o.bottom>t.bottom&&o.left<t.left&&o.right>t.right;case"left":return o.left+r<t.left&&o.bottom>t.bottom&&o.top<t.top&&o.right>t.right;case"bottom":return o.bottom-r>t.bottom&&o.left<t.left&&o.right>t.right&&o.top<t.top;case"right":return o.right-r>t.right&&o.left<t.left&&o.top<t.top&&o.bottom>t.bottom}}},function(e,t,o){"use strict";o.r(t),o.d(t,"default",function(){return y});var n=o(1),r=o.n(n),i=o(2),a=o.n(i),l=o(0),c=o.n(l),s=o(3),p=o.n(s);function f(e){return(f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function u(e,t){for(var o=0;o<t.length;o++){var n=t[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function h(e){return(h=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function b(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function d(e,t){return(d=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function m(e,t,o){return t in e?Object.defineProperty(e,t,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[t]=o,e}var y=function(){function t(e){var u;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t),m(b(u=function(e,t){return!t||"object"!==f(t)&&"function"!=typeof t?b(e):t}(this,h(t).call(this,e))),"getContainer",function(){return u.props.containment||window}),m(b(u),"addEventListener",function(e,t,o,n){var r;u.debounceCheck||(u.debounceCheck={});function i(){r=null,u.check()}var a={target:e,fn:-1<n?function(){r=r||setTimeout(i,n||0)}:function(){clearTimeout(r),r=setTimeout(i,o||0)},getLastTimeout:function(){return r}};e.addEventListener(t,a.fn),u.debounceCheck[t]=a}),m(b(u),"startWatching",function(){u.debounceCheck||u.interval||(u.props.intervalCheck&&(u.interval=setInterval(u.check,u.props.intervalDelay)),u.props.scrollCheck&&u.addEventListener(u.getContainer(),"scroll",u.props.scrollDelay,u.props.scrollThrottle),u.props.resizeCheck&&u.addEventListener(window,"resize",u.props.resizeDelay,u.props.resizeThrottle),u.props.delayedCall||u.check())}),m(b(u),"stopWatching",function(){if(u.debounceCheck)for(var e in u.debounceCheck)if(u.debounceCheck.hasOwnProperty(e)){var t=u.debounceCheck[e];clearTimeout(t.getLastTimeout()),t.target.removeEventListener(e,t.fn),u.debounceCheck[e]=null}u.debounceCheck=null,u.interval&&(u.interval=clearInterval(u.interval))}),m(b(u),"check",function(){var e,t,o=u.node;if(!o)return u.state;if(e=function(e){return void 0===e.width&&(e.width=e.right-e.left),void 0===e.height&&(e.height=e.bottom-e.top),e}(u.roundRectDown(o.getBoundingClientRect())),u.props.containment){var n=u.props.containment.getBoundingClientRect();t={top:n.top,left:n.left,bottom:n.bottom,right:n.right}}else t={top:0,left:0,bottom:window.innerHeight||document.documentElement.clientHeight,right:window.innerWidth||document.documentElement.clientWidth};var r=u.props.offset||{};"object"===f(r)&&(t.top+=r.top||0,t.left+=r.left||0,t.bottom-=r.bottom||0,t.right-=r.right||0);var i={top:e.top>=t.top,left:e.left>=t.left,bottom:e.bottom<=t.bottom,right:e.right<=t.right},a=0<e.height&&0<e.width||!u.props.requireContentsSize,l=a&&i.top&&i.left&&i.bottom&&i.right;if(a&&u.props.partialVisibility){var c=e.top<=t.bottom&&e.bottom>=t.top&&e.left<=t.right&&e.right>=t.left;"string"==typeof u.props.partialVisibility&&(c=i[u.props.partialVisibility]),l=u.props.minTopValue?c&&e.top<=t.bottom-u.props.minTopValue:c}"string"==typeof r.direction&&"number"==typeof r.value&&(console.warn("[notice] offset.direction and offset.value have been deprecated. They still work for now, but will be removed in next major version. Please upgrade to the new syntax: { %s: %d }",r.direction,r.value),l=p()(r,e,t));var s=u.state;return u.state.isVisible!==l&&(s={isVisible:l,visibilityRect:i},u.setState(s),u.props.onChange&&u.props.onChange(l)),s}),u.state={isVisible:null,visibilityRect:{}},u}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&&d(e,t)}(t,r.a.Component),function(e,t,o){t&&u(e.prototype,t),o&&u(e,o)}(t,[{key:"componentDidMount",value:function(){this.node=a.a.findDOMNode(this),this.props.active&&this.startWatching()}},{key:"componentWillUnmount",value:function(){this.stopWatching()}},{key:"componentDidUpdate",value:function(e){this.node=a.a.findDOMNode(this),this.props.active&&!e.active?(this.setState({isVisible:null,visibilityRect:{}}),this.startWatching()):this.props.active||this.stopWatching()}},{key:"roundRectDown",value:function(e){return{top:Math.floor(e.top),left:Math.floor(e.left),bottom:Math.floor(e.bottom),right:Math.floor(e.right)}}},{key:"render",value:function(){return this.props.children instanceof Function?this.props.children({isVisible:this.state.isVisible,visibilityRect:this.state.visibilityRect}):r.a.Children.only(this.props.children)}}]),t}();m(y,"defaultProps",{active:!0,partialVisibility:!1,minTopValue:0,scrollCheck:!1,scrollDelay:250,scrollThrottle:-1,resizeCheck:!1,resizeDelay:250,resizeThrottle:-1,intervalCheck:!0,intervalDelay:100,delayedCall:!1,offset:{},containment:null,children:r.a.createElement("span",null),requireContentsSize:!0}),m(y,"propTypes",{onChange:c.a.func,active:c.a.bool,partialVisibility:c.a.oneOfType([c.a.bool,c.a.oneOf(["top","right","bottom","left"])]),delayedCall:c.a.bool,offset:c.a.oneOfType([c.a.shape({top:c.a.number,left:c.a.number,bottom:c.a.number,right:c.a.number}),c.a.shape({direction:c.a.oneOf(["top","right","bottom","left"]),value:c.a.number})]),scrollCheck:c.a.bool,scrollDelay:c.a.number,scrollThrottle:c.a.number,resizeCheck:c.a.bool,resizeDelay:c.a.number,resizeThrottle:c.a.number,intervalCheck:c.a.bool,intervalDelay:c.a.number,containment:"undefined"!=typeof window?c.a.instanceOf(window.Element):c.a.any,children:c.a.oneOfType([c.a.element,c.a.func]),minTopValue:c.a.number,requireContentsSize:c.a.bool})},function(e,t,o){"use strict";var l=o(6);function n(){}function r(){}r.resetWarningCache=n,e.exports=function(){function e(e,t,o,n,r,i){if(i!==l){var a=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types");throw a.name="Invariant Violation",a}}function t(){return e}var o={array:e.isRequired=e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:t,element:e,elementType:e,instanceOf:t,node:e,objectOf:t,oneOf:t,oneOfType:t,shape:t,exact:t,checkPropTypes:r,resetWarningCache:n};return o.PropTypes=o}},function(e,t,o){"use strict";e.exports="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"}],r.c=a,r.d=function(e,t,o){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(r.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)r.d(o,n,function(e){return t[e]}.bind(null,n));return o},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=4);function r(e){if(a[e])return a[e].exports;var t=a[e]={i:e,l:!1,exports:{}};return i[e].call(t.exports,t,t.exports,r),t.l=!0,t.exports}var i,a});