---
title: Switch
description: "`Switch` is a component used to toggle between on and off states."
links:
- style: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/switch/switch.style.ts
- source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/switch
- storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-switch--basic
---
# Switch
`Switch` is a component used to toggle between on and off states.
```tsx
Basic
```
## Usage
```tsx
import { Switch } from "@yamada-ui/react"
```
```tsx
import { Switch } from "@/components/ui"
```
```tsx
import { Switch } from "@workspaces/ui"
```
```tsx
```
:::tip
To allow users to select one option from multiple choices, use [Radio](https://yamada-ui.com/docs/components/radio.md).
:::
:::tip
If users need to select multiple values from multiple options, use [Checkbox](https://yamada-ui.com/docs/components/checkbox.md).
:::
### Change Variant
```tsx
{(variant) => (
{toTitleCase(variant)}
)}
```
### Change Size
```tsx
{(size) => (
Size ({size})
)}
```
### Change Color Scheme
```tsx
{(colorScheme) => (
{toTitleCase(colorScheme)}
)}
```
### Change Label Position
```tsx
Reverse Label
```
### Change Shape
```tsx
{(shape) => (
{toTitleCase(shape)}
)}
```
### Disable
```tsx
Disabled
Disabled
```
### Read-Only
```tsx
Read Only
Read Only
```
### Customize Icon
```tsx
, on: }}
>
Dark Mode
```
### Customize Label
```tsx
const id = useId()
return (
Please Click
)
```
### Control
```tsx
const [checked, { toggle }] = useBoolean(false)
return (
Custom Control
)
```
## Props
| Prop | Default | Type | Description |
| ---------------- | --------- | ------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------- |
| `as` | - | `As` | The HTML element to render. |
| `asChild` | - | `boolean` | Merges its props onto its immediate child. |
| `css` | - | `CSSObject \| CSSObject[]` | The CSS object. |
| `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. |
| `size` | `"md"` | `"lg" \| "md" \| "sm"` | The size of the component. |
| `variant` | `"thick"` | `"thick" \| "thin"` | The variant of the component. |
| `checked` | `false` | `boolean` | If `true`, the switch will be checked. |
| `checkOnEnter` | `true` | `boolean` | If `true`, the switch will be checked when the Enter key is pressed. |
| `defaultChecked` | `false` | `boolean` | If `true`, the switch will be initially checked. |
| `disabled` | `false` | `boolean` | If `true`, the field will be disabled. |
| `icon` | - | `ReactNode \| { off: ReactNode; on: ReactNode }` | The switch icon to use. |
| `id` | - | `string` | id assigned to input. |
| `inputProps` | - | `HTMLStyledProps<"input">` | Props for switch input element. |
| `invalid` | `false` | `boolean` | If `true`, the field will be invalid. |
| `labelProps` | - | `HTMLStyledProps<"span">` | Props for switch label element. |
| `name` | - | `string` | The HTML `name` attribute used for forms. |
| `onBlur` | - | `FocusEventHandler` | The callback invoked when the switch is blurred. |
| `onChange` | - | `ChangeEventHandler` | The callback invoked when the checked state changes. |
| `onFocus` | - | `FocusEventHandler` | The callback invoked when the switch is focused. |
| `readOnly` | `false` | `boolean` | If `true`, the field will be readonly. |
| `required` | `false` | `boolean` | If `true`, the field will be required. |
| `tabIndex` | - | `number` | The tab-index property of the underlying input element. |
| `value` | - | `Y` | The value to be used in the switch input. |
## Accessibility
The `Switch` follows the [WAI-ARIA - Switch Pattern](https://www.w3.org/WAI/ARIA/apg/patterns/switch/) for accessibility.
Use the same label for `Switch` regardless of its state.
### 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
| Element | Roles and Attributes | Usage |
| ------------------------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `label.ui-switch__root > input` | `role="switch"` | Indicates that this is a switch. |
| | `aria-checked` | Sets to `"true"` when on and `"false"` when 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. |
## Similar Components
- [Checkbox](https://yamada-ui.com/docs/components/checkbox.md): `Checkbox` is a component used for allowing users to select multiple values from multiple options.
- [NativeSelect](https://yamada-ui.com/docs/components/native-select.md): `NativeSelect` is a component used for allowing users to select one value from a list of options. It displays a native dropdown list provided by the browser (user agent).
- [Radio](https://yamada-ui.com/docs/components/radio.md): `Radio` is a component used for allowing users to select one option from multiple choices.
- [Rating](https://yamada-ui.com/docs/components/rating.md): `Rating` is a component used to allow users to provide ratings.
- [SegmentedControl](https://yamada-ui.com/docs/components/segmented-control.md): `SegmentedControl` is a component used for allowing users to select one option from multiple choices.
- [CheckboxCard](https://yamada-ui.com/docs/components/checkbox-card.md): `CheckboxCard` is a component used for allowing users to select multiple values from multiple options.
- [RadioCard](https://yamada-ui.com/docs/components/radio-card.md): `RadioCard` is a component used for allowing users to select one option from multiple choices.
- [Toggle](https://yamada-ui.com/docs/components/toggle.md): `Toggle` is a component that has two states: on or off.
## Uses Components & Hooks
- [Field](https://yamada-ui.com/docs/components/field.md): `Field` is a component used to group form elements with label, helper message, error message, etc.