Switch
Switch
is a component used to toggle between on and off states.
<Switch>Basic</Switch>
Usage
import { Switch } from "@yamada-ui/react"
import { Switch } from "@/components/ui"
import { Switch } from "@workspaces/ui"
<Switch />
Change Variant
<HStack>
<For each={["thick", "thin"]}>
{(variant) => (
<Switch key={variant} variant={variant}>
{toTitleCase(variant)}
</Switch>
)}
</For>
</HStack>
Change Size
<HStack>
<For each={["sm", "md", "lg"]}>
{(size) => (
<Switch key={size} size={size}>
Size ({size})
</Switch>
)}
</For>
</HStack>
Change Color Scheme
<HStack>
<For each={["success", "warning"]}>
{(colorScheme) => (
<Switch key={colorScheme} colorScheme={colorScheme} defaultChecked>
{toTitleCase(colorScheme)}
</Switch>
)}
</For>
</HStack>
Change Label Position
<Switch reverse>Reverse Label</Switch>
Change Shape
<VStack>
<For each={["square", "rounded", "circle"]}>
{(shape) => (
<Switch key={shape} shape={shape} defaultChecked>
{toTitleCase(shape)}
</Switch>
)}
</For>
</VStack>
Disable
<HStack>
<Switch disabled>Disabled</Switch>
<Switch disabled defaultChecked>
Disabled
</Switch>
</HStack>
Read-Only
<HStack>
<Switch readOnly>Read Only</Switch>
<Switch readOnly defaultChecked>
Read Only
</Switch>
</HStack>
Customize Icon
<Switch
size="lg"
icon={{ off: <MoonIcon fontSize="sm" />, on: <SunIcon fontSize="sm" /> }}
>
Dark Mode
</Switch>
Customize Label
const id = useId()
return (
<HStack gap="sm">
<Text as="label" htmlFor={id} userSelect="none">
Please Click
</Text>
<Switch id={id} />
</HStack>
)
Control
const [checked, { toggle }] = useBoolean(false)
return (
<Switch checked={checked} onChange={toggle}>
Custom Control
</Switch>
)
"use client"
to the top of the file.Props
Accessibility
Currently, this section is being updated due to the migration of v2.