UNPKG

react-box-tools

Version:

Box tools react components, utils and hooks

2 lines (1 loc) 739 B
"use strict";const o=require("react");function a(e,{threshold:s=0,root:r=null,rootMargin:n="0%",freezeOnceVisible:t=!1}){const[u,b]=o.useState(),i=(u==null?void 0:u.isIntersecting)&&t,O=([c])=>{b(c)};return o.useEffect(()=>{const c=e==null?void 0:e.current;if(!!!window.IntersectionObserver||i||!c)return;const f={threshold:s,root:r,rootMargin:n},v=new IntersectionObserver(O,f);return v.observe(c),()=>{v.disconnect()}},[e,JSON.stringify(s),r,n,i]),u}function I(e){const s=o.useRef(null);return o.useEffect(()=>{const r=s.current;if(!r)return;const n=new ResizeObserver(t=>{!Array.isArray(t)||!t.length||e(t[0].contentRect)});return n.observe(r),()=>{n.unobserve(r)}},[e]),s}exports.useIntersectionObserver=a;exports.useResizeObserver=I;