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.
<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>import { Toggle } from "@/components/vin/toggle";
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 ToggleIcons() {
return (
<div style={{ display: "flex", gap: 4 }}>
<Toggle size="icon" aria-label="Bold" defaultPressed><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" /></Toggle>
<Toggle size="icon" aria-label="Italic"><Icon d="M19 4h-9M14 20H5M15 4 9 20" /></Toggle>
<Toggle size="icon" aria-label="Underline"><Icon d="M6 4v6a6 6 0 0 0 12 0V4M4 20h16" /></Toggle>
</div>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Toggle from "@/components/vin/toggle.vue";
</script>
<template>
<div style="display: flex; gap: 4px">
<Toggle size="icon" aria-label="Bold" default-pressed><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></Toggle>
<Toggle size="icon" 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></Toggle>
<Toggle size="icon" 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></Toggle>
</div>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Toggle from "$lib/components/vin/toggle.svelte";
</script>
<div style="display: flex; gap: 4px">
<Toggle size="icon" aria-label="Bold" pressed><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></Toggle>
<Toggle size="icon" 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></Toggle>
<Toggle size="icon" 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></Toggle>
</div>Outline, with text
A border and a text label, for toggles that stand alone.
<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>import { Toggle } from "@/components/vin/toggle";
export function ToggleOutline() {
return (
<>
<Toggle variant="outline" defaultPressed>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="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
</Toggle>
<Toggle variant="outline">Show archived</Toggle>
<Toggle variant="outline" disabled>Locked</Toggle>
</>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Toggle from "@/components/vin/toggle.vue";
</script>
<template>
<Toggle variant="outline" default-pressed>
<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
</Toggle>
<Toggle variant="outline">Show archived</Toggle>
<Toggle variant="outline" disabled>Locked</Toggle>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Toggle from "$lib/components/vin/toggle.svelte";
</script>
<Toggle variant="outline" pressed>
<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
</Toggle>
<Toggle variant="outline">Show archived</Toggle>
<Toggle variant="outline" disabled>Locked</Toggle>Sizes
Small, medium and large, matching the Button heights.
<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>import { Toggle } from "@/components/vin/toggle";
const Bold = () => (
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="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>
);
/* size="icon" is the medium square; add the sm/lg class for the other square sizes. */
export function ToggleSizes() {
return (
<>
<Toggle variant="outline" size="icon" className="vin-toggle--sm" aria-label="Bold, small" defaultPressed><Bold /></Toggle>
<Toggle variant="outline" size="icon" aria-label="Bold, medium" defaultPressed><Bold /></Toggle>
<Toggle variant="outline" size="icon" className="vin-toggle--lg" aria-label="Bold, large" defaultPressed><Bold /></Toggle>
</>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Toggle from "@/components/vin/toggle.vue";
</script>
<template>
<!-- size="icon" is the medium square; add the sm/lg class for the other square sizes. -->
<Toggle variant="outline" size="icon" class="vin-toggle--sm" aria-label="Bold, small" default-pressed><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></Toggle>
<Toggle variant="outline" size="icon" aria-label="Bold, medium" default-pressed><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></Toggle>
<Toggle variant="outline" size="icon" class="vin-toggle--lg" aria-label="Bold, large" default-pressed><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></Toggle>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Toggle from "$lib/components/vin/toggle.svelte";
</script>
<!-- size="icon" is the medium square; add the sm/lg class for the other square sizes. -->
<Toggle variant="outline" size="icon" class="vin-toggle--sm" aria-label="Bold, small" pressed><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></Toggle>
<Toggle variant="outline" size="icon" aria-label="Bold, medium" pressed><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></Toggle>
<Toggle variant="outline" size="icon" class="vin-toggle--lg" aria-label="Bold, large" pressed><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></Toggle>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 toggleimport { Toggle, toggleClasses } from "@/components/vin/toggle";import Toggle, { toggleClasses } from "@/components/vin/toggle.vue";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.
| Prop | Values | Default | In HTML |
|---|---|---|---|
pressed / defaultPressedOn or off (controlled or not). Use onPressedChange to hear about changes. | boolean | — | aria-pressed="true|false" |
variant | defaultoutline | default | vin-toggle--{value} |
size | smmdlgicon | md | vin-toggle--{value} |
disabled | boolean | false | disabled attribute |
Parts
| React, Vue, Svelte | HTML 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.
Customize
Saved in this browser. Style and accent are in the header.