# Toggle switch

> An on/off switch whose knob springs across, with a color and size you can set to match your design.

- Category: [Inputs](https://library.aimran.es/components/inputs)
- Built with: React + Motion
- Live editor: https://library.aimran.es/blocks/toggle-switch
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

Use a toggle for settings that take effect straight away: dark mode, email notifications, “Show prices with VAT” in a B2B shop or availability in a booking dashboard. The springy knob lands with a satisfying little snap. If you searched for an animated toggle switch in React, this is a clean starting point.

A toggle should never need a “Save” button after it; if the change only applies later, a checkbox is more honest. Put a label next to it that says what “on” means, and check that the off color has enough contrast on your background.

## Code

### React (Motion)

```tsx
import { useState } from "react";
import { motion } from "framer-motion";

interface ToggleSwitchProps {
  color?: string;
  size?: number;
  checked?: boolean;
  defaultChecked?: boolean;
  onCheckedChange?: (checked: boolean) => void;
  label?: string;
}

export default function ToggleSwitch({
  color = "#0071e3",
  size = 36,
  checked,
  defaultChecked = true,
  onCheckedChange,
  label = "Turn on",
}: ToggleSwitchProps) {
  const [internal, setInternal] = useState(defaultChecked);
  const on = checked ?? internal;

  const toggle = () => {
    if (checked === undefined) setInternal(!on);
    onCheckedChange?.(!on);
  };

  return (
    <button
      type="button"
      role="switch"
      aria-checked={on}
      aria-label={label}
      onClick={toggle}
      className="flex shrink-0 items-center rounded-full p-1 transition-colors"
      style={{
        width: size * 1.85,
        height: size,
        backgroundColor: on ? color : "oklch(0.3 0.012 260)",
      }}
    >
      <motion.span
        className="rounded-full bg-white shadow"
        style={{ width: size - 8, height: size - 8 }}
        animate={{ x: on ? size * 0.85 : 0 }}
        transition={{ type: "spring", stiffness: 500, damping: 30 }}
      />
    </button>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Toggle switch” block (Inputs) from AIMRAN Library to my project.

## What it is
An on/off switch whose knob springs across, with a color and size you can set to match your design.

## Chosen settings
- Color: #0071e3
- Size: 36px (from 28 to 64px)

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with React + TypeScript, with framer-motion for the animation and lucide-react for the icons. If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
import { useState } from "react";
import { motion } from "framer-motion";

interface ToggleSwitchProps {
  color?: string;
  size?: number;
  checked?: boolean;
  defaultChecked?: boolean;
  onCheckedChange?: (checked: boolean) => void;
  label?: string;
}

export default function ToggleSwitch({
  color = "#0071e3",
  size = 36,
  checked,
  defaultChecked = true,
  onCheckedChange,
  label = "Turn on",
}: ToggleSwitchProps) {
  const [internal, setInternal] = useState(defaultChecked);
  const on = checked ?? internal;

  const toggle = () => {
    if (checked === undefined) setInternal(!on);
    onCheckedChange?.(!on);
  };

  return (
    <button
      type="button"
      role="switch"
      aria-checked={on}
      aria-label={label}
      onClick={toggle}
      className="flex shrink-0 items-center rounded-full p-1 transition-colors"
      style={{
        width: size * 1.85,
        height: size,
        backgroundColor: on ? color : "oklch(0.3 0.012 260)",
      }}
    >
      <motion.span
        className="rounded-full bg-white shadow"
        style={{ width: size - 8, height: size - 8 }}
        animate={{ x: on ? size * 0.85 : 0 }}
        transition={{ type: "spring", stiffness: 500, damping: 30 }}
      />
    </button>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
