Apple Switch
unlumen-ui/apple-switchFreeComponent
Animated toggle switch with separate track/thumb sizing, spring feedback, optional label text, and tone variants.
Live preview
live · rendered by the registry
Rendered by Unlumen Ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://ui.unlumen.com/r/apple-switch.jsonSource
1"use client";23import { motion, useMotionValue, useSpring, useTransform } from "motion/react";4import { forwardRef, useEffect, useId, useRef, useState } from "react";5import { cn } from "@/lib/utils";67const switchSizes = {8 sm: {9 trackX: 46,10 trackY: 24,11 thumbX: 22,12 thumbY: 18,13 padding: 3,14 },15 md: {16 trackX: 62,17 trackY: 30,18 thumbX: 32,19 thumbY: 24,20 padding: 4,21 },22 lg: {23 trackX: 74,24 trackY: 36,25 thumbX: 34,26 thumbY: 28,27 padding: 5,28 },29} as const;3031const switchTones = {32 neutral: {33 off: "color-mix(in srgb, var(--muted) 82%, transparent)",34 on: "#34c759",35 thumb: "#ffffff",36 glow: "color-mix(in srgb, #34c759 32%, transparent)",37 },38 accent: {39 off: "color-mix(in srgb, var(--muted) 82%, transparent)",40 on: "var(--foreground)",41 thumb: "#ffffff",42 glow: "color-mix(in srgb, var(--accent) 42%, transparent)",43 },44} as const;4546const thumbSpring = {47 stiffness: 700,48 damping: 48,49 mass: 0.55,50};5152const grabSpring = {53 stiffness: 500,54 damping: 25,55};5657interface AppleSwitchProps58 extends Omit<59 React.ButtonHTMLAttributes<HTMLButtonElement>,60 "onChange" | "role"61 > {62 checked?: boolean;63 defaultChecked?: boolean;64 onCheckedChange?: (checked: boolean) => void;65 label?: React.ReactNode;66 description?: React.ReactNode;67 /** @default "md" */68 size?: keyof typeof switchSizes;69 /** @default "neutral" */70 tone?: keyof typeof switchTones;71 /** @default "right" */72 labelSide?: "left" | "right";73}7475const clamp = (value: number, min: number, max: number) => {76 return Math.min(Math.max(value, min), max);77};7879const AppleSwitch = forwardRef<HTMLButtonElement, AppleSwitchProps>(80 (81 {82 checked,83 onCheckedChange,84 label,85 description,86 size = "md",87 tone = "neutral",88 labelSide = "right",89 className,90 style,91 disabled,92 defaultChecked,93 id,94 type = "button",95 onClick,96 onPointerCancel,97 onPointerDown,98 onPointerLeave,99 onPointerMove,100 onPointerUp,101 ...props102 },103 ref,104 ) => {105 const generatedId = useId();106 const switchId = id ?? generatedId;107 const [uncontrolledChecked, setUncontrolledChecked] = useState(108 Boolean(defaultChecked),109 );110 const currentChecked = checked ?? uncontrolledChecked;111 const metrics = switchSizes[size];112 const colors = switchTones[tone];113// … truncated
What it pulls in
npm packages
Files it writes
More from Unlumen Ui
All 233 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| AI Chatai-chat | Unlumen Ui | Components | Paid | 2 deps | |
| Vercel Animate Countanimate-count | Unlumen Ui | Components | Free | 1 dep | |
| Animate Digitsanimate-digits | Unlumen Ui | Components | Free | 1 dep | |
| Animated Inputanimate-text-input-typing | Unlumen Ui | Components | Paid | 1 dep | |
| Animated Listanimated-list | Unlumen Ui | Components | Free | 1 dep | |
| Aurora Barsaurora-bars | Unlumen Ui | Components | Free | 1 dep | |
| Aurora Bluraurora-blur | Unlumen Ui | Components | Free | 2 deps | |
| Aurora Cardaurora-card | Unlumen Ui | Components | Paid | 2 deps |
