Switch

Binary on/off switch for settings and preferences. Provides immediate visual feedback and is ideal for options that take effect instantly.

Setup

import { Switch } from '@fragments-sdk/ui';

Examples

Default Off

Switch in the off state

Loading preview...

Checked

Switch in the on state

Loading preview...

With Helper Text

Switch with explanatory helper text

Loading preview...

Sizes

Available size variants

Loading preview...

Disabled States

Non-interactive switches showing both states

Loading preview...

Settings Panel

Multiple switches in a realistic settings layout

Loading preview...

Props

PropTypeDefaultDescription
checkedbooleanfalseWhether the switch is in the on state
defaultCheckedbooleanNot setDefault checked state (uncontrolled)
onCheckedChangefunctionNot setCalled when the switch is toggled: (checked: boolean) => void
onChangefunctionNot setAlias for onCheckedChange: (checked: boolean) => void
labelstringNot setVisible label text
helperTextstringNot setHelper text shown below the label (preferred)
descriptionstringNot setDeprecated alias for helperText
disabledbooleanfalseWhether the switch is non-interactive
sizeenumsmmdlgmdSwitch track size
classNamestringNot set
namestringNot set
idstringNot set
aria-labelstringNot set
aria-labelledbystringNot set
aria-describedbystringNot set

Usage Guidelines

When to use

  • Binary settings that take effect immediately (e.g., dark mode, notifications)
  • Enabling/disabling features in a settings panel
  • Options where the result is immediately visible
  • Mobile-friendly boolean inputs

When not to use

  • Multiple options in a group (use checkbox group)
  • Selection requires form submission to take effect (use checkbox)
  • Yes/No questions in forms (use radio buttons)
  • Complex multi-state options (use select or radio)

Best practices

  • Switch should always have a visible label explaining what it controls
  • The "on" state should be the positive/enabling action
  • Changes should take effect immediately - no save button needed
  • Include helperText for switches whose effect isn't obvious from the label
  • Group related switches visually in settings panels
Accessibility
  • Uses role="switch" with aria-checked for proper semantics
  • Must have an accessible label (visible or aria-label)
  • Focus indicator must be clearly visible
  • State change must be announced by screen readers