|
| 1 | +import { cn } from "helpers/common.helper"; |
| 2 | +import React, { useState, useRef, useEffect, ReactNode, MutableRefObject } from "react"; |
| 3 | + |
| 4 | +type Props = { |
| 5 | + defaultHeight?: string; |
| 6 | + verticalOffset?: number; |
| 7 | + horizonatlOffset?: number; |
| 8 | + root?: MutableRefObject<HTMLElement | null>; |
| 9 | + children: ReactNode; |
| 10 | + as?: keyof JSX.IntrinsicElements; |
| 11 | + classNames?: string; |
| 12 | + alwaysRender?: boolean; |
| 13 | + placeholderChildren?: ReactNode; |
| 14 | + pauseHeightUpdateWhileRendering?: boolean; |
| 15 | + changingReference?: any; |
| 16 | +}; |
| 17 | + |
| 18 | +const RenderIfVisible: React.FC<Props> = (props) => { |
| 19 | + const { |
| 20 | + defaultHeight = "300px", |
| 21 | + root, |
| 22 | + verticalOffset = 50, |
| 23 | + horizonatlOffset = 0, |
| 24 | + as = "div", |
| 25 | + children, |
| 26 | + classNames = "", |
| 27 | + alwaysRender = false, //render the children even if it is not visble in root |
| 28 | + placeholderChildren = null, //placeholder children |
| 29 | + pauseHeightUpdateWhileRendering = false, //while this is true the height of the blocks are maintained |
| 30 | + changingReference, //This is to force render when this reference is changed |
| 31 | + } = props; |
| 32 | + const [shouldVisible, setShouldVisible] = useState<boolean>(alwaysRender); |
| 33 | + const placeholderHeight = useRef<string>(defaultHeight); |
| 34 | + const intersectionRef = useRef<HTMLElement | null>(null); |
| 35 | + |
| 36 | + const isVisible = alwaysRender || shouldVisible; |
| 37 | + |
| 38 | + // Set visibility with intersection observer |
| 39 | + useEffect(() => { |
| 40 | + if (intersectionRef.current) { |
| 41 | + const observer = new IntersectionObserver( |
| 42 | + (entries) => { |
| 43 | + if (typeof window !== undefined && window.requestIdleCallback) { |
| 44 | + window.requestIdleCallback(() => setShouldVisible(entries[0].isIntersecting), { |
| 45 | + timeout: 300, |
| 46 | + }); |
| 47 | + } else { |
| 48 | + setShouldVisible(entries[0].isIntersecting); |
| 49 | + } |
| 50 | + }, |
| 51 | + { |
| 52 | + root: root?.current, |
| 53 | + rootMargin: `${verticalOffset}% ${horizonatlOffset}% ${verticalOffset}% ${horizonatlOffset}%`, |
| 54 | + } |
| 55 | + ); |
| 56 | + observer.observe(intersectionRef.current); |
| 57 | + return () => { |
| 58 | + if (intersectionRef.current) { |
| 59 | + observer.unobserve(intersectionRef.current); |
| 60 | + } |
| 61 | + }; |
| 62 | + } |
| 63 | + }, [root?.current, intersectionRef, children, changingReference]); |
| 64 | + |
| 65 | + //Set height after render |
| 66 | + useEffect(() => { |
| 67 | + if (intersectionRef.current && isVisible) { |
| 68 | + placeholderHeight.current = `${intersectionRef.current.offsetHeight}px`; |
| 69 | + } |
| 70 | + }, [isVisible, intersectionRef, alwaysRender, pauseHeightUpdateWhileRendering]); |
| 71 | + |
| 72 | + const child = isVisible ? <>{children}</> : placeholderChildren; |
| 73 | + const style = |
| 74 | + isVisible && !pauseHeightUpdateWhileRendering ? {} : { height: placeholderHeight.current, width: "100%" }; |
| 75 | + const className = isVisible ? classNames : cn(classNames, "bg-custom-background-80"); |
| 76 | + |
| 77 | + return React.createElement(as, { ref: intersectionRef, style, className }, child); |
| 78 | +}; |
| 79 | + |
| 80 | +export default RenderIfVisible; |
0 commit comments