Build faster with Premium Chakra UI Components 💎

Learn more
Skip to Content
DocsPlaygroundGuidesBlog
Sponsor

Switch

Used to capture a binary state

SourceStorybookRecipeArk

Setup

If you don't already have the snippet, run the following command to add the switch snippet

npx @chakra-ui/cli snippet add switch

The snippet includes a closed component composition for the Switch component.

Usage

import { Switch } from "@/components/ui/switch"
<Switch>Label</Switch>

Examples

Sizes

Use the size prop to change the size of the switch component.

Variants

Use the variant prop to change the visual style of the switch.

Colors

Use the colorPalette prop to change the color scheme of the component.

gray

red

green

blue

teal

pink

purple

cyan

orange

yellow

Controlled

Use the checked and onCheckedChange prop to control the state of the switch.

Hook Form

Here's an example of integrating the switch with react-hook-form.

Disabled

Use the disabled prop to disable the switch.

Invalid

Use the invalid prop to indicate an error state for the switch.

Tooltip

Here's an example of a switch with a tooltip.

Track Label

Use the trackLabel prop to display different label based on the checked state.

Thumb Label

Use the thumbLabel prop to add an icon to the switch thumb.

Props

Root

PropDefaultType
value '\'on\''
string | number

The value of switch input. Useful for form submission.

colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' | 'accent'

The color palette of the component

variant 'solid'
'solid' | 'raised'

The variant of the component

size 'md'
'xs' | 'sm' | 'md' | 'lg'

The size of the component

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
checked
boolean

Whether the switch is checked.

disabled
boolean

Whether the switch is disabled.

ids
Partial<{ root: string hiddenInput: string control: string label: string thumb: string }>

The ids of the elements in the switch. Useful for composition.

invalid
boolean

If `true`, the switch is marked as invalid.

label
string

Specifies the localized strings that identifies the accessibility elements and their states

name
string

The name of the input field in a switch (Useful for form submission).

onCheckedChange
(details: CheckedChangeDetails) => void

Function to call when the switch is clicked.

readOnly
boolean

Whether the switch is read-only

required
boolean

If `true`, the switch input is marked as required,

as
React.ElementType

The underlying element to render.

unstyled
boolean

Whether to remove the component's style.

Previous

Steps

Next

Tabs