Tuimorphic

Toggle

Terminal-style switch component with ON/OFF display.

Import

import { Toggle } from 'tuimorphic';

Basic Usage

<Toggle label="Enable feature" />

Props

PropTypeDefaultDescription
checkedbooleanWhether the toggle is on (controlled)
defaultCheckedbooleanfalseDefault checked state (uncontrolled)
onCheckedChange(checked: boolean) => voidCallback when state changes
labelstringLabel text displayed next to toggle
disabledbooleanfalseWhether the toggle is disabled
namestringName attribute for forms
classNamestringAdditional CSS class names
refReact.Ref<HTMLButtonElement>Forwarded ref to the button element

With Label

<Toggle label="Enable notifications" />

Disabled States

<Toggle label="Disabled off" disabled />
<Toggle label="Disabled on" disabled checked />

Controlled Toggle

Status: Disabled
const [enabled, setEnabled] = useState(false);
 
<Toggle
  label="Dark mode"
  checked={enabled}
  onCheckedChange={setEnabled}
/>

Settings Panel Example

const [notifications, setNotifications] = useState(true);
const [sound, setSound] = useState(true);
const [autoSave, setAutoSave] = useState(false);
const [analytics, setAnalytics] = useState(false);
 
<div>
  <Toggle
    label="Push notifications"
    checked={notifications}
    onCheckedChange={setNotifications}
  />
  <Toggle
    label="Sound effects"
    checked={sound}
    onCheckedChange={setSound}
  />
  <Toggle
    label="Auto-save drafts"
    checked={autoSave}
    onCheckedChange={setAutoSave}
  />
  <Toggle
    label="Usage analytics"
    checked={analytics}
    onCheckedChange={setAnalytics}
  />
</div>

Interactive Example

This example shows how toggles can interact with each other:

const [airplaneMode, setAirplaneMode] = useState(false);
const [wifi, setWifi] = useState(true);
const [bluetooth, setBluetooth] = useState(false);
 
const handleAirplaneModeChange = (checked: boolean) => {
  setAirplaneMode(checked);
  if (checked) {
    setWifi(false);
    setBluetooth(false);
  }
};
 
<Toggle
  label="Airplane mode"
  checked={airplaneMode}
  onCheckedChange={handleAirplaneModeChange}
/>
<Toggle
  label="Wi-Fi"
  checked={wifi}
  onCheckedChange={setWifi}
  disabled={airplaneMode}
/>
<Toggle
  label="Bluetooth"
  checked={bluetooth}
  onCheckedChange={setBluetooth}
  disabled={airplaneMode}
/>

Keyboard Navigation

KeyAction
SpaceToggle state
EnterToggle state
TabMove focus to next element

Accessibility

  • Built on Base UI's accessible switch primitive
  • Proper ARIA attributes for screen readers
  • Keyboard accessible
  • Label properly associated with toggle

Styling

VariableDescription
--theme-borderToggle border color
--theme-buttonToggle track background when on
--theme-textLabel color
--theme-focused-borderBorder color when focused
  • Checkbox - For boolean selections with checkmark
  • Radio - For single selection from multiple options

On this page