@consumidor-positivo/ts-utils - v1.13.0
    Preparing search index...

    Function useInViewObserver

    • A hook that triggers a callback when an element enters the viewport.

      Parameters

      • callback: () => void

        Function to call when the element enters the viewport

      • options: UseInViewObserverOptions = ...

        Configuration options

      Returns RefObject<null | HTMLElement>

      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.