({
lazy = true,
children,
fallback = null,
className,
viewportOptions,
disconnectOnLeave = true,
}: LazyComponentProps)
| 45 | * Uses intersection observer under the hood for efficient viewport detection. |
| 46 | */ |
| 47 | export const LazyComponent = ({ |
| 48 | lazy = true, |
| 49 | children, |
| 50 | fallback = null, |
| 51 | className, |
| 52 | viewportOptions, |
| 53 | disconnectOnLeave = true, |
| 54 | }: LazyComponentProps) => { |
| 55 | const ref = useRef<HTMLDivElement>(null); |
| 56 | const once = useRef(false); |
| 57 | |
| 58 | const { inViewport } = useInViewport( |
| 59 | ref as RefObject<HTMLElement>, |
| 60 | viewportOptions, |
| 61 | { |
| 62 | disconnectOnLeave, |
| 63 | }, |
| 64 | ); |
| 65 | |
| 66 | useEffect(() => { |
| 67 | if (inViewport) { |
| 68 | once.current = true; |
| 69 | } |
| 70 | }, [inViewport]); |
| 71 | |
| 72 | const shouldRender = lazy ? once.current || inViewport : true; |
| 73 | |
| 74 | return ( |
| 75 | <div ref={ref} className={className}> |
| 76 | {shouldRender ? children : (fallback ?? <div />)} |
| 77 | </div> |
| 78 | ); |
| 79 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected