UNPKG

react-hooks-visible

Version:

react intersectionObserver helper library.

2 lines (1 loc) 907 B
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self)["react-hooks-visible"]={},e.React)}(this,(function(e,t){"use strict";function r(){return(r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}const n={root:null,rootMargin:"0px",threshold:Array.from({length:100},(e,t)=>.01*t)};e.useVisible=(e,o={})=>{const u=t.useRef(null),c=t.useRef(null),[s,i]=t.useState(0),f="function"==typeof e?e(s):s,a=t.useCallback(e=>{e.forEach(e=>{i(e.intersectionRatio)})},[]);return t.useEffect(()=>{c.current||u.current&&(c.current=new IntersectionObserver(a,r({},n,{},o)),c.current.observe(u.current))}),[u,f]},Object.defineProperty(e,"__esModule",{value:!0})}));