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:
Example not foundExample not foundExample not foundExample not foundControlled
Use the pressed and onPressedChange props to control the toggle's state.
Example not foundExample not foundExample not foundExample not foundDisabled
Use the disabled prop to disable the toggle.
Example not foundExample not foundExample not foundExample not foundIndicator
Use the Toggle.Indicator component to render different indicators based on the state of the toggle.
Example not foundExample not foundExample not foundExample not foundAPI Reference
Props
Root
| 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. |
| Data Attribute | Value |
|---|---|
[data-scope] | toggle |
[data-part] | root |
[data-state] | "on" | "off" |
[data-pressed] | Present when pressed |
[data-disabled] | Present when disabled |
Indicator
| 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. |
| Data 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 |
|---|---|
pressed | booleanWhether the toggle is pressed. |
disabled | booleanWhether the toggle is disabled. |
setPressed | (pressed: boolean) => voidSets the pressed state of the toggle. |