Components
Forms & Inputs

Spring Switch

An accessible toggle switch for React with a spring thumb and a press-down feel. Keyboard and screen reader friendly.

Want to understand how it works? Read the guide.

Notifications

Off. All quiet.

Plays on its own. Click to take over.

Install

Copies the source into components/ui/spring-switch.tsx and installs what it needs.

$ pnpm dlx shadcn@latest add https://makeui.xyz/r/spring-switch.json

Usage

app/page.tsx
import { SpringSwitch } from "@/components/ui/spring-switch"

export default function Example() {
  return <SpringSwitch label="Notifications" onChange={(on) => console.log(on)} />
}

Source

Prefer to copy by hand? It's one file. Requires motion and the shadcn cn helper.

components/ui/spring-switch.tsx
"use client"

import { useState } from "react"
import { motion } from "motion/react"
import { cn } from "@/lib/utils"

type Props = {
  label?: string
  /** Controlled state. Leave out to let it manage itself. */
  checked?: boolean
  defaultChecked?: boolean
  onChange?: (checked: boolean) => void
  className?: string
}

/** Switch with a spring thumb and a press-down feel. */
export function SpringSwitch({ label = "Notifications", checked, defaultChecked = false, onChange, className }: Props) {
  const [internal, setInternal] = useState(defaultChecked)
  const on = checked ?? internal

  return (
    <div className={cn("flex items-center gap-4", className)}>
      <span className="text-sm font-medium text-neutral-800 dark:text-neutral-100">{label}</span>
      <motion.button
        role="switch"
        aria-checked={on}
        aria-label={label}
        onClick={() => {
          setInternal(!on)
          onChange?.(!on)
        }}
        whileTap={{ scale: 0.94 }}
        className={cn(
          "relative h-8 w-14 shrink-0 rounded-full p-1 outline-none transition-colors duration-300 focus-visible:ring-2 focus-visible:ring-neutral-400/60",
          on ? "bg-emerald-500" : "bg-neutral-300 dark:bg-neutral-700"
        )}
      >
        <motion.span
          className="block size-6 rounded-full bg-white shadow-[0_1px_3px_rgb(0_0_0/0.3)]"
          initial={false}
          animate={{ x: on ? 24 : 0 }}
          transition={{ type: "spring", stiffness: 520, damping: 30 }}
        />
      </motion.button>
    </div>
  )
}