Switch
The Switch component is used as an alternative for the checkbox component.
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
Sizes
Pass the size prop to change the size of the Switch.
Switch background color
You can change the checked background color of the Switch by passing the
colorScheme prop.
State depending behavior
States like isDisabled have an impact on the usability of a Switch and on
the styles, except for the isInvalid and the isRequired prop.
Props
aria-describedby
aria-describedbystringaria-invalid
aria-invalidtruearia-label
aria-labelDefines the string that labels the checkbox element.
stringaria-labelledby
aria-labelledbyRefers to the id of the element that labels the checkbox element.
stringcolorScheme
colorScheme"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram""blue"defaultChecked
defaultCheckedIf true, the checkbox will be initially checked.
booleanid
idid assigned to input
stringisChecked
isCheckedIf true, the checkbox will be checked.
You'll need to pass onChange to update its value (since it is now controlled)
booleanisDisabled
isDisabledIf true, the checkbox will be disabled
booleanisFocusable
isFocusableIf true and isDisabled is passed, the checkbox will
remain tabbable but not interactive
booleanisInvalid
isInvalidIf true, the checkbox is marked as invalid.
Changes style of unchecked state.
booleanisReadOnly
isReadOnlyIf true, the checkbox will be readonly
booleanisRequired
isRequiredIf true, the checkbox input is marked as required,
and required attribute will be added
booleanname
nameThe name of the input field in a checkbox (Useful for form submission).
stringonBlur
onBlurThe callback invoked when the checkbox is blurred (loses focus)
((event: FocusEvent<HTMLInputElement, Element>) => void)onChange
onChangeThe callback invoked when the checked state of the Checkbox changes.
((event: ChangeEvent<HTMLInputElement>) => void)onFocus
onFocusThe callback invoked when the checkbox is focused
((event: FocusEvent<HTMLInputElement, Element>) => void)size
size"sm" | "md" | "lg""md"spacing
spacingThe spacing between the switch and its label text
SystemProps["marginLeft"]0.5remtabIndex
tabIndexThe tab-index property of the underlying input element.
numbervalue
valueThe value to be used in the checkbox input. This is the value that will be returned on form submission.
string | numbervariant
variantVariants for Switch are not implemented in the default theme. You can extend the theme to implement them.
string