Components
Switch
The Switch component is used to toggle between enabled or disabled states.
Use toggles when you intend to turn something on or off. For example, if you need to enable a feature flag. If a physical switch would work for the action, a toggle is probably the best component to use.
Import
import { Switch } from '@outfitio/outkit';
Usage
Sizes
Pass the size prop to change the size of the switch.
Accessibility
- Use the disabled prop to apply the HTML disabled attribute to the switch. This prevents users from being able to interact with the switch, and conveys its inactive state to assistive technologies.
- In the event there is no visible label for the switch - for example, within the context of a table column - use the
aria-labelprop to provide a label for assistive technologies. Alternatively, usearia-labelledbyto point to an alternate label.
Best Practice
- Switch labels should correspond to the positive or active state of the setting. For example
Turn on notificationsinstead ofTurn off notifications. - Use sentence case for switch labels, where the first word is capitalised and the rest are lowercase, unless the term is a proper noun
Props
| Name | Type | Default | Description |
|---|---|---|---|
size | sm, md, lg | The size of the switch. | |
color | string | The background color of the switch when checked. | |
name | string | The input name of the switch when used in a form. | |
value | string, boolean | The value of the switch. | |
children | React.ReactNode | The children of the switch. | |
aria-label | string | The aria-label of the switch for accessibility. | |
aria-labelledby | string | The aria-labelledby of the switch for accessibility. | |
isChecked | boolean | If true, set the switch to the checked state. | |
defaultIsChecked | boolean | If true, the checkbox will be initially checked. | |
isDisabled | boolean | If true, set the disabled to the invalid state. | |
isInvalid | boolean | If true, set the switch to the invalid state. |