Toggle
A two-state button that can be toggled on or off.
Anatomy
<Toggle.Root>
<Toggle.Indicator />
</Toggle.Root>
Examples
Controlled
Use the pressed and onPressedChange props to control the toggle's state.
Disabled
Use the disabled prop to disable the toggle.
Indicator
Use the Toggle.Indicator component to render different indicators based on the state of the toggle.
API Reference
Props
Root
Renders a <button> element.
| 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. | |
defaultPressed | booleanThe default pressed state of the toggle. | |
onPressedChange | (pressed: boolean) => voidEvent handler called when the pressed state of the toggle changes. | |
pressed | booleanThe pressed state of the toggle. |
| Attribute | Description |
|---|---|
[data-scope] | toggle |
[data-part] | root |
[data-state] | "on" | "off" |
[data-pressed] | Present when pressed |
[data-disabled] | Present when disabled |
Indicator
Renders a <div> element.
| 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. | |
fallback | string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<...>The fallback content to render when the toggle is not pressed. |
| Attribute | Description |
|---|---|
[data-scope] | toggle |
[data-part] | indicator |
[data-disabled] | Present when disabled |
[data-pressed] | Present when pressed |
[data-state] | "on" | "off" |
Context
API
| Property | Type |
|---|---|
pressed | booleanWhether the toggle is pressed. |
disabled | booleanWhether the toggle is disabled. |
setPressed | (pressed: boolean) => voidSets the pressed state of the toggle. |