Class IntersectionObserver
java.lang.Object
org.vaadin.firitin.util.IntersectionObserver
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).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordInformation about a component's intersection with the root.static interfaceA listener notified when a component's intersection state changes. -
Method Summary
Modifier and TypeMethodDescriptionstatic IntersectionObserverget()Returns a viewport-level IntersectionObserver for the current UI.observe(com.vaadin.flow.component.Component component, IntersectionObserver.IntersectionListener listener) Observe the intersection state of a component.static IntersectionObserverof(com.vaadin.flow.component.UI ui) Returns a viewport-level IntersectionObserver for the given UI.static IntersectionObserverof(com.vaadin.flow.component.UI ui, com.vaadin.flow.component.Component root) Creates an IntersectionObserver with a specific root component as the intersection root.unobserve(com.vaadin.flow.component.Component component) Stop observing a component entirely.unobserve(com.vaadin.flow.component.Component component, IntersectionObserver.IntersectionListener listener) Stop observing a specific listener for a component.withDebounceTimeout(int timeout) Set the debounce timeout for intersection events sent to the server.withRootMargin(String rootMargin) Set the root margin for the intersection observer.withThresholds(double... thresholds) Set the thresholds at which the observer callback fires.
-
Method Details
-
of
Returns a viewport-level IntersectionObserver for the given UI.- Parameters:
ui- the UI- Returns:
- the IntersectionObserver for the given UI
-
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 UIroot- 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 observelistener- 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 componentlistener- the listener to remove- Returns:
- this for chaining
-
unobserve
Stop observing a component entirely.- Parameters:
component- the component to stop observing- Returns:
- this for chaining
-
withRootMargin
Set the root margin for the intersection observer. This can expand or shrink the root's bounding box before computing intersections. Must be called beforeobserve(Component, IntersectionListener).- Parameters:
rootMargin- the root margin (e.g. "10px", "10px 20px", "10% 20%")- Returns:
- this for chaining
-
withThresholds
Set the thresholds at which the observer callback fires. Must be called beforeobserve(Component, IntersectionListener).- Parameters:
thresholds- visibility ratio thresholds (0.0 to 1.0)- Returns:
- this for chaining
-
withDebounceTimeout
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
-