Checkbox
- Usage
- Styling
Checkbox is an input field representing a binary choice. Checkbox Group is a group of related binary choices.
new tab
Checkbox checkbox = new Checkbox();
checkbox.setLabel("I accept the terms and conditions");
add(checkbox);
new tab
CheckboxGroup<String> checkboxGroup = new CheckboxGroup<>();
checkboxGroup.setLabel("Export data");
checkboxGroup.setItems("Order ID", "Product name", "Customer",
"Status");
checkboxGroup.select("Order ID", "Customer");
checkboxGroup.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
add(checkboxGroup);
Use Checkbox Group to group related items. Individual checkboxes should be used for options that aren’t necessarily related to each other in any way.
States
Checkbox has a few states: disabled; read-only; required; and indeterminate. These are described in this section, in the following sub-sections.
Disabled
Disable a field to mark it as currently unavailable. Disabled state is used for fields that aren’t editable and don’t need to be readable. Disabled elements can’t be focused and may be inaccessible to assistive technologies like screen readers.
Disabling can be preferable to hiding an element to prevent changes in layout when the element’s visibility changes, and to make users aware of its existence even when currently unavailable.
Disabling is supported both for individual checkboxes, and for an entire checkbox group.
new tab
CheckboxGroup<String> disabledCheckGroup = new CheckboxGroup<>();
disabledCheckGroup.setLabel("Departments");
disabledCheckGroup.setItems("Engineering", "Human Resources",
"Marketing", "Operations", "Sales");
disabledCheckGroup.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
disabledCheckGroup.setEnabled(false);
add(disabledCheckGroup);
Read-Only
Fields used to display values should be set to read-only mode to prevent editing. Read-only fields are focusable and visible to screen readers.
Read-only mode is supported both on individual checkboxes and on an entire checkbox group.
new tab
CheckboxGroup<String> checkboxGroup = new CheckboxGroup<>();
checkboxGroup.setLabel("Export data");
checkboxGroup.setItems("Order ID", "Product name", "Customer",
"Status");
checkboxGroup.select("Order ID", "Customer");
checkboxGroup.setReadOnly(true);
checkboxGroup.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
add(checkboxGroup);
Required
Individual checkboxes can be marked as required. This is commonly used for checkboxes that must be checked in order to proceed with an operation, such as submitting a form. Required checkboxes become invalid when validated or if left unchecked after being focused.
An entire checkbox group can also be marked as required. They become invalid when validated or when blurred if none of their items are checked.
new tab
Checkbox checkbox = new Checkbox();
checkbox.setLabel("Grant view permissions");
checkbox.setRequiredIndicatorVisible(true);
Binder<UserPermissions> binder = new Binder<>(UserPermissions.class);
binder.forField(checkbox).asRequired("This field is required")
.bind(UserPermissions::getView, UserPermissions::setView);
Indeterminate
The indeterminate state can be used for a parent checkbox to show that there is a mix of checked and unchecked child items in a list, and to change the state of all child items at once.
new tab
Checkbox checkbox = new Checkbox("Notify users");
CheckboxGroup<Person> checkboxGroup = new CheckboxGroup<>();
checkboxGroup.setLabel("Users to notify");
checkboxGroup.setItemLabelGenerator(
person -> person.getFirstName() + " " + person.getLastName());
checkboxGroup.setItems(items);
checkboxGroup.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
checkboxGroup.addValueChangeListener(event -> {
if (event.getValue().size() == items.size()) {
checkbox.setValue(true);
checkbox.setIndeterminate(false);
} else if (event.getValue().size() == 0) {
checkbox.setValue(false);
checkbox.setIndeterminate(false);
} else {
checkbox.setIndeterminate(true);
}
});
checkbox.addValueChangeListener(event -> {
if (checkbox.getValue()) {
checkboxGroup.setValue(new HashSet<>(items));
} else {
checkboxGroup.deselectAll();
}
});
checkboxGroup.select(items.get(0), items.get(2));
add(checkbox, checkboxGroup);
Orientation
The component’s default orientation is horizontal. However, vertical orientation is recommended whenever possible as it’s easier for the user to scan a vertical list of options:
new tab
CheckboxGroup<String> checkboxGroup = new CheckboxGroup<>();
checkboxGroup.setLabel("Working days");
checkboxGroup.setItems("Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday");
checkboxGroup.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
add(checkboxGroup);
In cases where vertical space needs to be conserved, horizontal orientation can be used. Still, no more than three options are recommended:
new tab
CheckboxGroup<String> checkboxGroup = new CheckboxGroup<>();
checkboxGroup.setLabel("Permissions");
checkboxGroup.setItems("Read", "Edit", "Delete");
add(checkboxGroup);
Basic Features
The following features, common to most input field components, are supported:
Label
The label is used to identify the input field. It supports plain-text content, and its length is limited to the width of the field. Helpers and Tooltips can be used to provide additional information that doesn’t fit into the label.
Visible labels are strongly recommended for all input fields. In cases where the built-in label cannot be used, an external element can be associated as the field’s label through the aria-labelledby
attribute. Fields without any visible label should include an invisible label for assistive technologies with the aria-label
attribute.
Helper
Helpers are used to provide additional information that the user may need to enter in the field, such as format requirements or explanations of the field’s purpose below the field.
A style variant is available for rendering the helper above the field.
In addition to plain text, helpers can contain components and HTML elements. However, complex and interactive content is likely to have accessibility issues.
Tooltip
Tooltips are small text pop-ups displayed on hover, and on keyboard-focus. They can be used to provide additional information about a field. This can be useful in situations where an always visible Helper is not appropriate. Helpers are generally recommended in favor of tooltips, though, as they provide much better discoverability and mobile support. See the Tooltip documentation for more information.
External & Invisible Labels (ARIA)
Visible labels are strongly recommended for all input fields. In situations where the built-in label cannot be used, an external element can be associated as the field’s label through its element id
. Fields without any visible label should be provided an invisible label for assistive technologies like screen readers.
<!-- Associates external element as label: -->
<label id="external-label">This is the label</label>
<vaadin-checkbox accessible-name-ref="external-label">...
<!-- Invisible label for screen readers: -->
<vaadin-checkbox accessible-name="This is the label">...
new tab
Checkbox checkbox = new Checkbox();
checkbox.setLabel("Label");
checkbox.setHelperText("Helper text");
CheckboxGroup<String> field = new CheckboxGroup<>();
field.setLabel("Label");
field.setHelperText("Helper text");
field.setTooltipText("Tooltip text");
Style Variants
The following style variants can be applied:
Helper Above Field
The helper can be rendered above the field, and below the label.
Borders
Borders can be applied to the field surface by providing a value (e.g., 1px
) to the --vaadin-input-field-border-width
CSS property. This can be applied globally to all input fields using the html
selector, or to individual component instances. Borders are required to achieve WCAG 2.1 level AA conformant color contrast with the default Lumo styling of fields.
You can override the default border color with the --vaadin-input-field-border-color
property.
new tab
CheckboxGroup<String> field = new CheckboxGroup<>();
field.addThemeVariants(CheckboxGroupVariant.LUMO_HELPER_ABOVE_FIELD);
field.getStyle().set("--vaadin-input-field-border-width", "1px");
Best Practices
One of the best practices to consider is related to labeling. Try to use short and descriptive labels with positive wording. Avoid negations.
It’s important to provide labels for Checkbox Groups to distinguish clearly any adjacent groups.
new tab
CheckboxGroup<String> manufacturer = new CheckboxGroup<>();
manufacturer.setLabel("Manufacturer");
manufacturer.setItems("Akuchi", "Broek", "Wulf");
manufacturer.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
CheckboxGroup<String> status = new CheckboxGroup<>();
status.setLabel("Status");
status.setItems("In progress", "Done", "Cancelled");
status.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
add(manufacturer, status);
Related Components
Component | Usage Recommendation |
---|---|
A field for selecting an item from a list of options which are presented in an overlay. This is useful when there is insufficient space for a Radio Button Group. | |
A filterable, lazy loading alternative to Select. This is recommended for ten or more items. | |
Scrollable list of options. This supports single and multi-select. | |
Corresponding component for mutually exclusive options, or single-select. |
F86F2BE5-1BDA-4E79-BD9E-6CE12742450B