Function to call when the element enters the viewport
Configuration options
A ref object to attach to the element that should be observed
import { useInViewObserver } from '@consumidor-positivo/ts-utils/hooks';
function ScrollAnimation() {
const handleInView = () => {
console.log('Element is in view!');
// Trigger animation, load more content, etc.
};
const observerRef = useInViewObserver(handleInView, { triggerOnce: true });
return (
<div ref={observerRef}>
This content will trigger the callback when it enters the viewport.
</div>
);
}
Note: Automated testing for this hook is challenging due to the need to mock the IntersectionObserver behavior. It has been manually tested in real components.
A hook that triggers a callback when an element enters the viewport.