React + MotionInputs

Interruptor toggle

Un interruptor de encendido y apagado cuyo círculo cruza con efecto muelle, con color y tamaño a tu gusto.

Ajustes

36px

Código

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 = "Activar",
}: 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 para IA

Pégalo en ChatGPT, Claude o Cursor y adaptará el bloque a tu proyecto con estos ajustes, aunque no uses React.

Más en Inputs

Dónde usarlo

Usa un toggle para ajustes que se aplican al momento: modo oscuro, avisos por email, «Ver precios con IVA» en una tienda B2B o la disponibilidad en un panel de reservas. El círculo con muelle encaja con un pequeño chasquido muy agradable. Si buscabas un toggle switch animado en React, aquí tienes una buena base.

Un toggle no debería necesitar un botón de «Guardar» después; si el cambio se aplica más tarde, una casilla es más honesta. Ponle al lado una etiqueta que diga qué significa encendido y revisa que el color apagado contraste con tu fondo.