Skip to content

Switch ​

A switch turns something on. The difference from a checkbox is when it takes effect: a switch applies immediately, a checkbox states an intention that a form submission later carries out. If there is a Save button, it should probably be a checkbox.

ts
import { Switch, SwitchContent, SwitchControl, SwitchThumb } from "ropav";
size
template
<Switch>
  <SwitchContent>
    <SwitchControl>
      <SwitchThumb />
    </SwitchControl>
    Notify me
  </SwitchContent>
</Switch>

An icon in the thumb ​

SwitchIcon sits inside SwitchThumb and rides along with it. Use it when the two states need to be told apart without reading the label — and remember that the icon moves rather than changing, so it says "this is a switch", not "this is on".

switch-icon.vue
vue
<script setup lang="ts">
import {
  IconClose,
  IconPlus,
  Switch,
  SwitchContent,
  SwitchControl,
  SwitchIcon,
  SwitchThumb,
} from "ropav";
</script>

<template>
  <Switch default-selected>
    <SwitchContent>
      <SwitchControl>
        <SwitchThumb>
          <SwitchIcon>
            <IconPlus />
          </SwitchIcon>
        </SwitchThumb>
      </SwitchControl>
      Sync on save
    </SwitchContent>
  </Switch>

  <Switch>
    <SwitchContent>
      <SwitchControl>
        <SwitchThumb>
          <SwitchIcon>
            <IconClose />
          </SwitchIcon>
        </SwitchThumb>
      </SwitchControl>
      Do not disturb
    </SwitchContent>
  </Switch>
</template>

Accessibility ​

  • The underlying control is a native checkbox with a switch role, so the state is the browser's to report rather than something the component announces.
  • SwitchContent puts the label inside the control, so the whole row toggles.
  • Under Forced Colors Mode the track and thumb are redrawn with system colours, because the fill that normally separates on from off is stripped there.

API ​

Switch

PropTypeDefaultDescription
ariaDescribedbystring—Ids of the elements that describe the switch, on top of any nested help text.
ariaLabelstring—Accessible name, for a switch with no visible label.
ariaLabelledbystring—Ids of the elements that name the switch.
defaultSelectedboolean—Whether the switch starts on, when it is uncontrolled.
formstring—id of the form to submit with, for a switch rendered outside it.
idstring—Lands on the hidden input rather than on the wrapper, because the input is the switch as far as assistive technology is concerned.
isDisabledboolean—Whether the switch is disabled — no interaction, no submission.
isInvalidboolean—Whether the value fails validation. Setting it either way takes the field over: false claims the switch is valid and shadows validate, the browser and the server alike.
isReadOnlyboolean—Whether the value can be read but not changed.
isRequiredboolean—Whether the switch has to be on for the form to submit.
isSelectedboolean—Whether the switch is on. Makes the switch controlled.
namestring—Name submitted with the form.
size"xs" | "sm" | "md" | "lg""md"Size of the control.
validateValidationFunction<boolean>—Checks the value and returns a message when it is not acceptable.
validationBehavior"aria" | "native""native"How the switch reports validation. Inherited from the surrounding form when unset.
valuestring"on"Value submitted with the form while the switch is on.

The default slot receives:

Slot propType
isDisabledboolean
isInvalidboolean
isReadOnlyboolean
isRequiredboolean
isSelectedboolean
EventPayload
changeboolean
update:isSelectedboolean

SwitchContent

The default slot receives:

Slot propType
isDisabledboolean
isFocusedboolean
isFocusVisibleboolean
isHoveredboolean
isInvalidboolean
isPressedboolean
isReadOnlyboolean
isRequiredboolean
isSelectedboolean

SwitchControl

Slots: default.

SwitchIcon

Slots: default.

SwitchThumb

Slots: default.