@guruhotel/aura-hooks
Version:
🪝 Hooks library designed by the Guruhotel team for Aura UI
1 lines • 668 B
JavaScript
import{useEffect as e,useMemo as t,useRef as n,useState as r}from"react";const c={x:0,y:0,width:0,height:0,top:0,left:0,bottom:0,right:0},i="undefined"!=typeof window;export function useResizeObserver(){const o=n(0),u=n(null),[s,l]=r(c),h=t((()=>i?new ResizeObserver((e=>{const t=e[0];t&&(cancelAnimationFrame(o.current),o.current=requestAnimationFrame((()=>{u.current&&l(t.contentRect)})))})):null),[]);return e((()=>(u.current&&(null==h||h.observe(u.current)),()=>{null==h||h.disconnect(),o.current&&cancelAnimationFrame(o.current)})),[u.current]),[u,s]}export function useElementSize(){const[e,{width:t,height:n}]=useResizeObserver();return{ref:e,width:t,height:n}}