Skip to main content

A toggle component for turning a single option on or off.

Features

Installation

typo3 ui:add switch

Read more about installing Components and Primitives.

Examples

Default Checked

Set the switch to be checked by default.

Disabled

Prevent interaction with the switch.

With Form Field

Use the switch with the Field component for descriptions and validation.

Receive product updates and occasional offers.

API Reference

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.

switch.root

Groups the switch control, label, and input into one interactive label. Renders a <label> element.

NameDescriptionRequiredDefault
disabledboolean
Whether the switch is disabled. Inherited from a surrounding field when available.
No
-
invalidboolean
If `true`, the switch is marked as invalid. Inherited from a surrounding field when available.
No
-
requiredboolean
If `true`, the switch input is marked as required. Inherited from a surrounding field when available.
No
-
defaultCheckedboolean
The initial checked state of the switch when rendered. Use when you don't need to control the checked state of the switch.
No
false
namestring
The name of the input field in a switch. Useful for form submission. Inherited from a surrounding field when available.
No
-
formstring
The id of the form that the switch belongs to.
No
-
readOnlyboolean
Whether the switch is read-only. Inherited from a surrounding field when available.
No
-
valuestring
The value of switch input. Useful for form submission.
No
1
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
[]
controlledboolean
If true, the component is meant to be initialized manually inside another component
No
false
classstring
The CSS class(es) to be applied to the component.
No
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]

Rendered data attributes

AttributeDescription
data-activePresent when active or pressed
data-focusPresent when focused
data-focus-visiblePresent when focused with keyboard
data-readonlyPresent when read-only
data-hoverPresent when hovered
data-disabledPresent when disabled
data-state"checked" | "unchecked"
data-invalidPresent when invalid
data-requiredPresent when required

switch.label

Displays the visible label text for the switch. Renders a <span> element.

NameDescriptionRequiredDefault
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
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]

Rendered data attributes

AttributeDescription
data-activePresent when active or pressed
data-focusPresent when focused
data-focus-visiblePresent when focused with keyboard
data-readonlyPresent when read-only
data-hoverPresent when hovered
data-disabledPresent when disabled
data-state"checked" | "unchecked"
data-invalidPresent when invalid
data-requiredPresent when required

switch.control

Displays the visual switch track. Renders a <span> element.

NameDescriptionRequiredDefault
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
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]

Rendered data attributes

AttributeDescription
data-activePresent when active or pressed
data-focusPresent when focused
data-focus-visiblePresent when focused with keyboard
data-readonlyPresent when read-only
data-hoverPresent when hovered
data-disabledPresent when disabled
data-state"checked" | "unchecked"
data-invalidPresent when invalid
data-requiredPresent when required

switch.thumb

Displays the movable thumb inside the switch track. Renders a <span> element.

NameDescriptionRequiredDefault
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
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]

Rendered data attributes

AttributeDescription
data-activePresent when active or pressed
data-focusPresent when focused
data-focus-visiblePresent when focused with keyboard
data-readonlyPresent when read-only
data-hoverPresent when hovered
data-disabledPresent when disabled
data-state"checked" | "unchecked"
data-invalidPresent when invalid
data-requiredPresent when required

switch.indicator

Displays UI for a specific switch state. Renders a <span> element.

NameDescriptionRequiredDefault
stateEnum\SwitchIndicatorState
The indicator state to render.
Yes
-
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
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]

switch.hiddenInput

Provides the native checkbox input for form submission. Renders an <input> element.

NameDescriptionRequiredDefault
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
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]

Machine JavaScript API

NameTypeDescription
checkedbooleanWhether the switch is checked
disabledboolean | undefinedWhether the switch is disabled
focusedboolean | undefinedWhether the switch is focused
setChecked(checked: boolean) => voidSets the checked state of the switch.
toggleCheckedVoidFunctionToggles the checked state of the switch.

Accessibility

KeyDescription
SpaceEnterToggle the switch

Anatomy

<primitives:switch.root>
    <primitives:switch.control>
        <primitives:switch.thumb />
    </primitives:switch.control>
    <primitives:switch.label />
    <primitives:switch.hiddenInput />
</primitives:switch.root>