Switch
Switch
is a component used to toggle between on and off states.
Import
import { Switch } from "@yamada-ui/react"
Usage
Editable example
<Switch>basic</Switch>
Change Variant
Editable example
<Wrap gap="md"> <Switch variant="thick">thick</Switch> <Switch variant="thin">thin</Switch> </Wrap>
Change Size
Editable example
<Wrap gap="md"> <Switch size="sm">small size</Switch> <Switch size="md">medium size</Switch> <Switch size="lg">large size</Switch> </Wrap>
Change Color Scheme
Editable example
<Wrap gap="md"> <Switch colorScheme="secondary" defaultIsChecked> Secondary </Switch> <Switch colorScheme="green" defaultIsChecked> Green </Switch> </Wrap>
Change Label Position
To change the label position, set isReverse
to true
.
Editable example
<Switch isReverse>basic</Switch>
Disable
To disable, set isDisabled
to true
.
Editable example
<Wrap gap="md"> <Switch isDisabled>disabled</Switch> <Switch isDisabled defaultIsChecked> disabled </Switch> </Wrap>
Make Read-Only
To make read-only, set isReadOnly
to true
.
Editable example
<Wrap gap="md"> <Switch isReadOnly>read only</Switch> <Switch isReadOnly defaultIsChecked> read only </Switch> </Wrap>
Customize Label
To customize the label, use Label
.
Editable example
const id = useId() return ( <HStack gap="sm"> <Label htmlFor={id} userSelect="none"> Please Click </Label> <Switch id={id} /> </HStack> )
Control
Editable example
const [isChecked, { toggle }] = useBoolean(false) return ( <Switch isChecked={isChecked} onChange={toggle}> custom control </Switch> )
Use React Hook Form
Editable example
type Data = { switch: boolean } const { control, handleSubmit, watch } = useForm<Data>() const onSubmit: SubmitHandler<Data> = (data) => console.log("submit:", data) console.log("watch:", watch()) return ( <VStack as="form" onSubmit={handleSubmit(onSubmit)}> <Controller name="switch" control={control} render={({ field: { value, ...rest } }) => ( <Switch isChecked={value} {...rest}> Dark mode </Switch> )} /> <Button type="submit" alignSelf="flex-end"> Submit </Button> </VStack> )
Edit this page on GitHub