# Switch

SvSwitchButton in a real settings panel: labelled rows with descriptions, on/off track labels and a disabled row.

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

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

## About this example

SvSwitchButton in a Svelte 5 settings panel: a list of labelled rows with descriptions, on and off labels on the track and a disabled row. Each switch is controlled through checked and onChange and writes into a settings object, and the panel is styled from the theme tokens.

SvSwitchButton - a production settings panel: a list of labelled switches with descriptions, on/off track labels and a disabled row. Copy-paste ready.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### How do I label a switch?

Wrap it in a labelled row as the demo does, or pass ariaLabel when the visual label is elsewhere. The switch exposes the switch role with aria-checked.

### Can the track show on and off text?

Yes. The track labels are a prop, so a switch can read Yes and No or On and Off inside the control.

### How is a disabled switch handled?

disabled greys the control, removes it from the tab order and ignores clicks, while still showing its current state.

## Related documentation

- [SvSwitchButton](https://svgrid.com/docs/help/ui-components/sv-switch-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.

## Source code (308-switch.svelte)

```svelte
<script lang="ts">
  /**
   * SvSwitchButton - a production settings panel: a list of labelled switches
   * with descriptions, on/off track labels and a disabled row. Copy-paste ready.
   */
  import { SvSwitchButton } from '@svgrid/grid'

  let settings = $state({ notifications: true, marketing: false, twoFactor: true, beta: false })
</script>

<div class="wrap">
  <header>
    <h2>Switch</h2>
    <p>An instant on/off control (ARIA <code>switch</code>) - the standard for settings and preferences.</p>
  </header>

  <div class="panel">
    <div class="row">
      <div class="txt"><strong>Push notifications</strong><span>Alerts on your devices</span></div>
      <SvSwitchButton checked={settings.notifications} onChange={(v) => (settings.notifications = v)} />
    </div>
    <div class="row">
      <div class="txt"><strong>Marketing emails</strong><span>Product news and offers</span></div>
      <SvSwitchButton checked={settings.marketing} onChange={(v) => (settings.marketing = v)} />
    </div>
    <div class="row">
      <div class="txt"><strong>Two-factor auth</strong><span>Extra login security</span></div>
      <SvSwitchButton checked={settings.twoFactor} onLabel="ON" offLabel="OFF" onChange={(v) => (settings.twoFactor = v)} />
    </div>
    <div class="row is-disabled">
      <div class="txt"><strong>Beta features</strong><span>Requires an admin invite</span></div>
      <SvSwitchButton checked={settings.beta} disabled onChange={(v) => (settings.beta = v)} />
    </div>
  </div>
</div>

<style>
  .wrap { padding: 20px; max-width: 480px; display: flex; flex-direction: column; gap: 16px; }
  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; }
  .panel { border: 1px solid var(--sg-border, #e2e8f0); border-radius: 12px; overflow: hidden; }
  .row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 16px; }
  .row + .row { border-top: 1px solid var(--sg-border, #e2e8f0); }
  .row.is-disabled { opacity: 0.6; }
  .txt { display: flex; flex-direction: column; gap: 2px; }
  .txt strong { font-size: 13.5px; }
  .txt span { font-size: 12px; color: var(--sg-muted, #64748b); }
</style>
```

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