Switch
A control element that allows for a binary selection.
A control element that allows for a binary selection.
To set up the switch correctly, you’ll need to understand its anatomy and how we name its parts.
Each part includes a
data-part
attribute to help identify them in the DOM.
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'
const Basic = () => (
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
</Switch.Root>
)
import { Switch } from '@ark-ui/solid'
const Basic = () => (
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
</Switch.Root>
)
<script setup lang="ts">
import { ref } from 'vue'
import { Switch } from '@ark-ui/vue'
const checked = ref(false)
</script>
<template>
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
</Switch.Root>
</template>
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 { Switch } from '@ark-ui/react'
import { useState } from 'react'
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.Root>
)
}
import { Switch } from '@ark-ui/solid'
import { createSignal } from 'solid-js'
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.Root>
)
}
<script setup lang="ts">
import { ref } from 'vue'
import { Switch } from '@ark-ui/vue'
const checked = ref(false)
</script>
<template>
<Switch.Root v-model="checked">
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Label</Switch.Label>
</Switch.Root>
</template>
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'
const RenderProp = () => (
<Switch.Root>
{(api) => (
<>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Feature is {api.isChecked ? 'enabled' : 'disabled'}</Switch.Label>
</>
)}
</Switch.Root>
)
import { Switch } from '@ark-ui/solid'
const RenderProp = () => (
<Switch.Root>
{(api) => (
<>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Feature is {api().isChecked ? 'enabled' : 'disabled'}</Switch.Label>
</>
)}
</Switch.Root>
)
<script setup lang="ts">
import { ref } from 'vue'
import { Switch } from '@ark-ui/vue'
const checked = ref(false)
</script>
<template>
<Switch.Root #default="api">
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Feature is {{ api.isChecked ? 'enabled' : 'disabled' }}</Switch.Label>
</Switch.Root>
</template>
Prop | Type | Default |
---|---|---|
asChild Render as a different element type. | boolean | |
checked Whether the switch is checked. | boolean | |
defaultChecked The initial checked state of the switch. | boolean | |
dir The document's text/writing direction. | 'ltr' | 'rtl' | "ltr" |
disabled Whether the switch is disabled. | boolean | |
form The id of the form that the switch belongs to | string | |
getRootNode A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron. | () => Node | ShadowRoot | Document | |
id The unique identifier of the machine. | string | |
ids The ids of the elements in the switch. Useful for composition. | Partial<{
root: string
input: string
control: string
label: string
thumb: string
}> | |
invalid If `true`, the switch is marked as invalid. | boolean | |
label Specifies the localized strings that identifies the accessibility elements and their states | string | |
name The name of the input field in a switch (Useful for form submission). | string | |
onCheckedChange Function to call when the switch is clicked. | (details: CheckedChangeDetails) => void | |
required If `true`, the switch input is marked as required, | boolean | |
value The value of switch input. Useful for form submission. | string | number | "on" |
Prop | Type | Default |
---|---|---|
asChild Render as a different element type. | boolean |
Prop | Type | Default |
---|---|---|
asChild Render as a different element type. | boolean |
Prop | Type | Default |
---|---|---|
asChild Render as a different element type. | boolean |