Toggle
A two-state button that can be toggled on or off.
Anatomy
<Toggle.Root>
<Toggle.Indicator />
</Toggle.Root>
Examples
Controlled
Pass pressed and update it in onPressedChange.
Disabled
Indicator
Toggle.Indicator renders different content based on the pressed state.
API Reference
Props
Root
Defines the <button> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. 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. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<ToggleRootState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-toggle-root] | <uid> |
[data-pressed] | Present when pressed |
[data-disabled] | Present when disabled |
Indicator
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. 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. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-toggle-indicator] | <uid> |
[data-disabled] | Present when disabled |
[data-pressed] | Present when pressed |
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. |
getRootState | () => RootStateReturns the state of the toggle |
getRootProps | () => T["element"]Props for the root `button` element. |
getIndicatorProps | () => T["element"]Props for the optional visual indicator (e.g. icon) element. |