# Toggle button

SvToggleButton: a pressed on/off button (aria-pressed) - a text-formatting toolbar and pin / live state toggles.

Uses `@svgrid/grid` (MIT). Category: Buttons & Toggles. Live demo: https://svgrid.com/demos/307-toggle-button/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvToggleButton, a pressed on and off button for Svelte 5 with aria-pressed semantics, shown as a text-formatting toolbar with bold, italic and underline and as pin and live-state toggles. The pressed prop and onChange keep it controlled, and the pressed look comes from the theme tokens.

SvToggleButton - a pressed on/off button. A production text-formatting toolbar and a set of view/pin toggles. Copy-paste ready.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### How is a toggle button different from a switch?

A toggle button is an action that stays pressed, such as Bold, and uses aria-pressed; a switch represents a setting being on or off and uses the switch role. Screen readers announce them differently.

### How do I group several toggles?

Place them in a toolbar or use SvButtonGroup in multiple mode, which adds arrow-key navigation between the toggles.

### Is the state controlled?

Yes. pressed is the source of truth and onChange reports the requested state, so your code can veto or transform a toggle.

## Related documentation

- [SvToggleButton](https://svgrid.com/docs/help/ui-components/sv-toggle-button.md)

## Related articles

- [SvGrid UI Components Tips and Tricks: Svelte 5 Buttons, Inputs, Tree, Forms](https://svgrid.com/blog/svgrid-ui-components-tips-and-tricks/) - Practical tips for the SvGrid UI component kit - SvButton states, a Cmd+K command palette, a virtualized searchable tree, schema-driven forms, a parsing date/time field, and the shared field contract - each with a code snippet.
- [An Actions Column (Edit, Delete) in SvGrid](https://svgrid.com/blog/actions-column-edit-delete/) - Build a per-row actions column with edit, delete, and duplicate buttons in SvGrid - handling data mutations, accessibility, and confirmation flows correctly.
- [A Density (Row-Height) Toggle for SvGrid](https://svgrid.com/blog/density-row-height-toggle/) - Let users pick compact, comfortable, or spacious rows - wired through CSS tokens, synced with the virtualizer, and persisted across sessions.

## Source code (307-toggle-button.svelte)

```svelte
<script lang="ts">
  /**
   * SvToggleButton - a pressed on/off button. A production text-formatting toolbar
   * and a set of view/pin toggles. Copy-paste ready.
   */
  import { SvToggleButton } from '@svgrid/grid'

  let bold = $state(true)
  let italic = $state(false)
  let underline = $state(false)
  let pinned = $state(false)
  let live = $state(true)
</script>

<div class="wrap">
  <header>
    <h2>Toggle button</h2>
    <p>A button that stays pressed (<code>aria-pressed</code>) - formatting toolbars, pin / favorite / mute toggles.</p>
  </header>

  <section class="block">
    <h3>Formatting toolbar</h3>
    <div class="toolbar">
      <SvToggleButton pressed={bold} ariaLabel="Bold" onChange={(v) => (bold = v)}><strong>B</strong></SvToggleButton>
      <SvToggleButton pressed={italic} ariaLabel="Italic" onChange={(v) => (italic = v)}><em>I</em></SvToggleButton>
      <SvToggleButton pressed={underline} ariaLabel="Underline" onChange={(v) => (underline = v)}><u>U</u></SvToggleButton>
    </div>
    <p class="preview" class:b={bold} class:i={italic} class:u={underline}>The quick brown fox</p>
  </section>

  <section class="block">
    <h3>State toggles</h3>
    <div class="toolbar">
      <SvToggleButton pressed={pinned} onChange={(v) => (pinned = v)}>{pinned ? 'Pinned' : 'Pin'}</SvToggleButton>
      <SvToggleButton pressed={live} onChange={(v) => (live = v)}>{live ? 'Live' : 'Paused'}</SvToggleButton>
    </div>
  </section>
</div>

<style>
  .wrap { padding: 20px; max-width: 560px; display: flex; flex-direction: column; gap: 22px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
  code { background: var(--sg-row-hover-bg, #eef2ff); padding: 1px 5px; border-radius: 5px; font-size: 12px; }
  .block h3 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--sg-muted, #64748b); }
  .toolbar { display: inline-flex; gap: 6px; }
  .preview { margin: 14px 0 0; font-size: 18px; }
  .preview.b { font-weight: 800; }
  .preview.i { font-style: italic; }
  .preview.u { text-decoration: underline; }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/307-toggle-button.svelte)
