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.
<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>import { Switch } from "@/components/vin/switch";
export function SwitchBasic() {
return (
<div style={{ display: "grid", gap: 14 }}>
<Switch label="Wi-Fi" defaultChecked />
<Switch label="Bluetooth" />
</div>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Switch from "@/components/vin/switch.vue";
</script>
<template>
<div style="display: grid; gap: 14px">
<Switch label="Wi-Fi" default-checked />
<Switch label="Bluetooth" />
</div>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Switch from "$lib/components/vin/switch.svelte";
</script>
<div style="display: grid; gap: 14px">
<Switch label="Wi-Fi" checked />
<Switch label="Bluetooth" />
</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.
<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>News, offers and product updates.
Sign-ins from new devices.
import { Switch } from "@/components/vin/switch";
export function SwitchSettings() {
return (
<div style={{ display: "grid", gap: 18, width: "100%", maxWidth: 380 }}>
<Switch label="Marketing emails" description="News, offers and product updates." controlPosition="end" />
<Switch label="Security alerts" description="Sign-ins from new devices." controlPosition="end" defaultChecked />
</div>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Switch from "@/components/vin/switch.vue";
</script>
<template>
<div style="display: grid; gap: 18px; width: 100%; max-width: 380px">
<Switch label="Marketing emails" description="News, offers and product updates." control-position="end" />
<Switch label="Security alerts" description="Sign-ins from new devices." control-position="end" default-checked />
</div>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Switch from "$lib/components/vin/switch.svelte";
</script>
<div style="display: grid; gap: 18px; width: 100%; max-width: 380px">
<Switch label="Marketing emails" description="News, offers and product updates." controlPosition="end" />
<Switch label="Security alerts" description="Sign-ins from new devices." controlPosition="end" checked />
</div>Small and disabled
A smaller switch for dense layouts. Disabled switches keep their state visible.
<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>import { Switch } from "@/components/vin/switch";
export function SwitchSizes() {
return (
<div style={{ display: "grid", gap: 14 }}>
<Switch label="Compact view" size="sm" defaultChecked />
<Switch label="Beta features (managed)" defaultChecked disabled />
<Switch label="Offline sync" disabled />
</div>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Switch from "@/components/vin/switch.vue";
</script>
<template>
<div style="display: grid; gap: 14px">
<Switch label="Compact view" size="sm" default-checked />
<Switch label="Beta features (managed)" default-checked disabled />
<Switch label="Offline sync" disabled />
</div>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Switch from "$lib/components/vin/switch.svelte";
</script>
<div style="display: grid; gap: 14px">
<Switch label="Compact view" size="sm" checked />
<Switch label="Beta features (managed)" checked disabled />
<Switch label="Offline sync" disabled />
</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:
npx @vinish/ui add switchimport { Switch } from "@/components/vin/switch";import Switch from "@/components/vin/switch.vue";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.
| Prop | Values | Default | In HTML |
|---|---|---|---|
labelVisible label, linked to the switch. Without it, pass an aria-label. | ReactNode | — | <label class="vin-choice__label" for="…"> |
descriptionSmaller text under the label, read out with the switch. | ReactNode | — | <p class="vin-choice__desc" id="…"> + aria-describedby |
size | smmd | md | vin-switch--{value} |
controlPositionPut the switch after the text, full width (settings rows). | startend | start | vin-choice--end |
checked / defaultChecked | boolean | — | checked attribute |
disabled | boolean | false | disabled attribute |
Parts
| React, Vue, Svelte | HTML 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).
Customize
Saved in this browser. Style and accent are in the header.