Skip to content
VinUIComponentsToggle Group

Toggle Group

A set of toggles: pick one, or turn on any number. Same markup and props in every style: switch the style in the header to see it change.

Pick one

type="single" works like a radio group: arrow keys move and choose. In HTML, add data-vin-toggle-group and include vinui.js.

toggle-group-single.html
<div class="vin-toggle-group vin-toggle-group--outline" role="radiogroup" aria-label="View" data-vin-toggle-group>
  <button type="button" class="vin-toggle" role="radio" aria-checked="false" tabindex="-1">List</button>
  <button type="button" class="vin-toggle" role="radio" aria-checked="true" tabindex="0">Grid</button>
  <button type="button" class="vin-toggle" role="radio" aria-checked="false" tabindex="-1">Board</button>
</div>

Pick any

type="multiple" for independent on/off options, like text formatting. Arrow keys move, Space toggles.

toggle-group-multiple.html
<div class="vin-toggle-group" role="group" aria-label="Text formatting" data-vin-toggle-group>
  <button type="button" class="vin-toggle vin-toggle--icon" aria-pressed="true" aria-label="Bold" tabindex="0">
    <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" tabindex="-1">
    <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" tabindex="-1">
    <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>

Icons

Icon-only items need an aria-label each.

toggle-group-alignment.html
<div class="vin-toggle-group vin-toggle-group--outline" role="radiogroup" aria-label="Text alignment" data-vin-toggle-group>
  <button type="button" class="vin-toggle vin-toggle--icon" role="radio" aria-checked="true" aria-label="Align left" tabindex="0">
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M15 12H3M17 18H3M21 6H3"/></svg>
  </button>
  <button type="button" class="vin-toggle vin-toggle--icon" role="radio" aria-checked="false" aria-label="Align center" tabindex="-1">
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M17 12H7M19 18H5M21 6H3"/></svg>
  </button>
  <button type="button" class="vin-toggle vin-toggle--icon" role="radio" aria-checked="false" aria-label="Align right" tabindex="-1">
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12H9M21 18H7M21 6H3"/></svg>
  </button>
</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 toggle-group
React
import { ToggleGroup, ToggleGroupItem } from "@/components/vin/toggle-group";
Vue
import ToggleGroup, { ToggleGroupItem } from "@/components/vin/toggle-group.vue";
Svelte
import ToggleGroup from "$lib/components/vin/toggle-group.svelte";
import ToggleGroupItem from "$lib/components/vin/toggle-group-item.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-group.json, which every framework version is tested against.

PropValuesDefaultIn HTML
type
single: exactly one on (a radio group). multiple: any number on.
singlemultiple—role="radiogroup" + role="radio" aria-checked, or role="group" + aria-pressed
value / defaultValue
What is on (a string for single, an array for multiple). Use onValueChange to hear about changes.
string | string[]—aria-checked / aria-pressed on the items
variantdefaultoutlinedefaultvin-toggle-group--outline
size
Applied to every item.
smmdlgiconmdvin-toggle--{value} on each item
aria-label
Names the group for screen readers.
string—aria-label

Parts

React, Vue, SvelteHTML classes
<ToggleGroup><ToggleGroupItem>.vin-toggle-group.vin-toggle-group--outline

Accessibility

  • Single choice uses role="radiogroup" with role="radio" and aria-checked; multiple choice uses aria-pressed buttons.
  • One Tab stop for the whole group; ← → (and ↑ ↓) move between items, Home/End jump. Right-to-left flips the arrows.
  • The group has an aria-label; icon-only items each have an aria-label.
  • The on state is a filled background, not only a text colour.