Skip to main content

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

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

Read more about installing Components and Primitives.

Set defaultChecked to render the switch on.

Set disabled to lock the switch in its current state.

Wrap the switch in a Field for a helper text, validation messages and the name it submits under.

Receive product updates and occasional offers.

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.

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
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-switch-root<uid>
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

Displays the visible label text for the switch. 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-switch-label<uid>
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

Displays the visual switch track. 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-switch-control<uid>
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

Displays the movable thumb inside the switch track. 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-switch-thumb<uid>
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

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

NameDescriptionRequiredDefault
stateEnum\SwitchIndicatorState
The indicator state to render.
Yes
-
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
-

Provides the native checkbox input for form submission. Renders an <input> element, with the empty-value input that submits '' while the switch is off in front of it.

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
-
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.
getRootState() => RootStateReturns the state of the switch
KeyDescription
SpaceEnterToggle the switch