"use client"; import { animate, motion, MotionConfig, useReducedMotion } from "motion/react"; import { useEffect, useId, useRef, useState } from "react"; import { useSound } from "@/lib/sound"; import { toggleOffSound, toggleOnSound } from "@/lib/sounds/toggle"; import { cn } from "@/lib/utils"; // Heavy, deliberate thumb - high mass keeps the travel weighty without wobble. const THUMB_SPRING = { type: "spring", stiffness: 800, damping: 80, mass: 4 } as const; export interface SwitchProps { /** Whether the switch is on. */ checked: boolean; /** Called with the new state when the switch is toggled. */ onCheckedChange: (checked: boolean) => void; /** Dims the switch and stops it from toggling. */ disabled?: boolean; /** Visible text beside the switch, also its accessible name. */ label?: string; /** Accessible name when there is no visible `label`. */ ariaLabel?: string; /** Play a rising blip on, a falling one off. Off by default; a `SoundProvider` can mute it app-wide. */ sound?: boolean; /** Classes merged onto the root element. */ className?: string; } export function Switch({ checked, onCheckedChange, disabled, label, ariaLabel, sound = false, className, }: SwitchProps) { const id = useId(); const playOn = useSound(toggleOnSound, { enabled: sound }); const playOff = useSound(toggleOffSound, { enabled: sound }); const thumbRef = useRef(null); const reduce = useReducedMotion(); const [isPressed, setIsPressed] = useState(false); // Disabled shake feedback when pressed. useEffect(() => { if (!thumbRef.current || reduce) return; if (disabled && isPressed) { animate( thumbRef.current, { x: [0, -2, 2, -1, 0] }, { delay: 0.2, duration: 0.6 }, ); } }, [disabled, isPressed, reduce]); const squish = !disabled && isPressed && !reduce; return ( { if (disabled) return; if (checked) playOff(); else playOn(); onCheckedChange(!checked); }} onPointerDown={() => setIsPressed(true)} onPointerUp={() => setIsPressed(false)} onPointerLeave={() => setIsPressed(false)} initial={false} data-state={checked ? "checked" : "unchecked"} className={cn( "group peer inline-flex h-7 w-12 shrink-0 cursor-pointer items-center px-1 rounded-full outline-none transition-colors duration-200", "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background", "disabled:cursor-not-allowed disabled:opacity-60", checked ? "justify-end bg-primary" : "justify-start bg-muted-foreground/60", )} > {/* Stretch toward the destination while active. */}
{label ? ( ) : null} ); }