Kobalte.v0.13.1

Switch

A control that allows users to choose one of two values: on or off.

Import

ts
import { Switch } from "@kobalte/core/switch";
// or
import { Root, Input, ... } from "@kobalte/core/switch";
// or (deprecated)
import { Switch } from "@kobalte/core";
ts
import { Switch } from "@kobalte/core/switch";
// or
import { Root, Input, ... } from "@kobalte/core/switch";
// or (deprecated)
import { Switch } from "@kobalte/core";

Features

  • Follow the WAI ARIA Switch design pattern.
  • Built with a native HTML <input> element, which is visually hidden to allow custom styling.
  • Syncs with form reset events.
  • Labeling support for assistive technology.
  • Support for description and error message help text linked to the input via ARIA.
  • Can be controlled or uncontrolled.

Anatomy

The switch consists of:

  • Switch: The root container for a switch.
  • Switch.Input: The native html input that is visually hidden in the switch.
  • Switch.Control: The element that visually represents a switch.
  • Switch.Thumb: The thumb that is used to visually indicate whether the switch is on or off.
  • Switch.Label: The label that gives the user information on the switch.
  • Switch.Description: The description that gives the user more information on the switch.
  • Switch.ErrorMessage: The error message that gives the user information about how to fix a validation error on the switch.
tsx
<Switch>
<Switch.Label />
<Switch.Description />
<Switch.ErrorMessage />
<Switch.Input />
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
</Switch>
tsx
<Switch>
<Switch.Label />
<Switch.Description />
<Switch.ErrorMessage />
<Switch.Input />
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
</Switch>

Example

Usage

Default checked

An initial, uncontrolled value can be provided using the defaultChecked prop.

tsx
<Switch defaultChecked>...</Switch>
tsx
<Switch defaultChecked>...</Switch>

Controlled checked

The checked prop can be used to make the checked state controlled. The onChange event is fired when the user toggle the switch, and receives the new value.

Airplane mode is inactive.

tsx
import { createSignal } from "solid-js";
export function ControlledExample() {
const [checked, setChecked] = createSignal(false);
return (
<>
<Switch checked={checked()} onChange={setChecked}>
...
</Switch>
<p>Airplane mode is {checked() ? "active" : "inactive"}.</p>
</>
);
}
tsx
import { createSignal } from "solid-js";
export function ControlledExample() {
const [checked, setChecked] = createSignal(false);
return (
<>
<Switch checked={checked()} onChange={setChecked}>
...
</Switch>
<p>Airplane mode is {checked() ? "active" : "inactive"}.</p>
</>
);
}

Description

The Switch.Description component can be used to associate additional help text with a switch.

Disable all network connections.
tsx
<Switch>
<Switch.Label>Airplane mode</Switch.Label>
<Switch.Description>Disable all network connections.</Switch.Description>
<Switch.Input />
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
</Switch>
tsx
<Switch>
<Switch.Label>Airplane mode</Switch.Label>
<Switch.Description>Disable all network connections.</Switch.Description>
<Switch.Input />
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
</Switch>

Error message

The Switch.ErrorMessage component can be used to help the user fix a validation error. It should be combined with the validationState prop to semantically mark the switch as invalid for assistive technologies.

By default, it will render only when the validationState prop is set to invalid, use the forceMount prop to always render the error message (ex: for usage with animation libraries).

You must enable airplane mode.
tsx
import { createSignal } from "solid-js";
function ErrorMessageExample() {
const [checked, setChecked] = createSignal(false);
return (
<Switch
checked={checked()}
onChange={setChecked}
validationState={!checked() ? "invalid" : "valid"}
>
<Switch.Label>Airplane mode</Switch.Label>
<Switch.ErrorMessage>You must enable airplane mode.</Switch.ErrorMessage>
<Switch.Input />
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
</Switch>
);
}
tsx
import { createSignal } from "solid-js";
function ErrorMessageExample() {
const [checked, setChecked] = createSignal(false);
return (
<Switch
checked={checked()}
onChange={setChecked}
validationState={!checked() ? "invalid" : "valid"}
>
<Switch.Label>Airplane mode</Switch.Label>
<Switch.ErrorMessage>You must enable airplane mode.</Switch.ErrorMessage>
<Switch.Input />
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
</Switch>
);
}

HTML forms

The name and value props can be used for integration with HTML forms.

tsx
function HTMLFormExample() {
const onSubmit = (e: SubmitEvent) => {
// handle form submission.
};
return (
<form onSubmit={onSubmit}>
<Switch name="airplane-mode" value="on">
...
</Switch>
<div>
<button type="reset">Reset</button>
<button type="submit">Submit</button>
</div>
</form>
);
}
tsx
function HTMLFormExample() {
const onSubmit = (e: SubmitEvent) => {
// handle form submission.
};
return (
<form onSubmit={onSubmit}>
<Switch name="airplane-mode" value="on">
...
</Switch>
<div>
<button type="reset">Reset</button>
<button type="submit">Submit</button>
</div>
</form>
);
}

API Reference

Switch

Switch is equivalent to the Root import from @kobalte/core/switch (and deprecated Switch.Root).

PropDescription
checkedboolean
The controlled checked state of the switch.
defaultCheckedboolean
The default checked state when initially rendered. Useful when you do not need to control the checked state.
onChange(checked: boolean) => void
Event handler called when the checked state of the switch changes.
namestring
The name of the switch, used when submitting an HTML form. See MDN.
valuestring
The value of the switch, used when submitting an HTML form. See MDN.
validationState'valid' | 'invalid'
Whether the switch should display its "valid" or "invalid" visual styling.
requiredboolean
Whether the user must check the switch before the owning form can be submitted.
disabledboolean
Whether the switch is disabled.
readOnlyboolean
Whether the switch can be checked but not changed by the user.
Render PropDescription
checkedAccessor<boolean>
Whether the switch is checked or not.
Data attributeDescription
data-validPresent when the switch is valid according to the validation rules.
data-invalidPresent when the switch is invalid according to the validation rules.
data-requiredPresent when the switch is required.
data-disabledPresent when the switch is disabled.
data-readonlyPresent when the switch is read only.
data-checkedPresent when the switch is checked.

Switch.Input, Switch.Control, Switch.Thumb, Switch.Label, Switch.Description and Switch.ErrorMessage shares the same data-attributes.

Switch.ErrorMessage

PropDescription
forceMountboolean
Used to force mounting when more control is needed. Useful when controlling animation with SolidJS animation libraries.

Rendered elements

ComponentDefault rendered element
Switchdiv
Switch.Inputinput
Switch.Controldiv
Switch.Indicatordiv
Switch.Labellabel
Switch.Descriptiondiv
Switch.ErrorMessagediv

Accessibility

Keyboard Interactions

KeyDescription
SpaceToggles the switch on and off.
Previous
Slider
Next
Tabs