(null);
+ const renderedHeight = useRef(0);
+ const inView = useRef(false);
const parent = props.parentRef.current;
const md = mdRef.current;
- const rendered = useRef(true); // disable lazy loading for bad ux
- const [counter, setCounter] = useState(0);
- useEffect(() => {
- // to triggr rerender
- setCounter(counter + 1);
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [props.loading]);
+ if (parent && md) {
+ const parentBounds = parent.getBoundingClientRect();
+ const twoScreenHeight = Math.max(500, parentBounds.height * 2);
+ const mdBounds = md.getBoundingClientRect();
+ const isInRange = (x: number) =>
+ x <= parentBounds.bottom + twoScreenHeight &&
+ x >= parentBounds.top - twoScreenHeight;
+ inView.current = isInRange(mdBounds.top) || isInRange(mdBounds.bottom);
+ }
- const inView =
- rendered.current ||
- (() => {
- if (parent && md) {
- const parentBounds = parent.getBoundingClientRect();
- const mdBounds = md.getBoundingClientRect();
- const isInRange = (x: number) =>
- x <= parentBounds.bottom && x >= parentBounds.top;
- const inView = isInRange(mdBounds.top) || isInRange(mdBounds.bottom);
-
- if (inView) {
- rendered.current = true;
- }
-
- return inView;
- }
- })();
-
- const shouldLoading = props.loading || !inView;
+ if (inView.current && md) {
+ renderedHeight.current = Math.max(
+ renderedHeight.current,
+ md.getBoundingClientRect().height,
+ );
+ }
return (
0
+ ? renderedHeight.current
+ : "auto",
+ }}
ref={mdRef}
onContextMenu={props.onContextMenu}
onDoubleClickCapture={props.onDoubleClickCapture}
>
- {shouldLoading ? (
-
- ) : (
-
- {props.content}
-
- )}
+ {inView.current &&
+ (props.loading ? (
+
+ ) : (
+
+ ))}
);
}