Skip to content

Toggle

A button that stays pressed until you press it again. Same markup and props in every style: switch the style in the header to see it change.

Icon toggles

Square toggles for a toolbar. Icon-only toggles need an aria-label. In HTML, add data-vin-toggle and include vinui.js.

toggle-icons.html
<div style="display: flex; gap: 4px">
  <button type="button" class="vin-toggle vin-toggle--icon" aria-pressed="true" aria-label="Bold" data-vin-toggle>
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8"/></svg>
  </button>
  <button type="button" class="vin-toggle vin-toggle--icon" aria-pressed="false" aria-label="Italic" data-vin-toggle>
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M19 4h-9M14 20H5M15 4 9 20"/></svg>
  </button>
  <button type="button" class="vin-toggle vin-toggle--icon" aria-pressed="false" aria-label="Underline" data-vin-toggle>
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 4v6a6 6 0 0 0 12 0V4M4 20h16"/></svg>
  </button>
</div>

Outline, with text

A border and a text label, for toggles that stand alone.

toggle-outline.html
<button type="button" class="vin-toggle vin-toggle--outline" aria-pressed="true" data-vin-toggle>
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m12 3 2.8 5.7 6.2.9-4.5 4.4 1.1 6.2L12 17.3l-5.6 2.9 1.1-6.2L3 9.6l6.2-.9z"/></svg>
  Starred
</button>
<button type="button" class="vin-toggle vin-toggle--outline" aria-pressed="false" data-vin-toggle>Show archived</button>
<button type="button" class="vin-toggle vin-toggle--outline" aria-pressed="false" disabled>Locked</button>

Sizes

Small, medium and large, matching the Button heights.

toggle-sizes.html
<button type="button" class="vin-toggle vin-toggle--outline vin-toggle--icon vin-toggle--sm" aria-pressed="true" aria-label="Bold, small" data-vin-toggle>
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8"/></svg>
</button>
<button type="button" class="vin-toggle vin-toggle--outline vin-toggle--icon" aria-pressed="true" aria-label="Bold, medium" data-vin-toggle>
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8"/></svg>
</button>
<button type="button" class="vin-toggle vin-toggle--outline vin-toggle--icon vin-toggle--lg" aria-pressed="true" aria-label="Bold, large" data-vin-toggle>
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8"/></svg>
</button>

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 toggle
React
import { Toggle, toggleClasses } from "@/components/vin/toggle";
Vue
import Toggle, { toggleClasses } from "@/components/vin/toggle.vue";
Svelte
import Toggle, { toggleClasses } from "$lib/components/vin/toggle.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. This component also needs vinui.js for its keyboard and pointer behaviour.

API

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

PropValuesDefaultIn HTML
pressed / defaultPressed
On or off (controlled or not). Use onPressedChange to hear about changes.
boolean—aria-pressed="true|false"
variantdefaultoutlinedefaultvin-toggle--{value}
sizesmmdlgiconmdvin-toggle--{value}
disabledbooleanfalsedisabled attribute

Parts

React, Vue, SvelteHTML classes
<Toggle>.vin-toggle.vin-toggle--outline.vin-toggle--sm.vin-toggle--lg.vin-toggle--icon

Accessibility

  • A <button> with aria-pressed: screen readers say "pressed" or "not pressed".
  • Keep the label the same when pressed ("Bold", not "Unbold"); aria-pressed tells the state.
  • Icon-only toggles have an aria-label.
  • The pressed state is shown by a filled background, not only by colour of the text.