Rob
Rob

Reputation: 6487

How to get browser window width in Vaadin 10 in server-side Java code

In Vaadin 8, I can get the size of the window like this:

Page.getCurrent().getBrowserWindowWidth();

In Vaadin 10, Page appears to no longer have the getBrowserWindowWidth method.

What is the proper way to determine the width of the browser window in Vaadin 10?

Upvotes: 3

Views: 1804

Answers (1)

Leif Åstrand
Leif Åstrand

Reputation: 8001

There is currently no proper feature for this in Vaadin 10. We have two open tickets related to this feature: one about making generic information available and one specifically about the window size.

The best way of doing something that depends on the browser size is probably to implement that part using either JavaScript or a CSS Media Query. If you really need to know the size on the server, you'd need to use Page.exectueJavaScript to manually extract the numbers in the browser and pass them back to the server using e.g. @ClientCallable on a component method.

It should also be pointed out that even if this is implemented, it wouldn't work exactly as well as in Vaadin 7 and 8 because those versions slow down the page bootstrap to wait for e.g. sizing information to be available before running UI.init, whereas Vaadin 10 will construct the initial UI contents even before the browser has had time to send the sizing information to the server.

Upvotes: 3

Related Questions