Scroller
- Usage
- Styling
Scroller is a component container for creating scrollable areas in the UI.
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 employmentTitle = new H3("Employment information");
employmentTitle.setId(EMPLOYMENT_TITLE_ID);
TextField position = new TextField("Position");
position.setWidthFull();
TextArea additionalInformation = new TextArea("Additional Information");
additionalInformation.setWidthFull();
Section employmentInformation = new Section(employmentTitle, 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’s much less common and may be difficult for users to recognize and use, in particular on non-mobile devices. |
Desktop
Excluding Grids, horizontal scrolling isn’t commonly used in desktop and/or business applications, as it can be non-obvious and cumbersome to use.
It’s recommended to use Buttons to help users notice and navigate horizontally scrollable sections. For horizontally scrollable lists, it’s considered good practice to display the number of items there are in the list, and which items the user is currently viewing.
Mobile
Scrolling horizontally or swiping is more common on mobile, for example for navigation purposes. It can also be used to conserve vertical space, for example in situations where the user is exploring less-important information, such as shortcuts or images.
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 to allowing the user to pan around large elements, such as images. It can also be used as a fallback for a responsive layout that can’t be guaranteed not to overflow in some situations.
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);