File size: 719 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import { useEffect, useState } from 'react';
import { calculateMetricsSectionScrollOffset } from '../utils/calculate-metrics-section-scroll-offset';
export function useIsMenuSectionVisible( ref: React.RefObject< HTMLObjectElement > | undefined ) {
const [ isIntersecting, setIntersecting ] = useState( false );
useEffect( () => {
const observer = new IntersectionObserver(
( [ entry ] ) => {
setIntersecting( entry.isIntersecting );
},
{ rootMargin: `-${ calculateMetricsSectionScrollOffset() + 1 }px` } // the additional pixel triggers the tab change
);
ref?.current && observer.observe( ref.current );
return () => {
observer.disconnect();
};
}, [ ref ] );
return isIntersecting;
}
|