Class IntersectionObserver

java.lang.Object
org.vaadin.firitin.util.IntersectionObserver

public class IntersectionObserver extends Object
A helper to detect when components become visible in a scrollable area or the viewport. Provides a Java API around the IntersectionObserver JS API. Useful for lazy loading, infinite scroll, visibility tracking, and triggering actions when elements scroll into view.

There is one IntersectionObserver instance per UI (for viewport observation) or per root component. Fetch a viewport-level observer with of(UI) or get(). For observing intersection with a specific scrollable container, use of(UI, Component).

  • Method Details

    • of

      public static IntersectionObserver of(com.vaadin.flow.component.UI ui)
      Returns a viewport-level IntersectionObserver for the given UI.
      Parameters:
      ui - the UI
      Returns:
      the IntersectionObserver for the given UI
    • get

      public static IntersectionObserver get()
      Returns a viewport-level IntersectionObserver for the current UI.
      Returns:
      the IntersectionObserver for the current UI
    • of

      public static IntersectionObserver of(com.vaadin.flow.component.UI ui, com.vaadin.flow.component.Component root)
      Creates an IntersectionObserver with a specific root component as the intersection root. This is useful for detecting visibility within a scrollable container.
      Parameters:
      ui - the UI
      root - the root component (scrollable container)
      Returns:
      a new IntersectionObserver with the given root
    • observe

      public IntersectionObserver observe(com.vaadin.flow.component.Component component, IntersectionObserver.IntersectionListener listener)
      Observe the intersection state of a component. The listener will be called when the component's visibility relative to the root changes.
      Parameters:
      component - the component to observe
      listener - the listener to be notified
      Returns:
      this for chaining
    • unobserve

      public IntersectionObserver unobserve(com.vaadin.flow.component.Component component, IntersectionObserver.IntersectionListener listener)
      Stop observing a specific listener for a component.
      Parameters:
      component - the component
      listener - the listener to remove
      Returns:
      this for chaining
    • unobserve

      public IntersectionObserver unobserve(com.vaadin.flow.component.Component component)
      Stop observing a component entirely.
      Parameters:
      component - the component to stop observing
      Returns:
      this for chaining
    • withRootMargin

      public IntersectionObserver withRootMargin(String rootMargin)
      Set the root margin for the intersection observer. This can expand or shrink the root's bounding box before computing intersections. Must be called before observe(Component, IntersectionListener).
      Parameters:
      rootMargin - the root margin (e.g. "10px", "10px 20px", "10% 20%")
      Returns:
      this for chaining
    • withThresholds

      public IntersectionObserver withThresholds(double... thresholds)
      Set the thresholds at which the observer callback fires. Must be called before observe(Component, IntersectionListener).
      Parameters:
      thresholds - visibility ratio thresholds (0.0 to 1.0)
      Returns:
      this for chaining
    • withDebounceTimeout

      public IntersectionObserver withDebounceTimeout(int timeout)
      Set the debounce timeout for intersection events sent to the server. Intersection changes are accumulated on the client side and flushed to the server after this timeout, reducing server roundtrips during rapid scrolling. Only the latest state per element is sent. The default is 100ms.
      Parameters:
      timeout - the timeout in milliseconds
      Returns:
      this for chaining