Checkbox
A Checkbox is a binary input that can appear by itself or in a multiselect group. Checkboxes can be selected, unselected or in an indeterminate state.
Name | Value |
---|---|
Props | |
status | |
disabled | |
indeterminate | |
Slots | |
default | |
description | |
View | |
Reading direction |
Overview
When to use Checkbox
Checkbox must feature a descriptive label. They may appear alone, such as in a disclaimer, or as a part of a group. A Checkbox may also have sub-options or child Checkboxes.
Use the Checkbox component when you want users to make one or more selections from a list of options. A Checkbox can also be used to accept terms and conditions. Avoid using Checkbox when only one selection is allowed; in such cases, use Radio instead.
About Checkbox
Checkbox includes the following elements.
Checkbox
The Checkbox’s input makes the selection visually distinct.
Label
The Checkbox must always contain a label, with the text size matching the base font size for consistency with the body text. Labels can include links and bold text and should be concise, clearly indicating the selected option.
Keep the choices short and mutually exclusive. Concise, Clear & Trustworthy
- Use text formatting like bold and italic sparingly in the label.
- Include standalone links within the label to provide additional information regarding a specific option when necessary.
- Avoid linking the entire label as it could cause issues with the selection.
Description (optional)
If additional information about the label is required, a description can be included.
Examples
Label and description
Checkboxes must always have a label and can also feature a description.
Developer notes
Always include label text via the default slot. You can also add description text via the #description
slot.
A single Checkbox does not need an inputValue
prop. v-model
is bound to a boolean value: true
for checked, false
for unchecked.
Form field
When used in a form, a single Checkbox or group of Checkboxes can be wrapped in the Field component to add features like a semantic label, description and help text, validation messages, and more. See the Field page for more information.
Create a direct question or a complete sentence to precede the Checkboxes. Translatable & Clear
Include inline error messages for both individual and groups of Checkboxes to inform users and guide them in fixing issues.
If using a Checkbox or Checkbox group outside of a form, follow the instructions in the next demo.
Developer notes
When building a Checkbox field, always set isFieldset
to true
, even for a single Checkbox, to ensure accessibility support. This wraps the group in a <fieldset>
element and labels it with an associated <legend>
.
Checkbox group
Checkboxes are most typically used in groups.
Developer notes
For a group of related Checkboxes, each Checkbox component's v-model
will be bound to an array of the inputValue
props of the Checkboxes that are currently "on".
This demo shows what to do when using a Checkbox group outside of a form:
- Wrap the group in an element with
role="group"
- Connect the group with a label via the
aria-labelledby
attribute
Inline Checkboxes
Checkboxes can be horizontally stacked if needed in some specific cases. However, the recommendation is to vertically stack them to maintain visual flow.
- Use inline Checkboxes for specific cases to prevent disruptions in the reading flow.
- Avoid using inline Checkboxes if there are too many Checkboxes per line.
- Avoid using inline Checkboxes if there is significant variation in the length of the Checkbox labels.
Developer notes
Use the inline
prop to get an inline layout.
Indeterminate state
In addition to selected and unselected, a Checkbox can be in an indeterminate
state. This state is common for checkboxes to present a number of sub-options (which are also checkboxes). If all of the sub-options are checked, the main checkbox will also be checked, and if they're all unchecked, the main checkbox would be unchecked. If any one or more of the sub-options have a different state than the others, the main checkbox would present an indeterminate state checkbox.
- Use the indeterminate Checkbox when there is a long list of sub-checkboxes to select.
- Align secondary Checkboxes with the label of the indeterminate Checkbox.
Developer notes
The parent component must house the logic to set a Checkbox to the indeterminate
state via this prop (e.g. in the case of a set of nested checkboxes where some boxes are checked and some are not, making the parent checkbox neither fully on nor fully off).
This prop is independent of the value provided by v-model
. If indeterminate
is set to true
, the indeterminate visual state will display, but the value will not be affected. Nor will the value affect the visual state: indeterminate
overrides the checked and unchecked visual states. If indeterminate
changes to false
, the visual state will reflect the current v-model
value.
With custom input
An additional input field can be included within the Checkbox to allow the user to input a custom response. The custom input within the Checkbox can include any of the following form components designed to gather user input, including:
- TextInput and TextArea
- Select
- Combobox
- ChipInput
- Lookup
- A combination of more than one input
- Display the custom input at the end of a Checkbox group whenever possible.
- Disable the custom input unless its corresponding Checkbox is selected.
- Don't design a layout where multiple Checkboxes include custom inputs visible simultaneously.
Developer notes
To add a custom input, use the custom-input
slot to pass in an input like TextInput, TextArea, Select, Combobox, ChipInput, Lookup, or a combination of more than one input.
In the example below, the custom input is always visible but remains disabled until the parent Checkbox is selected. Inside the custom input <div>
, a Field wraps the TextInput to display its own validation message.
Open the console to review form data on submission.
Technical implementation
Vue usage
Typical use of this component will involve using v-for
to loop through an array of items and output a Checkbox component for each one. Each Checkbox will have the same v-model
prop, but different inputValue
props and label content.
For a single Checkbox, the v-model
value will be a boolean true
when the checkbox is checked and false
when unchecked.
For multiple Checkboxes, the v-model
value will be an array of the inputValue
props of any currently checked checkboxes (or an empty array if no checkboxes are checked).
Props
Prop name | Description | Type | Default |
---|---|---|---|
modelValue | Value of the checkbox or checkbox group. Provided by v-model binding in the parent component. | (string|number)[] | false |
inputValue | HTML "value" attribute to assign to the input. Required for input groups. | string|number|boolean | false |
name | HTML "name" attribute to assign to the input. | string | null |
disabled | Whether the disabled attribute should be added to the input. | boolean | false |
indeterminate | Whether the indeterminate visual state should be displayed. This is unrelated to the value provided by v-model , and the indeterminate visual state will override the checked or unchecked visual state. | boolean | false |
inline | Whether the component should display inline. By default, display: block is set and a margin exists between sibling components, for a stacked layout. | boolean | false |
hideLabel | Whether the label should be visually hidden. When true, the label will remain accessible to assistive technology. | boolean | false |
status | Validation status of the Checkbox. | ValidationStatusType | 'default' |
Events
Event name | Properties | Description |
---|---|---|
update:modelValue | modelValue boolean|string[]|number[] - The new model value | Emitted when modelValue changes. |
Slots
Name | Description | Bindings |
---|---|---|
default | Label text. | |
description | Short description text. | |
custom-input | Custom input. |
CSS-only version
Markup structure
With description
To add a description below the label:
- Add a
<span>
after the<label>
element with an ID and classcdx-label__description
. Include the description text here. - Add an
aria-describedby
attribute to the<input>
element whose value is the ID of the description
Checkbox group
Native attributes of the <input>
element can be used. For example:
- Add the
checked
attribute to the<input>
element if it should be selected initially. - Add the
disabled
attribute to the<input>
element if it should be disabled.
Note that indeterminate
is not supported in the CSS-only version since it cannot be set without JavaScript.
Always include one of these two features for accessible grouping:
- If using the Checkbox group in a field, wrap the group in a
<fieldset>
element and add a<legend>
element with the group label. This method is demonstrated below and requires some style resets on<fieldset>
and<legend>
. You can use the CSS-only Field and Label components to reset browser styles of these elements. - If using the Checkbox group outside of a field, wrap the group in a
<div>
withrole="group"
andaria-labelledby
set to the ID of the group label. See an example of this above.
Inline Checkboxes
Add the cdx-checkbox--inline
class to the root element to get an inline layout.
With custom input
To add a custom input, add a <div>
element with the cdx-radio__custom-input
class inside a Checkbox. Inside the custom input, add an input like TextInput, TextArea, Select, Combobox, ChipInput, Lookup, or a combination of more than one input.
Keyboard navigation
Key | Function |
---|---|
Tab | It moves the focus to the next Checkbox within a group or to the next interactive element in tab order. |
Shift + Tab | It moves the focus to the previous Checkbox within a group or to the previous interactive element. |
Space | If the focus is placed on the Checkbox, it toggles the Checkbox state. |