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.
<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>import { ToggleGroup, ToggleGroupItem } from "@/components/vin/toggle-group";
export function ToggleGroupSingle() {
return (
<ToggleGroup type="single" variant="outline" defaultValue="grid" aria-label="View">
<ToggleGroupItem value="list">List</ToggleGroupItem>
<ToggleGroupItem value="grid">Grid</ToggleGroupItem>
<ToggleGroupItem value="board">Board</ToggleGroupItem>
</ToggleGroup>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import ToggleGroup, { ToggleGroupItem } from "@/components/vin/toggle-group.vue";
</script>
<template>
<ToggleGroup type="single" variant="outline" default-value="grid" aria-label="View">
<ToggleGroupItem value="list">List</ToggleGroupItem>
<ToggleGroupItem value="grid">Grid</ToggleGroupItem>
<ToggleGroupItem value="board">Board</ToggleGroupItem>
</ToggleGroup>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import ToggleGroup from "$lib/components/vin/toggle-group.svelte";
import ToggleGroupItem from "$lib/components/vin/toggle-group-item.svelte";
</script>
<ToggleGroup type="single" variant="outline" value="grid" aria-label="View">
<ToggleGroupItem value="list">List</ToggleGroupItem>
<ToggleGroupItem value="grid">Grid</ToggleGroupItem>
<ToggleGroupItem value="board">Board</ToggleGroupItem>
</ToggleGroup>Pick any
type="multiple" for independent on/off options, like text formatting. Arrow keys move, Space toggles.
<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>import { ToggleGroup, ToggleGroupItem } from "@/components/vin/toggle-group";
const Icon = ({ d }: { d: string }) => (
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d={d} /></svg>
);
export function ToggleGroupMultiple() {
return (
<ToggleGroup type="multiple" size="icon" defaultValue={["bold"]} aria-label="Text formatting">
<ToggleGroupItem value="bold" aria-label="Bold"><Icon 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" /></ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Italic"><Icon d="M19 4h-9M14 20H5M15 4 9 20" /></ToggleGroupItem>
<ToggleGroupItem value="underline" aria-label="Underline"><Icon d="M6 4v6a6 6 0 0 0 12 0V4M4 20h16" /></ToggleGroupItem>
</ToggleGroup>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import ToggleGroup, { ToggleGroupItem } from "@/components/vin/toggle-group.vue";
</script>
<template>
<ToggleGroup type="multiple" size="icon" :default-value="['bold']" aria-label="Text formatting">
<ToggleGroupItem value="bold" aria-label="Bold"><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></ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Italic"><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></ToggleGroupItem>
<ToggleGroupItem value="underline" aria-label="Underline"><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></ToggleGroupItem>
</ToggleGroup>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import ToggleGroup from "$lib/components/vin/toggle-group.svelte";
import ToggleGroupItem from "$lib/components/vin/toggle-group-item.svelte";
</script>
<ToggleGroup type="multiple" size="icon" value={["bold"]} aria-label="Text formatting">
<ToggleGroupItem value="bold" aria-label="Bold"><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></ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Italic"><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></ToggleGroupItem>
<ToggleGroupItem value="underline" aria-label="Underline"><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></ToggleGroupItem>
</ToggleGroup>Icons
Icon-only items need an aria-label each.
<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>import { ToggleGroup, ToggleGroupItem } from "@/components/vin/toggle-group";
const Icon = ({ d }: { d: string }) => (
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d={d} /></svg>
);
export function ToggleGroupAlignment() {
return (
<ToggleGroup type="single" variant="outline" size="icon" defaultValue="left" aria-label="Text alignment">
<ToggleGroupItem value="left" aria-label="Align left"><Icon d="M15 12H3M17 18H3M21 6H3" /></ToggleGroupItem>
<ToggleGroupItem value="center" aria-label="Align center"><Icon d="M17 12H7M19 18H5M21 6H3" /></ToggleGroupItem>
<ToggleGroupItem value="right" aria-label="Align right"><Icon d="M21 12H9M21 18H7M21 6H3" /></ToggleGroupItem>
</ToggleGroup>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import ToggleGroup, { ToggleGroupItem } from "@/components/vin/toggle-group.vue";
</script>
<template>
<ToggleGroup type="single" variant="outline" size="icon" default-value="left" aria-label="Text alignment">
<ToggleGroupItem value="left" aria-label="Align left"><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></ToggleGroupItem>
<ToggleGroupItem value="center" aria-label="Align center"><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></ToggleGroupItem>
<ToggleGroupItem value="right" aria-label="Align right"><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></ToggleGroupItem>
</ToggleGroup>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import ToggleGroup from "$lib/components/vin/toggle-group.svelte";
import ToggleGroupItem from "$lib/components/vin/toggle-group-item.svelte";
</script>
<ToggleGroup type="single" variant="outline" size="icon" value="left" aria-label="Text alignment">
<ToggleGroupItem value="left" aria-label="Align left"><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></ToggleGroupItem>
<ToggleGroupItem value="center" aria-label="Align center"><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></ToggleGroupItem>
<ToggleGroupItem value="right" aria-label="Align right"><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></ToggleGroupItem>
</ToggleGroup>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 toggle-groupimport { ToggleGroup, ToggleGroupItem } from "@/components/vin/toggle-group";import ToggleGroup, { ToggleGroupItem } from "@/components/vin/toggle-group.vue";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.
| Prop | Values | Default | In HTML |
|---|---|---|---|
typesingle: exactly one on (a radio group). multiple: any number on. | singlemultiple | — | role="radiogroup" + role="radio" aria-checked, or role="group" + aria-pressed |
value / defaultValueWhat 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 |
variant | defaultoutline | default | vin-toggle-group--outline |
sizeApplied to every item. | smmdlgicon | md | vin-toggle--{value} on each item |
aria-labelNames the group for screen readers. | string | — | aria-label |
Parts
| React, Vue, Svelte | HTML 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.
Customize
Saved in this browser. Style and accent are in the header.