Checkbox
- Usage
- Styling
Checkbox is an input field representing a binary choice. Checkbox Group is a group of related binary choices.
new tab
<vaadin-checkbox-group
label="Export data"
.value="${this.value}"
@value-changed="${(event: CheckboxGroupValueChangedEvent) => {
this.value = event.detail.value;
}}"
theme="vertical"
>
<vaadin-checkbox value="0" label="Order ID"></vaadin-checkbox>
<vaadin-checkbox value="1" label="Product name"></vaadin-checkbox>
<vaadin-checkbox value="2" label="Customer"></vaadin-checkbox>
<vaadin-checkbox value="3" label="Status"></vaadin-checkbox>
</vaadin-checkbox-group>
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
<vaadin-checkbox-group label="Departments" theme="vertical" disabled>
<vaadin-checkbox value="engineering" label="Engineering"></vaadin-checkbox>
<vaadin-checkbox value="human-resources" label="Human Resources"></vaadin-checkbox>
<vaadin-checkbox value="marketing" label="Marketing"></vaadin-checkbox>
<vaadin-checkbox value="operations" label="Operations"></vaadin-checkbox>
<vaadin-checkbox value="sales" label="Sales"></vaadin-checkbox>
</vaadin-checkbox-group>
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
<vaadin-checkbox-group label="Export data" .value="${this.value}" readonly theme="vertical">
<vaadin-checkbox value="0" label="Order ID"></vaadin-checkbox>
<vaadin-checkbox value="1" label="Product name"></vaadin-checkbox>
<vaadin-checkbox value="2" label="Customer"></vaadin-checkbox>
<vaadin-checkbox value="3" label="Status"></vaadin-checkbox>
</vaadin-checkbox-group>
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
<vaadin-checkbox
label="Grant view permissions"
required
error-message="This field is required"
${field(this.binder.model.view)}
></vaadin-checkbox>
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
<vaadin-checkbox
label="Notify users"
.checked="${selectedIds.length === items.length}"
.indeterminate="${selectedIds.length > 0 && selectedIds.length < items.length}"
@change="${(e: Event) => {
this.selectedIds = (e.target as HTMLInputElement).checked
? this.items.map((person) => String(person.id))
: [];
}}"
></vaadin-checkbox>
<vaadin-checkbox-group
label="Users to notify"
theme="vertical"
.value="${this.selectedIds}"
@value-changed="${(event: CheckboxGroupValueChangedEvent) => {
this.selectedIds = event.detail.value;
}}"
>
${items.map(
(person) => html`
<vaadin-checkbox
.value="${String(person.id)}"
label="${person.firstName} ${person.lastName}"
></vaadin-checkbox>
`
)}
</vaadin-checkbox-group>
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
<vaadin-checkbox-group label="Working days" theme="vertical">
<vaadin-checkbox value="mon" label="Monday"></vaadin-checkbox>
<vaadin-checkbox value="tue" label="Tuesday"></vaadin-checkbox>
<vaadin-checkbox value="wed" label="Wednesday"></vaadin-checkbox>
<vaadin-checkbox value="thu" label="Thursday"></vaadin-checkbox>
<vaadin-checkbox value="fri" label="Friday"></vaadin-checkbox>
<vaadin-checkbox value="sat" label="Saturday"></vaadin-checkbox>
<vaadin-checkbox value="sun" label="Sunday"></vaadin-checkbox>
</vaadin-checkbox-group>
In cases where vertical space needs to be conserved, horizontal orientation can be used. Still, no more than three options are recommended:
new tab
<vaadin-checkbox-group label="Permissions">
<vaadin-checkbox value="read" label="Read"></vaadin-checkbox>
<vaadin-checkbox value="edit" label="Edit"></vaadin-checkbox>
<vaadin-checkbox value="delete" label="Delete"></vaadin-checkbox>
</vaadin-checkbox-group>
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
<vaadin-checkbox label="Label" helper-text="Helper text"></vaadin-checkbox>
<vaadin-checkbox-group label="Label" helper-text="Helper text">
<vaadin-tooltip slot="tooltip" text="Tooltip text"></vaadin-tooltip>
<vaadin-checkbox value="1" label="Item 1"></vaadin-checkbox>
<vaadin-checkbox value="2" label="Item 2"></vaadin-checkbox>
<vaadin-checkbox value="3" label="Item 3"></vaadin-checkbox>
</vaadin-checkbox-group>
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
<vaadin-checkbox-group
theme="helper-above-field"
label="Label"
helper-text="Helper text"
style="--vaadin-input-field-border-width: 1px;"
>
</vaadin-checkbox-group>
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.
new tab
@customElement('checkbox-labeling')
export class Example extends LitElement {
protected override createRenderRoot() {
const root = super.createRenderRoot();
// Apply custom theme (only supported if your app uses one)
applyTheme(root);
return root;
}
protected override render() {
return html`<vaadin-checkbox label="Yes, I agree"></vaadin-checkbox>`;
}
}
It’s important to provide labels for Checkbox Groups to distinguish clearly any adjacent groups.
new tab
<vaadin-vertical-layout>
<vaadin-checkbox-group label="Manufacturer" theme="vertical">
<vaadin-checkbox value="0" label="Akuchi"></vaadin-checkbox>
<vaadin-checkbox value="1" label="Broek"></vaadin-checkbox>
<vaadin-checkbox value="2" label="Wulf"></vaadin-checkbox>
</vaadin-checkbox-group>
<vaadin-checkbox-group label="Status" theme="vertical">
<vaadin-checkbox value="0" label="In progress"></vaadin-checkbox>
<vaadin-checkbox value="1" label="Done"></vaadin-checkbox>
<vaadin-checkbox value="2" label="Cancelled"></vaadin-checkbox>
</vaadin-checkbox-group>
</vaadin-vertical-layout>
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