Skip to Content
⭐️ Leave a star →
ComponentsSwitch

Switch

Toggle component with a thumb element. Accessible — renders as a <button role="switch"> with aria-checked.

Features

Renders as button with role=“switch” and aria-checked.
Can be controlled or uncontrolled.
Keyboard toggle with Space key.
Supports disabled state.

Example

Push notifications

Email updates

Marketing emails

import { Switch } from '@wire-ui/react' {[ { label: 'Push notifications', value: pushEnabled, onChange: setPushEnabled }, { label: 'Email updates', value: emailEnabled, onChange: setEmailEnabled }, { label: 'Marketing', value: marketingEnabled, onChange: setMarketingEnabled }, ].map(({ label, value, onChange }) => ( <div key={label}> <p>{label}</p> <Switch.Root checked={value} onChange={onChange}> <Switch.Thumb /> </Switch.Root> </div> ))}

Styling

Switch uses data-state (value "checked" or "unchecked") on both Root and Thumb. Use data-checked as a shorthand for the checked state.

<Switch.Root className=" bg-gray-200 data-[checked]:bg-black data-[disabled]:opacity-50 data-[focus-visible]:ring-2 "> <Switch.Thumb className=" bg-white translate-x-0 data-[checked]:translate-x-5 " /> </Switch.Root>

Using data attributes

Switch sets data-state ("checked" / "unchecked") and data-disabled on Root and Thumb.

/* Track background */ [data-state="checked"] { background: #000; } [data-state="unchecked"] { background: #e5e5e5; } /* Thumb position */ .thumb[data-state="checked"] { transform: translateX(20px); } /* Disabled */ [data-disabled] { opacity: 0.5; cursor: not-allowed; }

Root props

PropTypeDefaultDescription
checkedbooleanControlled checked state
defaultCheckedbooleanfalseInitial state for uncontrolled use
onCheckedChange(checked: boolean) => voidCalled when toggled
disabledbooleanfalseDisables the switch

Data attributes

AttributeElementValues
data-stateRoot, Thumb"checked" / "unchecked"
data-disabledRootPresent when disabled

Accessibility

  • role="switch" with aria-checked reflects the current state
  • Keyboard: Space toggles the switch

Keyboard Interactions

KeyDescription
SpaceToggles the switch on/off.
TabMoves focus to the switch.
Last updated on

MIT License © 2026 wire-ui

Switch – Wire UI