# iOS-style toggle

> A compact switch in the style of iPhone settings, green when on, with a natural spring to the knob.

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

## Where to use it

Most people have flipped thousands of these on their phones, so they know exactly what it does without reading. It fits settings screens and preference panels: “Remember me” on a login, “Newsletter” in a customer account or “Auto-renew” in a subscription app. An iOS toggle switch in React that behaves as a proper switch for screen readers.

Green says “on” to iPhone users, but switch it to your brand color if green means something else on your site, like stock or success. Always give it a clear label, since the switch on its own says nothing about what it controls.

## Code

### React (Motion)

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

const spring = { type: "spring" as const, stiffness: 300, damping: 30 };

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

export default function IosToggle({
  color = "#34c759",
  checked,
  defaultChecked = true,
  onCheckedChange,
  label = "Turn on",
}: IosToggleProps) {
  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 h-8 w-14 items-center rounded-full p-1 transition-colors ${
        on ? "" : "bg-black/5 dark:bg-white/10"
      }`}
      style={on ? { backgroundColor: color } : undefined}
    >
      <motion.span
        className="h-6 w-6 rounded-full bg-white shadow-[0_1px_2px_rgba(0,0,0,0.03),0_8px_28px_rgba(0,0,0,0.05)]"
        animate={{ x: on ? 24 : 0 }}
        transition={spring}
      />
    </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 “iOS-style toggle” block (Inputs) from AIMRAN Library to my project.

## What it is
A compact switch in the style of iPhone settings, green when on, with a natural spring to the knob.

## Chosen settings
- Color: #34c759
- Active: yes

## 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";

const spring = { type: "spring" as const, stiffness: 300, damping: 30 };

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

export default function IosToggle({
  color = "#34c759",
  checked,
  defaultChecked = true,
  onCheckedChange,
  label = "Turn on",
}: IosToggleProps) {
  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 h-8 w-14 items-center rounded-full p-1 transition-colors ${
        on ? "" : "bg-black/5 dark:bg-white/10"
      }`}
      style={on ? { backgroundColor: color } : undefined}
    >
      <motion.span
        className="h-6 w-6 rounded-full bg-white shadow-[0_1px_2px_rgba(0,0,0,0.03),0_8px_28px_rgba(0,0,0,0.05)]"
        animate={{ x: on ? 24 : 0 }}
        transition={spring}
      />
    </button>
  );
}
```

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