kipluo
React6 min read4 steps

How to Build an Animated Toggle Switch in React

A toggle switch is a button with two states and a moving knob. Getting the semantics right matters more than the animation: screen readers need role="switch" and aria-checked, and the label must be clickable. This guide builds one from scratch and shows the spring version.

You needReact 18+CSS transitionsBasic ARIA roles
Loading demo
Click, tap or press Space on the switch. You can also drag the knob.Component page

1. Switch or checkbox?

Use a switch for settings that take effect immediately, such as turning notifications on. Use a checkbox when the choice is submitted later with a form. Screen readers announce them differently ("on/off" versus "checked/not checked"), so choose the role that matches the behaviour.

2. Build the switch

A <button> with role="switch" gets keyboard focus and Space/Enter activation automatically. aria-checked carries the state. The knob is a child span that moves with transform, which the browser can animate without layout work.

Linking a visible <label> with htmlFor makes the text clickable and gives the button its accessible name.

Switch.tsx
import { useId } from "react";

type SwitchProps = {
  checked: boolean;
  onCheckedChange: (checked: boolean) => void;
  label: string;
};

export function Switch({ checked, onCheckedChange, label }: SwitchProps) {
  const id = useId();
  return (
    <div className="flex items-center gap-3">
      <button
        id={id}
        type="button"
        role="switch"
        aria-checked={checked}
        onClick={() => onCheckedChange(!checked)}
        className="switch"
      >
        <span className="switch-knob" aria-hidden />
      </button>
      <label htmlFor={id}>{label}</label>
    </div>
  );
}

3. Animate the knob

The attribute selector [aria-checked="true"] styles the on state, so the ARIA state and the visuals can never disagree. A slight overshoot curve (the last cubic-bezier value above 1) gives the knob a small bounce at the end.

switch.css
.switch {
  position: relative;
  width: 44px;
  height: 26px;
  border-radius: 999px;
  background: #d4d4d8;
  transition: background-color 150ms;
}

.switch[aria-checked="true"] {
  background: #16a34a;
}

.switch-knob {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 999px;
  background: white;
  transition: transform 220ms cubic-bezier(0.3, 1.4, 0.5, 1);
}

.switch[aria-checked="true"] .switch-knob {
  transform: translateX(18px);
}

.switch:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .switch-knob { transition: none; }
}

4. The spring version: Kipluo Spring Toggle

The CSS curve above always takes 220 ms and restarts if the user taps twice quickly. Kipluo's free Spring Toggle moves the knob on a spring, so rapid taps reverse smoothly, and the knob can also be dragged and flicked. It keeps the same semantics: role="switch", aria-checked, keyboard activation and a clickable label.

Settings.tsx
// npx kipluo add spring-toggle
import { useState } from "react";
import { SpringToggle } from "@/components/kipluo/spring-toggle";

export function Settings() {
  const [on, setOn] = useState(true);
  return <SpringToggle checked={on} onCheckedChange={setOn} label="Email notifications" />;
}

Accessibility checklist

Tick each one off as you check your build. Your ticks stay in this browser.

0 of 4 checked

Build it yourself or install it

Build it yourself when

A handful of settings switches where a CSS transition is enough. The component above is complete.

Install Kipluo when

You want drag-to-toggle, interruptible spring motion, sound and three visual styles, with a matching Flutter SpringToggle.

Keep building