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
The Switch follows the WAI-ARIA - Switch Pattern for accessibility.
Keyboard Navigation
| Key | Description | State |
|---|---|---|
Tab | Focuses on a switch that is not disabled. | - |
Space | Toggles the state of the focused switch. | - |
Enter | Toggles the state of the focused switch when checkOnEnter is true. | - |
ARIA Roles and Attributes
| Component | Roles and Attributes | Usage |
|---|---|---|
Switch | role="switch" | Indicates that this is a switch. |
aria-checked | Sets to "true" if the switch is on, and "false" if it is off. | |
aria-describedby | If Switch is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
aria-disabled | Sets to "true" if disabled is set. | |
aria-invalid | Sets to "true" if invalid is set. | |
aria-readonly | Sets to "true" if readOnly is set. | |
aria-required | Sets to "true" if required is set. |