Toggle
A two-state button that can be toggled on or off.
Examples
Here's a basic example of how to use the Toggle
component:
import { Toggle } from '@ark-ui/react/toggle'
import { BoldIcon } from 'lucide-react'
export const Basic = () => {
return (
<Toggle.Root>
<BoldIcon />
</Toggle.Root>
)
}
import { Toggle } from '@ark-ui/solid/toggle'
import { BoldIcon } from 'lucide-solid'
export const Basic = () => {
return (
<Toggle.Root>
<BoldIcon />
</Toggle.Root>
)
}
<script setup lang="ts">
import { Toggle } from '@ark-ui/vue/toggle'
import { BoldIcon } from 'lucide-vue-next'
</script>
<template>
<Toggle.Root>
<BoldIcon />
</Toggle.Root>
</template>
Controlled
Use the pressed
and onPressedChange
props to control the toggle's state.
Disabled
Use the disabled
prop to disable the toggle.
import { Toggle } from '@ark-ui/react/toggle'
import { BoldIcon } from 'lucide-react'
export const Disabled = () => {
return (
<Toggle.Root disabled>
<BoldIcon />
</Toggle.Root>
)
}
import { Toggle } from '@ark-ui/solid/toggle'
import { BoldIcon } from 'lucide-solid'
export const Disabled = () => {
return (
<Toggle.Root disabled>
<BoldIcon />
</Toggle.Root>
)
}
<script setup lang="ts">
import { Toggle } from '@ark-ui/vue/toggle'
import { BoldIcon } from 'lucide-vue-next'
</script>
<template>
<Toggle.Root disabled>
<BoldIcon />
</Toggle.Root>
</template>
Indicator
Use the Toggle.Indicator
component to render different indicators based on the state of the toggle.
import { Toggle } from '@ark-ui/react/toggle'
import { Volume, VolumeOff } from 'lucide-react'
export const Indicator = () => {
return (
<Toggle.Root>
<Toggle.Indicator fallback={<Volume />}>
<VolumeOff />
</Toggle.Indicator>
</Toggle.Root>
)
}
import { Toggle } from '@ark-ui/solid/toggle'
import { Volume, VolumeOff } from 'lucide-solid'
export const Indicator = () => {
return (
<Toggle.Root>
<Toggle.Indicator fallback={<Volume />}>
<VolumeOff />
</Toggle.Indicator>
</Toggle.Root>
)
}
<script setup lang="ts">
import { Toggle } from '@ark-ui/vue/toggle'
import { Volume, VolumeOff } from 'lucide-vue-next'
</script>
<template>
<Toggle.Root>
<Toggle.Indicator>
<template #fallback>
<Volume />
</template>
<VolumeOff />
</Toggle.Indicator>
</Toggle.Root>
</template>
API Reference
Root
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
defaultPressed | boolean The default pressed state of the toggle. | |
onPressedChange | (pressed: boolean) => void Event handler called when the pressed state of the toggle changes. | |
pressed | boolean The pressed state of the toggle. |
Indicator
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
fallback | type ONLY_FOR_FORMAT =
| string
| number
| boolean
| ReactElement<any, string | JSXElementConstructor<any>>
| Iterable<ReactNode>
| ReactPortal The fallback content to render when the toggle is not pressed. |