# Toggle URL: https://ark-ui.com/docs/components/toggle Source: https://raw.githubusercontent.com/chakra-ui/ark/refs/heads/main/website/src/content/pages/components/toggle.mdx A two-state button that can be toggled on or off. --- ## Examples Here's a basic example of how to use the `Toggle` component: **Example: basic** #### React ```tsx import { Toggle } from '@ark-ui/react/toggle' import { BoldIcon } from 'lucide-react' export const Basic = () => { return ( ) } ``` #### Solid ```tsx import { Toggle } from '@ark-ui/solid/toggle' import { BoldIcon } from 'lucide-solid' export const Basic = () => { return ( ) } ``` #### Vue ```vue ``` #### Svelte ```svelte ``` ### Controlled Use the `pressed` and `onPressedChange` props to control the toggle's state. **Example: controlled** #### React ```tsx import { Toggle } from '@ark-ui/react/toggle' import { Volume, VolumeOff } from 'lucide-react' import { useState } from 'react' export const Controlled = () => { const [pressed, setPressed] = useState(false) return (
{pressed ? : }

Volume is {pressed ? 'unmuted' : 'muted'}

) } ``` #### Solid ```tsx import { Toggle } from '@ark-ui/solid/toggle' import { Volume, VolumeOff } from 'lucide-solid' import { Show, createSignal } from 'solid-js' export const Controlled = () => { const [pressed, setPressed] = createSignal(false) return (
}>

Volume is {pressed() ? 'unmuted' : 'muted'}

) } ``` #### Vue ```vue ``` #### Svelte ```svelte
{#if pressed} {:else} {/if}

Volume is {pressed ? 'unmuted' : 'muted'}

``` ### Disabled Use the `disabled` prop to disable the toggle. **Example: disabled** #### React ```tsx import { Toggle } from '@ark-ui/react/toggle' import { BoldIcon } from 'lucide-react' export const Disabled = () => { return ( ) } ``` #### Solid ```tsx import { Toggle } from '@ark-ui/solid/toggle' import { BoldIcon } from 'lucide-solid' export const Disabled = () => { return ( ) } ``` #### Vue ```vue ``` #### Svelte ```svelte ``` ### Indicator Use the `Toggle.Indicator` component to render different indicators based on the state of the toggle. **Example: indicator** #### React ```tsx import { Toggle } from '@ark-ui/react/toggle' import { Volume, VolumeOff } from 'lucide-react' export const Indicator = () => { return ( }> ) } ``` #### Solid ```tsx import { Toggle } from '@ark-ui/solid/toggle' import { Volume, VolumeOff } from 'lucide-solid' export const Indicator = () => { return ( }> ) } ``` #### Vue ```vue ``` #### Svelte ```svelte {#snippet fallback()} {/snippet} ``` ## API Reference ### Props **Component API Reference** #### React **Root Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `defaultPressed` | `boolean` | No | The default pressed state of the toggle. | | `onPressedChange` | `(pressed: boolean) => void` | No | Event handler called when the pressed state of the toggle changes. | | `pressed` | `boolean` | No | The pressed state of the toggle. | **Root Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | toggle | | `[data-part]` | root | | `[data-state]` | "on" | "off" | | `[data-pressed]` | Present when pressed | | `[data-disabled]` | Present when disabled | **Indicator Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `fallback` | `string | number | bigint | boolean | ReactElement> | Iterable | ReactPortal | Promise<...>` | No | The fallback content to render when the toggle is not pressed. | **Indicator Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | toggle | | `[data-part]` | indicator | | `[data-disabled]` | Present when disabled | | `[data-pressed]` | Present when pressed | | `[data-state]` | "on" | "off" | #### Solid **Root Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `(props: ParentProps<'button'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `defaultPressed` | `boolean` | No | The default pressed state of the toggle. | | `onPressedChange` | `(pressed: boolean) => void` | No | Event handler called when the pressed state of the toggle changes. | | `pressed` | `boolean` | No | The pressed state of the toggle. | **Root Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | toggle | | `[data-part]` | root | | `[data-state]` | "on" | "off" | | `[data-pressed]` | Present when pressed | | `[data-disabled]` | Present when disabled | **Indicator Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `fallback` | `number | boolean | Node | (string & {}) | ArrayElement` | No | | **Indicator Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | toggle | | `[data-part]` | indicator | | `[data-disabled]` | Present when disabled | | `[data-pressed]` | Present when pressed | | `[data-state]` | "on" | "off" | #### Svelte **Root Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `Snippet<[PropsFn<'button'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `defaultPressed` | `boolean` | No | The default pressed state of the toggle. | | `disabled` | `boolean` | No | Whether the toggle is disabled. | | `onPressedChange` | `(pressed: boolean) => void` | No | Event handler called when the pressed state of the toggle changes. | | `pressed` | `boolean` | No | The pressed state of the toggle. | | `ref` | `Element` | No | | **Root Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | toggle | | `[data-part]` | root | | `[data-state]` | "on" | "off" | | `[data-pressed]` | Present when pressed | | `[data-disabled]` | Present when disabled | **Context Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `render` | `Snippet<[UseToggleContext]>` | Yes | | **Indicator Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `fallback` | `Snippet<[]>` | No | The fallback content to render when the toggle is not pressed. | | `ref` | `Element` | No | | **Indicator Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | toggle | | `[data-part]` | indicator | | `[data-disabled]` | Present when disabled | | `[data-pressed]` | Present when pressed | | `[data-state]` | "on" | "off" | ### Context These are the properties available when using `Toggle.Context`, `useToggleContext` hook or `useToggle` hook. **API:** | Property | Type | Description | |----------|------|-------------| | `pressed` | `boolean` | Whether the toggle is pressed. | | `disabled` | `boolean` | Whether the toggle is disabled. | | `setPressed` | `(pressed: boolean) => void` | Sets the pressed state of the toggle. |