Switch
A control element that allows for a binary selection.
Anatomy
To set up the switch correctly, you'll need to understand its anatomy and how we name its parts.
Each part includes a
data-partattribute to help identify them in the DOM.
Design impact on the asChild property
The Switch.Root element of the switch is a label element. This is because the switch is a form
control and should be associated with a label to provide context and meaning to the user. Otherwise,
the HTML and accessibility structure will be invalid.
If you need to use the
asChildproperty, make sure that thelabelelement is the direct child of theSwitch.Rootcomponent.
Examples
Learn how to use the Switch component in your project. Let's take a look at the most basic
example:
import { Switch } from '@ark-ui/react'
export const Basic = () => (
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
)
import { Switch } from '@ark-ui/solid'
export const Basic = () => (
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
)
<script setup lang="ts">
import { Switch } from '@ark-ui/vue'
</script>
<template>
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
</template>
Controlled Switch
For a controlled Switch component, the state of the toggle is managed using the checked prop, and
updates when the onCheckedChange event handler is called:
import { useState } from 'react'
import { Switch } from '@ark-ui/react'
export const Controlled = () => {
const [checked, setChecked] = useState(false)
return (
<Switch.Root checked={checked} onCheckedChange={(e) => setChecked(e.checked)}>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
)
}
import { createSignal } from 'solid-js'
import { Switch } from '@ark-ui/solid'
export const Controlled = () => {
const [checked, setChecked] = createSignal(false)
return (
<Switch.Root checked={checked()} onCheckedChange={(e) => setChecked(e.checked)}>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
)
}
<script setup lang="ts">
import { ref } from 'vue'
import { Switch } from '@ark-ui/vue'
const checked = ref(true)
</script>
<template>
<Switch.Root v-model:checked="checked">
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
</template>
Render Prop Usage
The Switch component also allows for a render prop, granting direct access to its internal state. This enables you to dynamically adjust and customize aspects of the component based on its current state:
import { Switch } from '@ark-ui/react'
export const RenderProp = () => (
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Context>
{(context) => (
<Switch.Label>Feature is {context.checked ? 'enabled' : 'disabled'}</Switch.Label>
)}
</Switch.Context>
<Switch.HiddenInput />
</Switch.Root>
)
import { Switch } from '@ark-ui/solid'
export const RenderProp = () => (
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Context>
{(context) => (
<Switch.Label>Feature is {context().checked ? 'enabled' : 'disabled'}</Switch.Label>
)}
</Switch.Context>
<Switch.HiddenInput />
</Switch.Root>
)
<script setup lang="ts">
import { Switch } from '@ark-ui/vue'
</script>
<template>
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Context v-slot="api">
<Switch.Label>Feature is {{ api.checked ? 'enabled' : 'disabled' }}</Switch.Label>
</Switch.Context>
<Switch.HiddenInput />
</Switch.Root>
</template>
Using the Field Component
The Field component helps manage form-related state and accessibility attributes of a switch.
It includes handling ARIA labels, helper text, and error text to ensure proper accessibility.
import { Field, Switch } from '@ark-ui/react'
export const WithField = (props: Field.RootProps) => (
<Field.Root {...props}>
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
<Field.HelperText>Additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
)
import { Field, Switch } from '@ark-ui/solid'
export const WithField = (props: Field.RootProps) => (
<Field.Root {...props}>
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
<Field.HelperText>Additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
)
<script setup lang="ts">
import { Field, Switch } from '@ark-ui/vue'
</script>
<template>
<Field.Root>
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
<Field.HelperText>Additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
</template>
API Reference
Root
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
checked | booleanWhether the switch is checked. | |
disabled | booleanWhether 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 | booleanIf `true`, the switch is marked as invalid. | |
label | stringSpecifies the localized strings that identifies the accessibility elements and their states | |
name | stringThe name of the input field in a switch (Useful for form submission). | |
onCheckedChange | (details: CheckedChangeDetails) => voidFunction to call when the switch is clicked. | |
readOnly | booleanWhether the switch is read-only | |
required | booleanIf `true`, the switch input is marked as required, | |
value | 'on' | string | numberThe value of switch input. Useful for form submission. |
| Data Attribute | Value |
|---|---|
[data-active] | Present when active or pressed |
[data-focus] | Present when focused |
[data-readonly] | Present when read-only |
[data-hover] | Present when hovered |
[data-disabled] | Present when disabled |
[data-state] | "checked" | "unchecked" |
[data-invalid] | Present when invalid |
Control
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-active] | Present when active or pressed |
[data-focus] | Present when focused |
[data-readonly] | Present when read-only |
[data-hover] | Present when hovered |
[data-disabled] | Present when disabled |
[data-state] | "checked" | "unchecked" |
[data-invalid] | Present when invalid |
HiddenInput
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Label
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-active] | Present when active or pressed |
[data-focus] | Present when focused |
[data-readonly] | Present when read-only |
[data-hover] | Present when hovered |
[data-disabled] | Present when disabled |
[data-state] | "checked" | "unchecked" |
[data-invalid] | Present when invalid |
RootProvider
| Prop | Default | Type |
|---|---|---|
value | UseSwitchReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Thumb
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-active] | Present when active or pressed |
[data-focus] | Present when focused |
[data-readonly] | Present when read-only |
[data-hover] | Present when hovered |
[data-disabled] | Present when disabled |
[data-state] | "checked" | "unchecked" |
[data-invalid] | Present when invalid |
Accessibility
Complies with the Switch WAI-ARIA design pattern.
Keyboard Support
| Key | Description |
|---|---|
SpaceEnter | Toggle the switch |