UNPKG

@rooks/use-boundingclientrect-ref

Version:

A hook that tracks the boundingclientrect of an element. It returns a callbackRef so that the element node if changed is easily tracked.

3 lines (2 loc) 945 B
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).useBoundingclientrectRef=t(e.React)}(this,(function(e){"use strict";var t={attributes:!0,characterData:!0,subtree:!0,childList:!0};function n(e,t){"function"==typeof e?e(t):e&&(e.current=t)}return function(){const[u,c]=e.useState(null),[l,o]=e.useState(null),s=e.useCallback((()=>{c(l?l.getBoundingClientRect():null)}),[l]);e.useEffect((()=>{s()}),[l]);const i=e.useCallback((e=>{o(e)}),[]),[r]=function(n,u=t){const[c,l]=e.useState(null);return e.useEffect((()=>{if(c){const e=new MutationObserver(n);return e.observe(c,u),()=>{e.disconnect()}}}),[c,n,u]),[e.useCallback((e=>{l(e)}),[])]}(s);var f,a;return[(f=i,a=r,e.useMemo((()=>null==f&&null==a?null:e=>{n(f,e),n(a,e)}),[f,a])),u,s]}})); //# sourceMappingURL=index.min.js.map