Switch

Alpha

An accessible toggle switch with label, checked state management, and CSS variable theming.

Import

import { Switch } from "@compa11y/react";

Usage

Example.tsx
import { useState } from "react";
import { Switch } from "@compa11y/react";

function Example() {
  const [enabled, setEnabled] = useState(false);

  return (
    <Switch
      checked={enabled}
      onCheckedChange={setEnabled}
    >
      Enable notifications
    </Switch>
  );
}

Features

  • Toggle on/off state
  • Accessible label
  • Keyboard activation with Space and Enter
  • CSS variable customization for colors and sizing
  • aria-checked attribute

Props

Switch component props
PropTypeDefaultDescription
checkedbooleanfalseControlled checked state
onCheckedChange(checked: boolean) => void-Called when checked state changes
disabledbooleanfalseDisables the switch
childrenReactNode-Label text

Keyboard Interactions

Accessibility
All keyboard interactions follow WAI-ARIA best practices and work without any additional configuration.
Keyboard shortcuts for Switch
KeyAction
SpaceToggles the switch
EnterToggles the switch

CSS Customization

Override these CSS custom properties to customize the appearance.

CSS custom properties for Switch
VariableDescriptionDefault
--compa11y-switch-bgBackground when unchecked#d1d5db
--compa11y-switch-checked-bgBackground when checked#10b981
--compa11y-switch-thumb-bgThumb colorwhite
--compa11y-switch-widthSwitch width3rem
--compa11y-switch-heightSwitch height1.75rem