iOS-style toggle
A compact switch in the style of iPhone settings, green when on, with a natural spring to the knob.
Settings
Code
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
Paste it into ChatGPT, Claude or Cursor and it will adapt the block to your project with these settings, even if you don’t use React.
More in Inputs
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.