| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 
 | import * as React from 'react';
 export default function useTopDistance<T extends HTMLDivElement>(defaultInstance = 24) {
 const divRef = React.useRef<T>(null);
 const [height, setHeight] = React.useState(0);
 
 const getElementInstanceToTop = (el: HTMLElement | null): number => {
 if (el?.offsetParent) {
 return getElementInstanceToTop(el?.offsetParent as HTMLElement) + el.offsetTop + el.scrollTop;
 }
 return el ? el.offsetTop : 0;
 };
 
 const calculateHeight = React.useCallback(() => {
 const { clientHeight } = document.body;
 const offsetParentTop = getElementInstanceToTop(divRef.current);
 const result = clientHeight - offsetParentTop ?? 0;
 setHeight(result - defaultInstance);
 }, []);
 
 React.useLayoutEffect(() => {
 window.requestAnimationFrame(calculateHeight);
 window.addEventListener('resize', calculateHeight);
 return () => {
 window.removeEventListener('resize', calculateHeight);
 };
 }, []);
 
 return {
 divRef,
 height,
 calculateHeight,
 
 };
 }
 
 
 |