Class ResizeObserver
java.lang.Object
org.vaadin.firitin.util.ResizeObserver
A helper to detect and observe size changes of components. Provides a Java API around the
ResizeObserver JS API.
Allows you for example to easily configure Grid columns for different
devices or swap component implementations based on the screen size/orientation.
When you start to observe a component size, the initial size is reported immediately. So unlike with the Page#addBrowserWindowResizeListener, you don't need to wait for the first resize event or to combine it with the Page#retrieveExtendedClientDetails to get the initial size.
There is one ResizeObserver instance per UI, but your listeners are attached to a specific component.
As the current version of Vaadin does not support extending UI, but this API is designed to be
UI specific, fetch a ResizeObserver instance with of(UI)
or get()
(this uses
UI.getCurrent()
).
There are two ways to listen to size changes:
- Using the
addResizeListener(Component, ComponentEventListener)
method, which is a Vaadin core style API, the listener getsResizeObserver.SizeChangeEvent
. and the return value is aRegistration
you can use to stop listening. - Using the
observe(Component, SizeChangeListener)
method, your listener simply receives theResizeObserver.Dimensions
of the listened component.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final record
A record that describes the size and position of a component.static class
Event fired when the size of a component changes.static interface
A simple listener notified when the size of a component changes. -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.shared.Registration
addResizeListener
(com.vaadin.flow.component.Component component, com.vaadin.flow.component.ComponentEventListener<ResizeObserver.SizeChangeEvent> listener) Adds a listener to be notified when the size of the given component changes.static ResizeObserver
get()
observe
(com.vaadin.flow.component.Component component, ResizeObserver.SizeChangeListener listener) Observe the size of a component.static ResizeObserver
of
(com.vaadin.flow.component.UI ui) unobserve
(com.vaadin.flow.component.Component component, ResizeObserver.SizeChangeListener listener) Stop observing the size of a component.withDebounceTimeout
(int timeout) Set a debounce timeout for the resize events.
-
Method Details
-
of
- Parameters:
ui
- the UI whose ResizeObserver you want to use- Returns:
- the ResizeObserver for the given UI
-
get
- Returns:
- the ResizeObserver for the current UI
-
addResizeListener
public com.vaadin.flow.shared.Registration addResizeListener(com.vaadin.flow.component.Component component, com.vaadin.flow.component.ComponentEventListener<ResizeObserver.SizeChangeEvent> listener) Adds a listener to be notified when the size of the given component changes. Also the initial size is reported immediately.- Parameters:
component
- the component to observelistener
- the listener to be notified- Returns:
- a registration that can be used to stop listening
-
observe
public ResizeObserver observe(com.vaadin.flow.component.Component component, ResizeObserver.SizeChangeListener listener) Observe the size of a component. The listener will be notified when the size of the component changes.- Parameters:
component
- the component to observelistener
- the listener to be notified- Returns:
- this for chaining
-
unobserve
public ResizeObserver unobserve(com.vaadin.flow.component.Component component, ResizeObserver.SizeChangeListener listener) Stop observing the size of a component.- Parameters:
component
- the component to stop observinglistener
- the listener to remove- Returns:
- this for chaining
-
withDebounceTimeout
Set a debounce timeout for the resize events. This can be useful if you want to avoid doing heavy operations on every resize event. The default is 100ms- Parameters:
timeout
- the timeout in milliseconds- Returns:
- this for chaining
-