base-ui-svelte

Switch

A control that allows the user to toggle between checked and not checked.

View as Markdown

API Reference

Props mirror Base UI React where applicable, adapted for Svelte 5. Native HTML attributes for the rendered element are also accepted unless noted.

Switch.Root

Represents the switch itself. Renders a <span> element and a hidden <input> beside.

Props

checked boolean —
Name
checked
Description

Controlled checked state.

Type
boolean | undefined
defaultChecked boolean false
Description

Uncontrolled initial checked state.

Type
boolean
Default
false
onCheckedChange function —
Description

Event handler called when the checked state changes.

Type
((checked: boolean, event: Event) => void) | undefined
disabled boolean false
Description

Whether the control ignores user interaction.

Type
boolean
Default
false
required boolean false
Description

Whether the user must activate the switch before submitting a form.

Type
boolean
Default
false
name string —
Name
name
Description

Identifies the field when a form is submitted.

Type
string | undefined
value string —
Name
value
Description

Controlled value.

Type
string | undefined
children snippet —
Description

Content rendered inside the part.

Type
Snippet<[{ checked: boolean; disabled: boolean }]>

Data attributes

AttributeDescription
data-checkedPresent when the switch is checked.
data-disabledPresent when the switch is disabled.
data-uncheckedPresent when the switch is not checked.

Switch.Thumb

The movable part of the switch that indicates whether the switch is on or off. Renders a <span>.

Props

children snippet —
Description

Content rendered inside the part.

Type
Snippet

Data attributes

AttributeDescription
data-checkedPresent when the switch is checked.
data-disabledPresent when the switch is disabled.
data-uncheckedPresent when the switch is not checked.