Switch
A control that indicates whether a setting is on or off.
Usage guidelines
- Form controls must have an accessible name: It can be created using a
<label>element or theFieldcomponent. See Labeling a switch and the forms guide.
Anatomy
Import the component and assemble its parts:
Examples
Labeling a switch
An enclosing <label> is the simplest labeling pattern:
Rendering as a native button
By default, <Switch.Root> renders a <span> element to support enclosing labels. Prefer rendering the switch as a native button when using sibling labels (for/id).
Native buttons with wrapping labels are supported by using the render snippet to avoid invalid HTML, so the hidden input is placed outside the label:
Form integration
Use Field to handle label associations and form integration:
API reference
Root
Represents the switch itself. Renders a <span> and a hidden checkbox beside it.
checked boolean false
- Name
- Description
Whether the switch is currently active. Use
bind:checkedto share it with the parent.- Type
boolean- Default
false- Binding
bind:checked
defaultChecked boolean false
- Name
- Description
The state an empty
checkedstarts from, as on an HTML checkbox.- Type
boolean- Default
false
disabled boolean | null false
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean | null- Default
false
readonly boolean | null false
- Name
- Description
Whether the user should be unable to activate or deactivate the switch.
- Type
boolean | null- Default
false
required boolean | null false
- Name
- Description
Whether the user must activate the switch before submitting a form.
- Type
boolean | null- Default
false
nativeButton boolean false
- Name
- Description
Whether the host is a native
<button>. Settrueand render a<button>. Theidis then applied to that button instead of the hidden input.- Type
boolean- Default
false
name string | null —
- Name
- Description
Identifies the field when a form is submitted.
- Type
string | null
value string —
- Name
- Description
The value submitted with the form when the switch is on. By default, the switch submits
"on", matching a native checkbox.- Type
string
uncheckedValue string —
- Name
- Description
The value submitted with the form when the switch is off. By default, an unchecked switch does not submit a value.
- Type
string
form string | null —
- Name
- Description
Identifies the form that owns the hidden input. Useful when the switch is rendered outside the form.
- Type
string | null
id string | null —
- Name
- Description
The id of the hidden input. When
nativeButtonis true, the id is applied to the root element instead.- Type
string | null
onCheckedChange function —
- Name
- Description
Called before the checked state changes. Call
eventDetails.cancel()to veto it.- Type
(checked: boolean, eventDetails: SwitchRootChangeEventDetails) => void
onclick MouseEventHandler<HTMLElement> | null —
- Name
- Description
Runs before the switch's click handler. Call
event.preventDefault()to skip it.- Type
MouseEventHandler<HTMLElement> | null
render Snippet<[props: SwitchHostProps, state: SwitchRootState, children: Snippet | undefined]> —
- Name
- Description
Replace the default
<span>. Spreadpropsonto the host element.- Type
Snippet<[props: SwitchHostProps, state: SwitchRootState, children: Snippet | undefined]>
checked boolean
- Name
checked- Description
Whether the switch is currently active.
- Type
boolean
disabled boolean
- Name
disabled- Description
Whether the component should ignore user interaction.
- Type
boolean
readOnly boolean
- Name
readOnly- Description
Whether the user should be unable to activate or deactivate the switch.
- Type
boolean
required boolean
- Name
required- Description
Whether the user must activate the switch before submitting a form.
- Type
boolean
touched boolean
- Name
touched- Description
Whether the field has been touched.
- Type
boolean
dirty boolean
- Name
dirty- Description
Whether the field value has changed from its initial value.
- Type
boolean
valid boolean | null
- Name
valid- Description
Whether the field is valid.
nullmeans validity is not known yet.- Type
boolean | null
filled boolean
- Name
filled- Description
Whether the field has a value.
- Type
boolean
focused boolean
- Name
focused- Description
Whether the field is focused.
- Type
boolean
data-checked
data-unchecked
data-disabled
data-readonly
data-required
data-valid
data-invalid
data-dirty
data-touched
data-filled
data-focused
Thumb
The movable part of the switch that indicates whether the switch is on or off. Renders a <span>.
render Snippet<[props: HTMLAttributes<HTMLSpanElement>, state: SwitchThumbState, children: Snippet | undefined]> —
- Name
- Description
Replace the default
<span>. Spreadpropsonto the host element.- Type
Snippet<[props: HTMLAttributes<HTMLSpanElement>, state: SwitchThumbState, children: Snippet | undefined]>
checked boolean
- Name
checked- Description
Whether the switch is currently active.
- Type
boolean
disabled boolean
- Name
disabled- Description
Whether the component should ignore user interaction.
- Type
boolean
readOnly boolean
- Name
readOnly- Description
Whether the user should be unable to activate or deactivate the switch.
- Type
boolean
required boolean
- Name
required- Description
Whether the user must activate the switch before submitting a form.
- Type
boolean
touched boolean
- Name
touched- Description
Whether the field has been touched.
- Type
boolean
dirty boolean
- Name
dirty- Description
Whether the field value has changed from its initial value.
- Type
boolean
valid boolean | null
- Name
valid- Description
Whether the field is valid.
nullmeans validity is not known yet.- Type
boolean | null
filled boolean
- Name
filled- Description
Whether the field has a value.
- Type
boolean
focused boolean
- Name
focused- Description
Whether the field is focused.
- Type
boolean