Skip to main content
PROYECTOVIVIANA

Switch

A toggle switch for binary on/off states. An alternative to checkboxes when the immediate effect of the change should be visually apparent.

Import

import { ToggleSwitch } from '@proyecto-viviana/solid-spectrum';

Basic Usage

A controlled toggle switch.

Off
const [isOn, setIsOn] = createSignal(false);

<ToggleSwitch isSelected={isOn()} onChange={setIsOn} />
<span>{isOn() ? 'On' : 'Off'}</span>

With Label

Provide an accessible label for the switch.

<label class="switch-row">
  <ToggleSwitch isSelected={notifications()} onChange={setNotifications} />
  <span>Enable notifications</span>
</label>

Default Selected

Uncontrolled switch with an initial value.

<ToggleSwitch defaultSelected />

Disabled

Disabled switches cannot be toggled.

Disabled off
Disabled on
<ToggleSwitch isDisabled />
<ToggleSwitch isDisabled defaultSelected />

Props

PropTypeDefaultDescription
isSelectedbooleanControlled on/off state
defaultSelectedbooleanUncontrolled initial state
onChange(isSelected: boolean) => voidCalled when the switch is toggled
isDisabledbooleanfalsePrevents toggling
aria-labelstringAccessible label (required if no visible label)
childrenJSX.ElementOptional label content rendered alongside the switch

Accessibility

  • Uses role="switch" with aria-checked
  • Space key toggles the switch when focused
  • Communicates state to screen readers as "on" or "off"
  • Always provide a visible or aria-label label