Skip to content

Commit

Permalink
add useIsVisible hook for table height
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi-sl committed Aug 23, 2024
1 parent f60ffa6 commit 87d1132
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Containers/NetworkTableContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useTheme } from '../state/theme/Context';
import InputHAR from '../Components/Import/InputHAR';
import NetworkTableBody from '../Components/NetworkTable/NetworkTableBody';
import { TABLE_HEADER_HEIGHT } from '../constants';
import { useIsVisible } from '../hooks/useIsVisible';

const context = classNames.bind(Styles);

Expand All @@ -22,12 +23,13 @@ const NetworkTableContainer = () => {

const [tableBodyHeight, setTableBodyHeight] = useState(0);
const ref = useRef(null);
const isVisible = useIsVisible(ref);

useEffect(() => {
if (ref?.current) {
if (ref?.current && isVisible) {
setTableBodyHeight(ref.current.clientHeight - TABLE_HEADER_HEIGHT);
}
}, [ref, actualData]);
}, [ref, actualData, isVisible]);

if (error) {
return (
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/useIsVisible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect, useMemo, useState } from 'react';

export const useIsVisible = (elementRef) => {
const [isIntersecting, setIntersecting] = useState(false);

const observer = useMemo(() => new IntersectionObserver(
([entry]) => setIntersecting(entry.isIntersecting),
), [elementRef]);

useEffect(() => {
if (elementRef?.current) {
observer.observe(elementRef.current);
}

return () => observer.disconnect();
}, []);

return isIntersecting;
};

0 comments on commit 87d1132

Please sign in to comment.