Form

Switch

An on/off toggle — three sizes, three shapes, optional glyph, accessible, in React (@bpdm/ui) and Angular (@bpdm/ng).

The Switch is an on/off toggle built on Radix. Use it for instant settings (no Save button). It's controlled (checked + onCheckedChange) or uncontrolled (defaultChecked); in Angular it's <bpdm-switch> with [(checked)], [(ngModel)], or reactive forms.

Usage

switch-usage.tsx
import { Switch } from '@bpdm/ui/switch';

export function SwitchUsage() {
  return <Switch defaultChecked aria-label="Toggle setting" />;
}

Sizes

The size prop offers sm, md (default), and lg.

sm
md
lg
switch-sizes.tsx
import { Switch } from '@bpdm/ui/switch';

export function Sizes() {
  return (
    <div className="flex items-center gap-4">
      <Switch size="sm" defaultChecked aria-label="Small" />
      <Switch size="md" defaultChecked aria-label="Medium" />
      <Switch size="lg" defaultChecked aria-label="Large" />
    </div>
  );
}

Shapes

The shape prop offers pill (default), square (rounded), and sharp.

pill
square
sharp
switch-shapes.tsx
import { Switch } from '@bpdm/ui/switch';

export function Shapes() {
  return (
    <div className="flex items-center gap-4">
      <Switch shape="pill" defaultChecked aria-label="Pill" />
      <Switch shape="square" defaultChecked aria-label="Square" />
      <Switch shape="sharp" defaultChecked aria-label="Sharp" />
    </div>
  );
}

With icon

Set icon to render a ✓ / ✗ glyph inside the thumb — handy when the on/off state needs to read at a glance.

on
off
large
switch-icon.tsx
import { Switch } from '@bpdm/ui/switch';

export function WithIcon() {
  return (
    <div className="flex items-center gap-4">
      <Switch icon defaultChecked aria-label="On" />
      <Switch icon aria-label="Off" />
      <Switch icon size="lg" defaultChecked aria-label="Large on" />
    </div>
  );
}

States

Off, on, and disabled (on or off).

off
on
disabled
disabled on
switch-states.tsx
import { Switch } from '@bpdm/ui/switch';

export function States() {
  return (
    <div className="flex items-center gap-4">
      <Switch aria-label="Off" />
      <Switch defaultChecked aria-label="On" />
      <Switch disabled aria-label="Disabled" />
      <Switch disabled defaultChecked aria-label="Disabled and on" />
    </div>
  );
}

With a label

Link a <label> to the switch by id (htmlFor in React, for in Angular) so clicking the text toggles it and screen readers announce the control's name.

switch-label.tsx
import { Switch } from '@bpdm/ui/switch';

export function AirplaneToggle() {
  return (
    <div className="flex items-center gap-3">
      <Switch id="airplane" defaultChecked />
      <label htmlFor="airplane" className="text-sm font-medium">
        Airplane mode
      </label>
    </div>
  );
}

Settings list

A common real-world layout — one labelled row per toggle.

switch-settings.tsx
import { Switch } from '@bpdm/ui/switch';

const settings = [
  { id: 's-2fa', label: 'Two-factor authentication', on: true },
  { id: 's-email', label: 'Email notifications', on: false },
  { id: 's-beta', label: 'Beta features', on: true },
];

export function SettingsList() {
  return (
    <div className="flex flex-col divide-y rounded-lg border">
      {settings.map((s) => (
        <div key={s.id} className="flex items-center justify-between gap-4 px-4 py-3">
          <label htmlFor={s.id} className="text-sm">
            {s.label}
          </label>
          <Switch id={s.id} defaultChecked={s.on} />
        </div>
      ))}
    </div>
  );
}

Controlled value

Drive the state yourself with checked + onCheckedChange (React) or two-way [(checked)] / [(ngModel)] (Angular).

switch-controlled.tsx
import { useState } from 'react';
import { Switch } from '@bpdm/ui/switch';

export function WifiToggle() {
  const [on, setOn] = useState(true);
  return (
    <div className="flex items-center gap-3">
      <Switch id="wifi" checked={on} onCheckedChange={setOn} />
      <label htmlFor="wifi" className="text-sm font-medium">
        Wi-Fi is {on ? 'on' : 'off'}
      </label>
    </div>
  );
}

API

PropTypeDefaultDescription
checked / defaultCheckedboolean—Controlled / uncontrolled on state.
onCheckedChange(checked: boolean) => void—React — fires on toggle. Angular uses [(checked)] / [(ngModel)].
sizesm | md | lgmdTrack and thumb size.
shapepill | square | sharppillCorner style of the track and thumb.
iconbooleanfalseShow a ✓ / ✗ glyph inside the thumb.
disabledbooleanfalseDisable the control.
idstring—Control id (label association / testing), forwarded to the switch.
aria-label / aria-labelledby / aria-describedbystring—Accessible name / description, forwarded to the switch.

In React, all native Radix Switch props (name, value, required, id, aria-*, data-*, …) are spread onto the control and the ref is forwarded. In Angular, size / shape / icon / id / aria-label / aria-labelledby / aria-describedby are @Inputs on <bpdm-switch>, which is a ControlValueAccessor ([(ngModel)] / reactive forms) with a standalone [(checked)].

Accessibility

  • A true role="switch" with aria-checked reflecting state and full keyboard support (Space / Enter toggle; Tab focuses with a visible ring).
  • Give it an accessible name — link a <label> by id (htmlFor in React, for in Angular), or pass aria-label / aria-labelledby (forwarded to the control in both frameworks).
  • The thumb glyph (icon) is aria-hidden and decorative — state is conveyed via aria-checked, never the glyph alone.

Internationalization

  • The switch renders no text of its own — pass a translated accessible name (aria-label / associated <label>) and translated help via aria-describedby. There's nothing hard-coded to override.
  • RTL-safe: in a dir="rtl" context the thumb slides to the left when on (the checked-state translate is mirrored), so the motion reads correctly right-to-left. Works from the ambient dir in both frameworks. See the Internationalization guide.

On this page