@ffsm/snapshot
Version:
React snapshot utilities for capturing and measuring DOM elements
2 lines (1 loc) • 1.21 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.SnapshotError=void 0,exports.useSnapshot=function(n,u={}){const{delay:o=0,lowerWidthBound:s=320,lowerHeightBound:i=0,retryInit:c=1,loading:l=!1,error:a=null,onRetry:d,validate:f,observer:h=!0}=u,p=(0,e.useRef)(null),E=(0,e.useRef)(0),N=(0,e.useCallback)(e=>{const t=Math.max(i,0);return!(e.width<s)&&(!(t>0&&e.height<t)&&("function"!=typeof f||f(e)))},[s,i,f]),_=(0,e.useCallback)(()=>{const e=p.current?.parentElement;if(!e)return void n(null,r.PARENT_NOT_FOUND);const u=e.clientWidth,s=e.clientHeight;N({width:u,height:s})?n({width:u,height:s},null):E.current<c?(E.current++,d?.(),(0,t.requestTimeout)(_,o)):n(null,r.INVALID_SIZE)},[o,c,n,d,N]);return(0,e.useEffect)(()=>(E.current=0,(0,t.requestTimeout)(_,o).cancel),[_]),(0,e.useEffect)(()=>{if(!h||l||a||!p.current?.parentElement||"undefined"==typeof ResizeObserver)return;const e=p.current?.parentElement,t=new ResizeObserver(()=>{E.current=0,_()});return t.observe(e),t.disconnect.bind(t)},[l,a,h,_]),p};const e=require("react"),t=require("@ffsm/requester");var r;!function(e){e.PARENT_NOT_FOUND="PARENT_NOT_FOUND",e.INVALID_SIZE="INVALID_SIZE"}(r||(exports.SnapshotError=r={}));