Skip to main content

A button that toggles between pressed and unpressed.

<primitives:toggle.root>
    <primitives:toggle.indicator />
</primitives:toggle.root>
typo3 ui:add toggle

Read more about installing Components and Primitives.

Set the toggle to be pressed by default.

Prevent the toggle from being pressed.

The indicator carries the same data-pressed and data-disabled attributes as the root, so it can show the state of the toggle without any extra markup.

The following tables cover the available props of the Fluid Primitives. For a full list of available client side props and methods, see the Zag.js Machine API.

Provides the pressed state and toggles it on click. Renders a <button> element.

NameDescriptionRequiredDefault
defaultPressedboolean
The initial pressed state of the toggle when rendered. Use when you don't need to control the pressed state of the toggle.
No
false
disabledboolean
Whether the toggle is disabled.
No
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
rootIdstring
The root ID of the component, used for hydration and identification.
No
-
idsarray
The IDs of of the component parts for composition.
No
[]
autoMountboolean
Whether the client initializes the component on its own. Set it to false to mount it yourself with `mount()`
No
true
classstring
The CSS class(es) to be applied to the component.
No
-

Rendered data attributes

AttributeDescription
data-toggle-root<uid>
data-pressedPresent when pressed
data-disabledPresent when disabled

Visual indicator of the pressed state, e.g. an icon or a dot. Renders a <span> element.

NameDescriptionRequiredDefault
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-

Rendered data attributes

AttributeDescription
data-toggle-indicator<uid>
data-disabledPresent when disabled
data-pressedPresent when pressed
NameTypeDescription
pressedbooleanWhether the toggle is pressed.
disabledbooleanWhether 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.
KeyDescription
SpaceEnterToggle the pressed state