Examples
To keep inline with accessibility guidelines, the label for a switch must never dynamically change. A dynamically changing label (such as one label for an "on" state and another label for an "off" state) can be confusing as the contexts for each label changes as the switch state does. This applies to both visible text labels as well as labels provided via aria-label
.
Props
Switch
Name | Type | Default | Description |
---|---|---|---|
aria-label | string | undefined | Adds an accessible name to the switch when the label prop is not passed, and must describe the isChecked="true" state. |
aria-labelledby | string | undefined | Adds an accessible name to the switch via one or more referenced id(s). The computed accessible name must describe the isChecked="true" state. |
className | string | Additional classes added to the switch | |
defaultChecked | boolean | Flag to set the default checked value of the switch when it is uncontrolled by React state. To make the switch controlled instead use the isChecked prop, but do not use both. | |
hasCheckIcon | boolean | Flag to show if the switch has a check icon. | |
id | string | id for the label. | |
isChecked | boolean | true | Flag to show if the switch is checked when it is controlled by React state. To make the switch uncontrolled instead use the defaultChecked prop, but do not use both. |
isDisabled | boolean | false | Flag to show if the switch is disabled. |
isReversed | boolean | false | Flag to reverse the layout of toggle and label (label at start, toggle at end). |
label | React.ReactNode | Text value for the visible label | |
onChange | (event: React.FormEvent<HTMLInputElement>, checked: boolean) => void | () => undefined as any | A callback for when the switch selection changes. (event, isChecked) => {} |
ouiaId | number | string | Value to overwrite the randomly generated data-ouia-component-id. | |
ouiaSafe | boolean | Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. |
CSS variables
Expand or collapse column | Selector | Variable | Value | |
---|---|---|---|---|
.pf-v6-c-switch | --pf-v6-c-switch--FontSize | 0.875rem | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch--ColumnGap | 0.5rem | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle-icon--FontSize | calc(0.875rem * .625) | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle-icon--Color | (In light theme) #ffffff | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle-icon--InsetInlineStart | calc(0.875rem * .4) | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle-icon--Offset | 0.125rem | ||
.pf-v6-c-switch | --pf-v6-c-switch__input--disabled__toggle-icon--Color | (In light theme) #a3a3a3 | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch--LineHeight | 1.5 | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch--Height | auto | ||
.pf-v6-c-switch | --pf-v6-c-switch__input--checked__toggle--BackgroundColor | (In light theme) #0066cc | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--checked__toggle--before--TranslateX | calc(100% + 0.125rem) | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--checked__toggle--BorderWidth | 0 | ||
.pf-v6-c-switch | --pf-v6-c-switch__input--checked__label--Color | (In light theme) #151515 | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--not-checked__label--Color | (In light theme) #4d4d4d | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--disabled__label--Color | (In light theme) #a3a3a3 | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--disabled__toggle--BackgroundColor | (In light theme) #c7c7c7 | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--checked__toggle--before--BackgroundColor | (In light theme) #ffffff | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--not-checked__toggle--before--BackgroundColor | (In light theme) #707070 | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--disabled__toggle--before--BackgroundColor | (In light theme) #4d4d4d | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--focus__toggle--OutlineWidth | 2px | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--focus__toggle--OutlineOffset | 0.25rem | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__input--focus__toggle--OutlineColor | (In light theme) #0066cc | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--Height | calc(0.875rem * 1.5) | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--BackgroundColor | (In light theme) #ffffff | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--BorderColor | (In light theme) #c7c7c7 | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--BorderWidth | 1px | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--BorderRadius | 999px | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--before--Width | calc(0.875rem - 0.125rem) | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--before--Height | calc(0.875rem - 0.125rem) | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--before--InsetInlineStart | calc((calc(0.875rem * 1.5) - calc(0.875rem - 0.125rem)) / 2) | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--before--BorderRadius | 24px | ||
| ||||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--before--Transition | transform .25s ease 0s | ||
.pf-v6-c-switch | --pf-v6-c-switch__toggle--Width | calc(calc(0.875rem * 1.5) + 0.125rem + calc(0.875rem - 0.125rem)) | ||
| ||||
.pf-v6-c-switch__input:checked ~ .pf-v6-c-switch__toggle | --pf-v6-c-switch__toggle--BorderWidth | 0 | ||
| ||||
.pf-v6-c-switch__input:disabled ~ .pf-v6-c-switch__toggle | --pf-v6-c-switch__toggle-icon--Color | (In light theme) #a3a3a3 | ||
|