optically-size-images
Version:
Normalize the size of a group of images, optically, based on their aspect ratios
2 lines (1 loc) • 826 B
JavaScript
import{calculateOpticalImageSize as d,imageIsReady as R}from"../../../lib/index.js";import{useRef as i,useState as g,useEffect as p,useCallback as b}from"react";const S=n=>{const c=i(null),[u,a]=g(!1),[o,f]=g(1),r=i(!0),t=i(null);p(()=>(r.current=!0,()=>{r.current=!1,t.current&&t.current.abort()}),[]),p(()=>{typeof n=="function"&&c.current&&u&&n(c.current,o)},[n,u,o]);const l=b(()=>{if(!(!r.current||!c.current))try{f(d(c.current)),a(!0)}catch(e){r.current&&console.error("Error calculating optical image size:",e)}},[]);return{imgRef:b(e=>{if(t.current&&t.current.abort(),c.current=e,!e||!r.current)return;t.current=new AbortController;const s=t.current.signal;R(e)?r.current&&!s.aborted&&(a(!0),f(d(e))):r.current&&!s.aborted&&(a(!1),e.addEventListener("load",l,{signal:s}))},[l]),ready:u,scale:o}};export{S as default};