Toggle

A two-state button that can be toggled on or off.

Loading...

Anatomy

rootindicator
<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.

AttributeDefaultType
asChild
boolean

Use 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
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.

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.

AttributeDescription
[data-toggle-root]<uid>
[data-pressed]Present when pressed
[data-disabled]Present when disabled

Indicator

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[data-toggle-indicator]<uid>
[data-disabled]Present when disabled
[data-pressed]Present when pressed

Context

API

PropertyType
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.

getRootState
() => RootState

Returns 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.