3 lines
11 KiB
JavaScript
3 lines
11 KiB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):(t=t||self).useIntersectionObserver=e(t.React)}(this,(function(t){"use strict";function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=new Array(e);e>n;n++)o[n]=t[n];return o}function n(){return this.intersectionRatio>0}function o(t){return t.value+t.unit}function r(t){t._checkForIntersections()}function i(t){if(!this._observationTargets.some((function(e){return e.element==t}))){if(!t||1!=t.nodeType)throw new Error("target must be an Element");this._registerInstance(),this._observationTargets.push({element:t,entry:null}),this._monitorIntersections(t.ownerDocument),this._checkForIntersections()}}function s(t){this._observationTargets=this._observationTargets.filter((function(e){return e.element!=t})),this._unmonitorIntersections(t.ownerDocument),0==this._observationTargets.length&&this._unregisterInstance()}function u(){this._observationTargets=[],this._unmonitorAllIntersections(),this._unregisterInstance()}function c(){var t=this._queuedEntries.slice();return this._queuedEntries=[],t}function h(t,e,n){if("number"!=typeof t||isNaN(t)||0>t||t>1)throw new Error("threshold must be a number between 0 and 1 inclusively");return t!==n[e-1]}function a(t){var e=t||[0];return Array.isArray(e)||(e=[e]),e.sort().filter(h)}function l(t){var e=/^(-?\d*\.?\d+)(px|%)$/.exec(t);if(!e)throw new Error("rootMargin must be specified in pixels or percent");return{value:parseFloat(e[1]),unit:e[2]}}function f(t){var e=(t||"0px").split(/\s+/).map(l);return e[1]=e[1]||e[0],e[2]=e[2]||e[0],e[3]=e[3]||e[1],e}function d(){var t=this._monitoringUnsubscribes.slice(0);this._monitoringDocuments.length=0,this._monitoringUnsubscribes.length=0;for(var e=0;e<t.length;e++)t[e]()}function p(t){var e=this._rootMarginValues.map((function(e,n){return"px"==e.unit?e.value:e.value*(n%2?t.width:t.height)/100})),n={top:t.top-e[0],right:t.right+e[1],bottom:t.bottom+e[2],left:t.left-e[3]};return n.width=n.right-n.left,n.height=n.bottom-n.top,n}function v(t,e){var n=t&&t.isIntersecting?t.intersectionRatio||0:-1,o=e.isIntersecting?e.intersectionRatio||0:-1;if(n!==o)for(var r=0;r<this.thresholds.length;r++){var i=this.thresholds[r];if(i==n||i==o||n>i!=o>i)return 1}}function g(t,e,n,o){"function"==typeof t.addEventListener?t.addEventListener(e,n,o||0):"function"==typeof t.attachEvent&&t.attachEvent("on"+e,n)}function m(t,e,n,o){"function"==typeof t.removeEventListener?t.removeEventListener(e,n,o||0):"function"==typeof t.detatchEvent&&t.detatchEvent("on"+e,n)}function b(t){return!t||"x"in t?t:{top:t.top,y:t.top,bottom:t.bottom,left:t.left,x:t.left,right:t.right,width:t.width,height:t.height}}function y(t,e){var n=e.top-t.top,o=e.left-t.left;return{top:n,left:o,height:e.height,width:e.width,bottom:n+e.height,right:o+e.width}}function _(t){try{return t.defaultView&&t.defaultView.frameElement||null}catch(t){return null}}function w(t){var n=t.root,o=function(t,e){if(null==t)return{};var n,o,r={},i=Object.keys(t);for(o=0;o<i.length;o++)n=i[o],0>e.indexOf(n)&&(r[n]=t[n]);return r}(t,["root"]),r=JSON.stringify(o),i=E.get(n);return i||(i={},E.set(n,i)),i[r]?i[r]:i[r]=function(t){var n=t.root,o=void 0===n?null:n,r=t.pollInterval,i=void 0===r?null:r,s=t.useMutationObserver,u=void 0===s?0:s,c=t.rootMargin,h=void 0===c?"0px 0px 0px 0px":c,a=t.threshold,l=void 0===a?0:a,f=new Set;if("undefined"==typeof IntersectionObserver)return null;var d=new IntersectionObserver((function(t){for(var n,o=function(t,n){var o;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(o=function(t){if(t){if("string"==typeof t)return e(t,void 0);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(t,void 0):void 0}}(t))||n&&t&&"number"==typeof t.length){o&&(t=o);var r=0;return function(){return r<t.length?{done:0,value:t[r++]}:{done:1}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(o=t[Symbol.iterator]()).next.bind(o)}(f);!(n=o()).done;)(0,n.value)(t,d)}),{root:o,rootMargin:h,threshold:l});return d.POLL_INTERVAL=i,d.USE_MUTATION_OBSERVER=u,{observer:d,getListeners:function(){return f},subscribe:function(t){return f.add(t)},unsubscribe:function(t){return f.delete(t)}}}(t)}!function(){function t(t){this.time=t.time,this.target=t.target,this.rootBounds=b(t.rootBounds),this.boundingClientRect=b(t.boundingClientRect),this.intersectionRect=b(t.intersectionRect||{top:0,bottom:0,left:0,right:0,width:0,height:0}),this.isIntersecting=!!t.intersectionRect;var e=this.boundingClientRect,n=e.width*e.height,o=this.intersectionRect,r=o.width*o.height;this.intersectionRatio=n?Number((r/n).toFixed(4)):this.isIntersecting?1:0}function e(t,e){var n=e||{};if("function"!=typeof t)throw new Error("callback must be a function");if(n.root&&1!=n.root.nodeType)throw new Error("root must be an Element");this._checkForIntersections=function(t,e){function n(){t(),o=null}var o=null;return function(){o||(o=setTimeout(n,e))}}(this._checkForIntersections.bind(this),this.THROTTLE_TIMEOUT),this._callback=t,this._observationTargets=[],this._queuedEntries=[],this._rootMarginValues=this._parseRootMargin(n.rootMargin),this.thresholds=this._initThresholds(n.threshold),this.root=n.root||null,this.rootMargin=this._rootMarginValues.map(o).join(" "),this._monitoringDocuments=[],this._monitoringUnsubscribes=[]}function h(t,e){O=t&&e?y(t,e):{top:0,bottom:0,left:0,right:0,width:0,height:0},R.forEach(r)}function l(t){var e;try{e=t.getBoundingClientRect()}catch(t){}return e?(e.width&&e.height||(e={top:e.top,right:e.right,bottom:e.bottom,left:e.left,width:e.right-e.left,height:e.bottom-e.top}),e):{top:0,bottom:0,left:0,right:0,width:0,height:0}}function w(t,e){for(var n=e;n;){if(n==t)return 1;n=I(n)}return 0}function I(t){var e=t.parentNode;return 9==t.nodeType&&t!=E?_(t):e&&11==e.nodeType&&e.host?e.host:e&&e.assignedSlot?e.assignedSlot.parentNode:e}if("object"==typeof window)if("IntersectionObserver"in window&&"IntersectionObserverEntry"in window&&"intersectionRatio"in window.IntersectionObserverEntry.prototype)"isIntersecting"in window.IntersectionObserverEntry.prototype||Object.defineProperty(window.IntersectionObserverEntry.prototype,"isIntersecting",{get:n});else{var E=window.document,R=[],T=null,O=null;e.prototype.THROTTLE_TIMEOUT=100,e.prototype.POLL_INTERVAL=null,e.prototype.USE_MUTATION_OBSERVER=1,e._setupCrossOriginUpdater=function(){return T||(T=h),T},e._resetCrossOriginUpdater=function(){T=null,O=null},e.prototype.observe=i,e.prototype.unobserve=s,e.prototype.disconnect=u,e.prototype.takeRecords=c,e.prototype._initThresholds=a,e.prototype._parseRootMargin=f,e.prototype._monitorIntersections=function(t){var e=t.defaultView;if(e&&-1==this._monitoringDocuments.indexOf(t)){var n=this._checkForIntersections,o=null,r=null;if(this.POLL_INTERVAL?o=e.setInterval(n,this.POLL_INTERVAL):(g(e,"resize",n,1),g(t,"scroll",n,1),this.USE_MUTATION_OBSERVER&&"MutationObserver"in e&&(r=new e.MutationObserver(n)).observe(t,{attributes:1,childList:1,characterData:1,subtree:1})),this._monitoringDocuments.push(t),this._monitoringUnsubscribes.push((function(){var e=t.defaultView;e&&(o&&e.clearInterval(o),m(e,"resize",n,1)),m(t,"scroll",n,1),r&&r.disconnect()})),t!=(this.root&&this.root.ownerDocument||E)){var i=_(t);i&&this._monitorIntersections(i.ownerDocument)}}},e.prototype._unmonitorIntersections=function(t){var e=this._monitoringDocuments.indexOf(t);if(-1!=e){var n=this.root&&this.root.ownerDocument||E;if(!this._observationTargets.some((function(e){var o=e.element.ownerDocument;if(o==t)return 1;for(;o&&o!=n;){var r=_(o);if((o=r&&r.ownerDocument)==t)return 1}return 0}))){var o=this._monitoringUnsubscribes[e];if(this._monitoringDocuments.splice(e,1),this._monitoringUnsubscribes.splice(e,1),o(),t!=n){var r=_(t);r&&this._unmonitorIntersections(r.ownerDocument)}}}},e.prototype._unmonitorAllIntersections=d,e.prototype._checkForIntersections=function(){if(this.root||!T||O){var e=this._rootIsInDom(),n=e?this._getRootRect():{top:0,bottom:0,left:0,right:0,width:0,height:0};this._observationTargets.forEach((function(o){var r=o.element,i=l(r),s=this._rootContainsTarget(r),u=o.entry,c=e&&s&&this._computeTargetAndRootIntersection(r,i,n),h=o.entry=new t({time:window.performance&&performance.now&&performance.now(),target:r,boundingClientRect:i,rootBounds:T&&!this.root?null:n,intersectionRect:c});u?e&&s?this._hasCrossedThreshold(u,h)&&this._queuedEntries.push(h):u&&u.isIntersecting&&this._queuedEntries.push(h):this._queuedEntries.push(h)}),this),this._queuedEntries.length&&this._callback(this.takeRecords(),this)}},e.prototype._computeTargetAndRootIntersection=function(t,e,n){if("none"!=window.getComputedStyle(t).display){for(var o,r,i,s,u,c,h,a,f=e,d=I(t),p=0;!p&&d;){var v=null,g=1==d.nodeType?window.getComputedStyle(d):{};if("none"==g.display)return null;if(d==this.root||9==d.nodeType)if(p=1,d==this.root||d==E)T&&!this.root?!O||0==O.width&&0==O.height?(d=null,v=null,f=null):v=O:v=n;else{var m=I(d),b=m&&l(m),_=m&&this._computeTargetAndRootIntersection(m,b,n);b&&_?(d=m,v=y(b,_)):(d=null,f=null)}else{var w=d.ownerDocument;d!=w.body&&d!=w.documentElement&&"visible"!=g.overflow&&(v=l(d))}if(v&&(o=v,r=f,void 0,void 0,void 0,void 0,void 0,void 0,i=Math.max(o.top,r.top),s=Math.min(o.bottom,r.bottom),u=Math.max(o.left,r.left),a=s-i,f=(h=(c=Math.min(o.right,r.right))-u)>=0&&a>=0&&{top:i,bottom:s,left:u,right:c,width:h,height:a}||null),!f)break;d=d&&I(d)}return f}},e.prototype._getRootRect=function(){var t;if(this.root)t=l(this.root);else{var e=E.documentElement,n=E.body;t={top:0,left:0,right:e.clientWidth||n.clientWidth,width:e.clientWidth||n.clientWidth,bottom:e.clientHeight||n.clientHeight,height:e.clientHeight||n.clientHeight}}return this._expandRectByRootMargin(t)},e.prototype._expandRectByRootMargin=p,e.prototype._hasCrossedThreshold=v,e.prototype._rootIsInDom=function(){return!this.root||w(E,this.root)},e.prototype._rootContainsTarget=function(t){return w(this.root||E,t)&&(!this.root||this.root.ownerDocument==t.ownerDocument)},e.prototype._registerInstance=function(){0>R.indexOf(this)&&R.push(this)},e.prototype._unregisterInstance=function(){var t=R.indexOf(this);-1!=t&&R.splice(t,1)},window.IntersectionObserver=e,window.IntersectionObserverEntry=t}}();var I=t["undefined"!=typeof document&&void 0!==document.createElement?"useLayoutEffect":"useEffect"],E=new Map;return function(e,n){void 0===n&&(n={});var o=n,r=o.root,i=void 0===r?null:r,s=o.pollInterval,u=void 0===s?null:s,c=o.useMutationObserver,h=void 0===c?0:c,a=o.rootMargin,l=void 0===a?"0px 0px 0px 0px":a,f=o.threshold,d=void 0===f?0:f,p=o.initialIsIntersecting,v=void 0===p?0:p,g=t.useState((function(){return{boundingClientRect:null,intersectionRatio:0,intersectionRect:null,isIntersecting:v,rootBounds:null,target:null,time:0}})),m=g[0],b=g[1],y=t.useState((function(){return w({root:i,pollInterval:u,useMutationObserver:h,rootMargin:l,threshold:d})})),_=y[0],E=y[1];return t.useEffect((function(){var t=w({root:i,pollInterval:u,useMutationObserver:h,rootMargin:l,threshold:d});E(t)}),[i,l,u,h,JSON.stringify(d)]),I((function(){var t=e&&"current"in e?e.current:e;if(_&&t){var n=0;_.observer.observe(t);var o=function(e){if(!n)for(var o=0;o<e.length;o++){var r=e[o];r.target===t&&b(r)}};return _.subscribe(o),function(){n=1,_.observer.unobserve(t),_.unsubscribe(o)}}}),[e,_]),m}}));
|
|
//# sourceMappingURL=use-intersection-observer.js.map
|