<script setup lang="ts">
import { ASwitchRoot, ASwitchThumb } from 'akar';
import { ref } from 'vue';
const switchState = ref(false);
</script>
<template>
<div class="flex items-start relative">
<div class="flex h-5 items-center">
<ASwitchRoot
id="airplane-mode"
v-model="switchState"
class="border-2 border-transparent rounded-full inline-flex shrink-0 w-9 transition-colors-280 items-center focus-visible:(outline-2 outline-primary outline-offset-2) data-[state=checked]:bg-primary data-[state=unchecked]:bg-background-accented"
>
<ASwitchThumb
class="group rounded-full bg-background flex size-4 pointer-events-none ring-0 shadow-lg transition-transform-280 items-center justify-center data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0 data-[state=checked]:rtl:-translate-x-4 data-[state=unchecked]:rtl:-translate-x-0"
/>
</ASwitchRoot>
</div>
<div class="text-sm ms-2">
<label
class="color-text font-medium block"
for="airplane-mode"
>
Airplane mode
</label>
</div>
</div>
</template>
Import all parts and piece them together.
<script setup>
import { ASwitchRoot, ASwitchThumb } from 'akar';
</script>
<template>
<ASwitchRoot>
<ASwitchThumb />
</ASwitchRoot>
</template>
One benefit of using Akar is its flexibility and low-level control over the components. However, this also means that you may need to manually construct more complex UI elements by combining multiple Akar components together.
If you feel there's a lot of elements that needs to be constructed manually using Akar, consider using Pohon UI instead. It provides a higher-level abstraction over Akar components with pre-defined styles and behaviors that can help you build UIs faster.
Contains all the parts of a switch. An input will also render when used within a form to ensure events propagate correctly.
| Prop | Default | Type |
|---|---|---|
as | 'button' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
defaultValue | booleanThe state of the switch when it is initially rendered. Use when you do not need to control its state. | |
disabled | booleanWhen | |
id | string | |
modelValue | boolean | null | |
name | stringThe name of the field. Submitted with its owning form as part of a name/value pair. | |
required | booleanWhen | |
value | 'on' | stringThe value given as data when submitted with a |
| Event | Type |
|---|---|
update:modelValue | [payload: boolean]Event handler called when the value of the switch changes. |
| Slot | Type |
|---|---|
modelValue | booleanThe controlled state of the switch. Can be bind as |
| Attribute | Value |
|---|---|
[data-state] | 'checked' | 'unchecked' |
[data-disabled] | Present when disabled |
The thumb that is used to visually indicate whether the switch is on or off.
| Prop | Default | Type |
|---|---|---|
as | 'span' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Attribute | Value |
|---|---|
[data-state] | 'checked' | 'unchecked' |
[data-disabled] | Present when disabled |
Adheres to the switch role requirements.
| Key | Description |
|---|---|
Space | Toggles the component's state. |
Enter | Toggles the component's state. |