Skip to content

Switch

Turn a setting on or off straight away. Same markup and props in every style: switch the style in the header to see it change.

Basic

A checkbox with role="switch": it works without JavaScript and submits with forms. Uses the same label layout as Checkbox.

switch-basic.html
<div style="display: grid; gap: 14px">
  <div class="vin-choice">
    <input id="wifi" class="vin-switch" type="checkbox" role="switch" checked>
    <div class="vin-choice__body"><label class="vin-choice__label" for="wifi">Wi-Fi</label></div>
  </div>
  <div class="vin-choice">
    <input id="bluetooth" class="vin-switch" type="checkbox" role="switch">
    <div class="vin-choice__body"><label class="vin-choice__label" for="bluetooth">Bluetooth</label></div>
  </div>
</div>

Settings list

Put the switch at the end of the row and add a description for each setting.

News, offers and product updates.

Sign-ins from new devices.

switch-settings.html
<div style="display: grid; gap: 18px; width: 100%; max-width: 380px">
  <div class="vin-choice vin-choice--end">
    <input id="s-marketing" class="vin-switch" type="checkbox" role="switch" aria-describedby="s-marketing-desc">
    <div class="vin-choice__body">
      <label class="vin-choice__label" for="s-marketing">Marketing emails</label>
      <p class="vin-choice__desc" id="s-marketing-desc">News, offers and product updates.</p>
    </div>
  </div>
  <div class="vin-choice vin-choice--end">
    <input id="s-security" class="vin-switch" type="checkbox" role="switch" checked aria-describedby="s-security-desc">
    <div class="vin-choice__body">
      <label class="vin-choice__label" for="s-security">Security alerts</label>
      <p class="vin-choice__desc" id="s-security-desc">Sign-ins from new devices.</p>
    </div>
  </div>
</div>

Small and disabled

A smaller switch for dense layouts. Disabled switches keep their state visible.

switch-sizes.html
<div style="display: grid; gap: 14px">
  <div class="vin-choice">
    <input id="compact" class="vin-switch vin-switch--sm" type="checkbox" role="switch" checked>
    <div class="vin-choice__body"><label class="vin-choice__label" for="compact">Compact view</label></div>
  </div>
  <div class="vin-choice">
    <input id="beta" class="vin-switch" type="checkbox" role="switch" checked disabled>
    <div class="vin-choice__body"><label class="vin-choice__label" for="beta">Beta features (managed)</label></div>
  </div>
  <div class="vin-choice">
    <input id="sync" class="vin-switch" type="checkbox" role="switch" disabled>
    <div class="vin-choice__body"><label class="vin-choice__label" for="sync">Offline sync</label></div>
  </div>
</div>

Installation

React, Vue and Svelte: add the component to your project with the CLI. It reads your framework from vin.json and also copies any component this one needs. Then import it:

Terminal
npx @vinish/ui add switch
React
import { Switch } from "@/components/vin/switch";
Vue
import Switch from "@/components/vin/switch.vue";
Svelte
import Switch from "$lib/components/vin/switch.svelte";

The import paths assume the default folders: @/components/vin (React and Vue, with the usual @ → src alias) and $lib/components/vin (SvelteKit). Use your own if you chose another folder in init. How the Vue and Svelte versions pass values and content is in Frameworks.

Plain HTML: the classes come with the style's stylesheet. It needs no JavaScript.

API

From spec/switch.json, which every framework version is tested against.

PropValuesDefaultIn HTML
label
Visible label, linked to the switch. Without it, pass an aria-label.
ReactNode—<label class="vin-choice__label" for="…">
description
Smaller text under the label, read out with the switch.
ReactNode—<p class="vin-choice__desc" id="…"> + aria-describedby
sizesmmdmdvin-switch--{value}
controlPosition
Put the switch after the text, full width (settings rows).
startendstartvin-choice--end
checked / defaultCheckedboolean—checked attribute
disabledbooleanfalsedisabled attribute

Parts

React, Vue, SvelteHTML classes
<Switch>.vin-switch.vin-switch--sm

Accessibility

  • A native checkbox with role="switch": screen readers say "on" or "off", and Space toggles it.
  • Every switch has a label: a <label for> or an aria-label.
  • Use a switch for settings that take effect at once; use a Checkbox inside forms that are submitted.
  • Visible focus ring in every style and mode; the on state is not shown by colour alone (the knob moves).