Accordion
- Usage
- Styling
Accordion is a vertically stacked set of expandable panels. It reduces clutter and helps maintain the user’s focus by showing only the relevant content at any time.
new tab
Accordion accordion = new Accordion();
Span name = new Span("Sophia Williams");
Span email = new Span("sophia.williams@company.com");
Span phone = new Span("(501) 555-9128");
VerticalLayout personalInformationLayout = new VerticalLayout(name,
email, phone);
personalInformationLayout.setSpacing(false);
personalInformationLayout.setPadding(false);
accordion.add("Personal information", personalInformationLayout);
Anatomy
Accordion consists of stacked panels, each composed of two parts: a summary and a content area. Only one panel can be expanded at a time. Use the Details component to allow multiple, simultaneously expanded sections.
Summary
The summary is the part that’s always visible, and typically describes the content, for example, with a title. Clicking on the summary toggles the content area’s visibility.
The summary supports rich content and can contain any component. This can be utilized, for example, to display the status of the corresponding content.
new tab
AccordionPanel customDetailsPanel = accordion.add(CUSTOMER_DETAILS,
customerDetailsFormLayout);
customDetailsPanel.addOpenedChangeListener(e -> {
if (e.isOpened()) {
customDetailsPanel.setSummaryText(CUSTOMER_DETAILS);
} else if (personBinder.getBean() != null) {
Person personValues = personBinder.getBean();
customDetailsPanel.setSummary(createSummary(CUSTOMER_DETAILS,
personValues.getFirstName() + " "
+ personValues.getLastName(),
personValues.getEmail(),
personValues.getAddress() != null
? personValues.getAddress().getPhone()
: ""));
}
});
Content
This is the collapsible part of a panel. It can contain any component. When the content area is collapsed, the content is invisible and inaccessible by keyboard or screen reader.
new tab
Accordion accordion = new Accordion();
accordion.add("Analytics",
createContent(createStyledAnchor("#", "Dashboard"),
createStyledAnchor("#", "Reports"),
createStyledAnchor("#", "Data sources")));
Theme Variants
Accordion has three theme variants: filled
, small
, and reverse
. Set the theme attribute separately for each panel, not on Accordion itself. Theme names can be combined with each other. For example, all three themes variants can be applied to a panel.
Filled Panels
The filled
theme variant makes the panel’s boundaries visible. This helps tie its content together visually and distinguishes it from the surrounding UI.
new tab
Accordion accordion = new Accordion();
AccordionPanel personalInfoPanel = accordion.add("Personal information",
personalInformationLayout);
personalInfoPanel.addThemeVariants(DetailsVariant.FILLED);
Small Panels
Use the small
theme variant for compact UIs.
new tab
AccordionPanel paymentPanel = accordion.add("Payment", paymentLayout);
paymentPanel.addThemeVariants(DetailsVariant.SMALL);
Reverse Panels
The reverse
theme variant places the toggle icon after the summary contents. This can be useful for aligning visually the summary with other content.
new tab
AccordionPanel paymentPanel = accordion.add("Payment", paymentLayout);
paymentPanel.addThemeVariants(DetailsVariant.REVERSE);
Disabled Panels
Accordion panels can be disabled to prevent them from being expanded or collapsed. Details can also be disabled to prevent them from being expanded or collapsed. Components inside a disabled expanded panel are automatically disabled as well.
new tab
Accordion accordion = new Accordion();
AccordionPanel billingAddressPanel = accordion.add("Billing address",
billingAddressLayout);
billingAddressPanel.setEnabled(false);
Best Practices
Accordions are suitable when users need to focus on smaller pieces of content at any given time. However, when all of the content is relevant to the user to make a decision, Accordions should be avoided. Content areas that are logically linked should be grouped together in one panel.
Accordions are better suited than Tabs for long labels. However, Accordions can feel jumpy as panels are toggled — especially if there are several panels or the panel content is long.
Details can be used instead of Accordion when there is a need to see content from multiple panels, simultaneously.
Accordions can be used to break a complex form into smaller step-by-step sections.
The expandable and collapsible nature of accordions can sometimes be difficult for users to discover. Use the filled variant and apply tooltips on the panels to make this more discoverable.