HEX
Server: LiteSpeed
System: Linux server318.web-hosting.com 4.18.0-513.18.1.lve.el8.x86_64 #1 SMP Thu Feb 22 12:55:50 UTC 2024 x86_64
User: joyfejor (3859)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: /home/joyfejor/www/wp-content/plugins/extendify/src/Agent/components/ScrollIntoViewOnce.jsx
import { useRef, useEffect } from 'react';

export const ScrollIntoViewOnce = ({ children, ...props }) => {
	const ref = useRef(null);
	const once = useRef(false);

	useEffect(() => {
		if (!ref.current || once.current) return;
		const c = ref.current;
		// only scroll if 50% isnt visible
		const rect = c.getBoundingClientRect();
		const windowHeight =
			window.innerHeight || document.documentElement.clientHeight;
		const elementHeight = rect.height;
		const visibleTop = Math.max(rect.top, 0);
		const visibleBottom = Math.min(rect.bottom, windowHeight);
		const visibleHeight = Math.max(0, visibleBottom - visibleTop);
		const visibleRatio = visibleHeight / elementHeight;

		if (visibleRatio >= 0.5) return;
		c.scrollIntoView({ behavior: 'smooth', block: 'end' });
		once.current = true;
	}, [props]);

	return (
		<div ref={ref} {...props}>
			{children}
		</div>
	);
};