Build 533169stable
Module 830178
2,739 characters · in no known chunkDependencies
Source Raw
function(e,t,n){"use strict";n.d(t,{W:()=>u,t:()=>c});var r=n(627968),i=n(64700),s=n(121894);let a=100,o=.7;class l{registeredNodes=new Map;visibleComponents=new Set;animatingComponents=new Set;observer;constructor(e=!1){if(e)return;this.observer=new window.IntersectionObserver(e=>{(0,s.r)(()=>{e.forEach(e=>{let t=this.registeredNodes.get(e.target);null==t||(e.intersectionRatio>=o?this.handleVisible(e,t):this.handleNotVisible(e,t))})})},{threshold:o})}handleVisible(e,t){if(this.visibleComponents.has(e.target))return;let n=Math.abs(e.intersectionRect.bottom-Number(e.rootBounds?.bottom))<Math.abs(e.intersectionRect.top-Number(e.rootBounds?.top));n?this.visibleComponents=new Set([e.target,...this.visibleComponents]):this.visibleComponents.add(e.target);let r=n||this.animatingComponents.size<a;r?this.animatingComponents.add(e.target):this.animatingComponents.delete(e.target),t.updateState(r),r&&this.visibleComponents.size>a&&this.stopNodeFromAnimating()}handleNotVisible(e,t){this.visibleComponents.has(e.target)&&(t.updateState(!1),this.visibleComponents.delete(e.target),this.animatingComponents.delete(e.target),this.potentiallyAnimateNewNode())}stopNodeFromAnimating(){let e=Array.from(this.visibleComponents);for(let t=e.length-1;t>=0;t--){let n=e[t];if(this.animatingComponents.has(n)){let e=this.registeredNodes.get(n);e?.updateState(!1),this.animatingComponents.delete(n);return}}}potentiallyAnimateNewNode(){if(this.animatingComponents.size<a&&this.visibleComponents.size>this.animatingComponents.size){for(let e of this.visibleComponents)if(!this.animatingComponents.has(e)){let t=this.registeredNodes.get(e);t?.updateState(!0),this.animatingComponents.add(e);return}}}registerNode(e,t){if(this.registeredNodes.has(e))throw Error("ThoughtfullyAnimated.registeredNode: Unable to register an already registered node...");return this.registeredNodes.set(e,{updateState:t}),this.observer?.observe(e),()=>{this.unregisterNode(e)}}unregisterNode(e){this.registeredNodes.delete(e),this.animatingComponents.delete(e),this.visibleComponents.delete(e),this.observer?.unobserve(e),this.potentiallyAnimateNewNode()}cleanUp(){this.observer?.disconnect(),this.registeredNodes.clear(),this.visibleComponents.clear()}}let u=i.createContext({manager:new l(!0),useThoughtfullyAnimated:()=>({animate:!0,registerRef:()=>{}})});function c(e){let{children:t}=e,[n]=i.useState(()=>{let e=new l;return{manager:e,useThoughtfullyAnimated(){let t=i.useRef(null),[n,r]=i.useState(!1);return{animate:n,registerRef:i.useCallback(n=>{null!=t.current&&e.unregisterNode(t.current),t.current=n,null!=t.current&&e.registerNode(t.current,r)},[])}}}});return i.useEffect(()=>()=>n.manager.cleanUp(),[n.manager]),(0,r.jsx)(u.Provider,{value:n,children:t})}}