Accessibility
To implement an accessible PatternFly checkbox component:
- Ensure the checkbox can be navigated to and interacted with via keyboard and other assistive technologies such as a screen reader
- Ensure the checkbox can be toggled by clicking its visible text label, if it has one
- Ensure the checkbox label has hover styles to indicate that it can be clicked
- Provide an
aria-label
if the checkbox does not have a visible text label - Place related checkboxes inside of a list element, and that list inside of an element that has the
role="group"
attribute
Testing
At a minimum, a checkbox should meet the following criteria:
- Tab navigates to the next checkbox or focusable element, and Shift + Tab navigates to the previous checkbox or focusable element. If a checkbox is inside of a menu, follow the menu accessibility documentation instead.
- Only Space should be able to toggle the checkbox.
- This provides extra clickable space for users who may have trouble clicking the checkbox input itself, such as on mobile or due to a motor or physical impairment.
- This provides context to users who may not be able to see or understand the visual context, such as a checkbox that selects a table row.
- The
role
attribute notifies users of assistive technologies that they are in a group of checkboxes, while the list element notifies them of how many checkboxes there are in the group.
React customization
The following React props have been provided for more fine-tuned control over accessibility.
Prop | Applied to | Reason |
---|---|---|
aria-label="[text that labels the checkbox]" | Checkbox | Adds an accessible name to the checkbox when there is no visible text label. Required when there is no visible text label. If there is already a visible text label via the label prop, this prop should not be passed in. |
id | Checkbox | Links the checkbox input with its text label, which allows clicking the label to toggle the checkbox. The linked label text is also used as the accessible name of the checkbox. Required. |
isRequired | Checkbox | Adds the required attribute to the checkbox and adds visual styling to notify users the checkbox must be checked. Required when users must check the checkbox. |
isValid={[true or false]} | Checkbox | Sets the aria-invalid attribute, notifying users of assistive technologies whether the checkbox value is invalid. |
label="[text that labels the checkbox]" | Checkbox | Adds a visible text label for the checkbox, which also acts as its accessible name. |
HTML/CSS customization
The following HTML attributes and PatternFly classes can be used for more fine-tuned control over accessibility.
Attribute or class | Applied to | Reason |
---|---|---|
aria-invalid={[true or false]} | .pf-v5-c-check__input | Notifies users of assistive technologies whether the checkbox value is invalid. |
aria-label="[text that labels the checkbox]" | .pf-v5-c-check__input | Adds an accessible name to the checkbox when there is no visible text label. Required when there is no visible text label. If there is already a visible text label via the label element, this attribute should not be passed in. |
id | .pf-v5-c-check__input | Links the checkbox input with its text label, which allows clicking the label to toggle the checkbox. The linked label text is also used as the accessible name of the checkbox. Required. |
required | .pf-v5-c-check__input | Notifies users that the checkbox must be checked. Required when users must check the checkbox. |
for="[id of the associated checkbox]" | label | Links the label element to the checkbox, providing a larger clickable area to toggle the checkbox. Required if the checkbox is not already wrapped inside the label element. |
.pf-v5-c-check__label-required | label > span | Adds styling to visually notify users that the checkbox is required and to differentiate the content from the checkbox label. Usually an asterisk * is used to visually convey that an input is required. Required when users must check the checkbox. |
View source on GitHub