Switch
The Switch component is used as an alternative for the
Checkbox component. You can switch between enabled or
disabled states.
Switch must always be accompanied by a label, and follows the same keyboard
workflow as a Checkbox.
Import#
import { Switch } from "@chakra-ui/react"
Usage#
Editable Example
Sizes#
Pass the size prop to change the size of the Switch.
Editable Example
Switch background color#
You can change the checked background color of the Switch by passing the
color prop.
Editable Example
Props#
| Name | Type | Description | Default |
|---|---|---|---|
| colorScheme | string | - | |
| defaultChecked | boolean | If `true`, the checkbox will be initially checked. | - |
| defaultIsChecked | boolean | If `true`, the checkbox will be initially checked. @deprecated Please use the `defaultChecked` prop, which mirrors default React checkbox behavior. | - |
| id | string | id assigned to input | - |
| isChecked | boolean | If `true`, the checkbox will be checked. You'll need to pass `onChange` to update its value (since it is now controlled) | - |
| isDisabled | boolean | If `true`, the checkbox will be disabled | - |
| isFocusable | boolean | If `true` and `isDisabled` is passed, the checkbox will remain tabbable but not interactive | - |
| isInvalid | boolean | If `true`, the checkbox is marked as invalid. Changes style of unchecked state. | - |
| isReadOnly | boolean | If `true`, the checkbox will be readonly | - |
| isRequired | boolean | If `true`, the checkbox input is marked as required, and `required` attribute will be added | - |
| name | string | The name of the input field in a checkbox (Useful for form submission). | - |
| onChange | ((event: ChangeEvent<HTMLInputElement>) => void) | The callback invoked when the checked state of the `Checkbox` changes.. | - |
| size | string | - | |
| value | string | number | The value to be used in the checkbox input. This is the value that will be returned on form submission. | - |
| variant | string | - |