Scroller

Scroller is a component container for creating scrollable areas in the UI.

Open in a
new tab
// Personal information
H3 personalTitle = new H3("Personal information");
personalTitle.setId(PERSONAL_TITLE_ID);

TextField firstName = new TextField("First name");
firstName.setWidthFull();

TextField lastName = new TextField("Last name");
lastName.setWidthFull();

DatePicker birthDate = new DatePicker("Birthdate");
birthDate.setInitialPosition(LocalDate.of(1990, 1, 1));
birthDate.setWidthFull();

Section personalInformation = new Section(personalTitle, firstName, lastName, birthDate);
personalInformation.getElement().setAttribute("aria-labelledby", PERSONAL_TITLE_ID);

// Employment information
H3 employementTitle = new H3("Employment information");
employementTitle.setId(EMPLOYMENT_TITLE_ID);

TextField position = new TextField("Position");
position.setWidthFull();

TextArea additionalInformation = new TextArea("Additional Information");
additionalInformation.setWidthFull();

Section employmentInformation = new Section(employementTitle, position, additionalInformation);
employmentInformation.getElement().setAttribute("aria-labelledby", EMPLOYMENT_TITLE_ID);

// NOTE
// We are using inline styles here to keep the example simple.
// We recommend placing CSS in a separate style sheet and to
// encapsulating the styling in a new component.
Scroller scroller = new Scroller(new Div(personalInformation, employmentInformation));
scroller.setScrollDirection(Scroller.ScrollDirection.VERTICAL);
scroller.getStyle()
        .set("border-bottom", "1px solid var(--lumo-contrast-20pct)")
        .set("padding", "var(--lumo-space-m)");
add(scroller);

Scroll Direction

Scroller has four different scroll directions: vertical, horizontal, both and none. Scroller’s default scroll direction is both.

Vertical

When the scroll position is vertical, the user can scroll vertically if the content overflows the container vertically. Content that overflows horizontally is clipped and inaccessible, so the width of the content should be 100%.

Horizontal

When the scroll position is horizontal, the user can scroll horizontally if the content overflows the container horizontally. Content that overflows vertically is clipped and inaccessible, so the height of the content should be 100%.

Note

Use horizontal scrolling with caution, as it is much less common and may be difficult for users to recognize and use, in particular on non-mobile devices.

Desktop

Excluding Grids, horizontal scrolling is not commonly used in desktop and/or business applications as it can be non-obvious and cumbersome to use.

It is recommended to use using Buttons to help users notice and navigate horizontally scrollable sections. For horizontally scrollable lists, it is considered good practice to display how many list items there are and which items the user is viewing at the moment.

Mobile

Scrolling horizontally or swiping is more common on mobile, for example for navigation purposes. It can also be used to preserve vertical space, for example in situations where the user is exploring less important information, such as shortcuts or images.

Open in a
new tab
Scroller scroller = new Scroller();
scroller.setScrollDirection(Scroller.ScrollDirection.HORIZONTAL);

Button auditBtn = new Button("Audit");
auditBtn.setIcon(new Icon(VaadinIcon.CLIPBOARD_CHECK));
auditBtn.setHeight("100px");

Button reportBtn = new Button("Report");
reportBtn.setIcon(new Icon(VaadinIcon.BOOK_DOLLAR));
reportBtn.setHeight("100px");

Button dashboardBtn = new Button("Dashboard");
dashboardBtn.setIcon(new Icon(VaadinIcon.LINE_CHART));
dashboardBtn.setHeight("100px");

Button invoiceBtn = new Button("Invoice");
invoiceBtn.setIcon(new Icon(VaadinIcon.INVOICE));
invoiceBtn.setHeight("100px");

HorizontalLayout buttons = new HorizontalLayout(auditBtn, reportBtn, dashboardBtn, invoiceBtn);
buttons.setPadding(true);
buttons.getStyle().set("display", "inline-flex");

scroller.setContent(buttons);
add(scroller);

Both

When the scroll position is Both (default), the user can scroll vertically and horizontally if the content overflows in both directions.

This scroll direction is best suited for allowing the user to pan around large elements, such as images. It can also be used as a fallback for a responsive layout that cannot be guaranteed not to overflow in some situations.

Open in a
new tab
Scroller scroller = new Scroller();
scroller.setWidthFull();
scroller.setHeight("300px");

StreamResource imageResource = new StreamResource("reindeer+.jpg",
        () -> getClass().getResourceAsStream("/images/reindeer.jpg"));

Image img = new Image(imageResource, "A reindeer walking on a snowy lake shore at dusk");
scroller.setContent(img);

add(scroller);

None

Use None to hide content that overflows in either direction. No scrollbars are available to the user for accessing the clipped content. None can be used in fixed size/layout situations, where overflow would cause issues.

ComponentUsage recommendations

Basic Layouts

Layouts that aligns components and HTML elements horizontally and vertically.