{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "hold-to-confirm-button",
  "type": "registry:ui",
  "title": "Hold To Confirm Button",
  "author": "Rishabh Bose <https://theb0se.vercel.app/>",
  "description": "Press-and-hold safety action button with a spring-pressured progress fill that recoils elastically when released early.",
  "dependencies": [
    "framer-motion",
    "lucide-react",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "components/ui/hold-to-confirm-button.tsx",
      "content": "\"use client\";\n\nimport React, { useEffect, useRef, useState } from \"react\";\nimport {\n  motion,\n  useMotionValue,\n  useSpring,\n  useTransform,\n  useReducedMotion,\n} from \"framer-motion\";\nimport { ShieldAlert, Check, RotateCcw } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface HoldToConfirmButtonProps {\n  /** Default button label before confirmation. Default: \"Hold to Purge Cache\" */\n  label?: string;\n  /** Label displayed once hold threshold is completed. Default: \"Cache Purged\" */\n  confirmedLabel?: string;\n  /** Hold duration in milliseconds required to trigger confirmation. Default: 1400 */\n  holdDuration?: number;\n  /** Visual variant. Default: \"default\" */\n  variant?: \"default\" | \"danger\";\n  /** Disable button interactions. Default: false */\n  disabled?: boolean;\n  /** Spring stiffness coefficient for fill bar snap-back. Default: 280 */\n  stiffness?: number;\n  /** Spring damping coefficient for fill bar snap-back. Default: 24 */\n  damping?: number;\n  /** Callback fired when hold completes 100% */\n  onConfirm?: () => void;\n  className?: string;\n}\n\nexport function HoldToConfirmButton({\n  label = \"Hold to Purge Cache\",\n  confirmedLabel = \"Cache Purged\",\n  holdDuration = 1400,\n  variant = \"default\",\n  disabled = false,\n  stiffness = 280,\n  damping = 24,\n  onConfirm,\n  className,\n}: HoldToConfirmButtonProps) {\n  const shouldReduceMotion = useReducedMotion() ?? false;\n  const [isHolding, setIsHolding] = useState(false);\n  const [isConfirmed, setIsConfirmed] = useState(false);\n\n  const progress = useMotionValue(0);\n  const springProgress = useSpring(progress, {\n    stiffness,\n    damping,\n    mass: 0.4,\n  });\n\n  const fillWidth = useTransform(\n    springProgress,\n    (v) => `${Math.max(0, Math.min(100, v))}%`\n  );\n\n  const rafRef = useRef<number | null>(null);\n  const startTimeRef = useRef<number>(0);\n\n  const stopHolding = () => {\n    if (rafRef.current !== null) {\n      cancelAnimationFrame(rafRef.current);\n      rafRef.current = null;\n    }\n    setIsHolding(false);\n    if (!isConfirmed) {\n      progress.set(0);\n    }\n  };\n\n  const startHolding = () => {\n    if (disabled) return;\n    if (isConfirmed) {\n      setIsConfirmed(false);\n      progress.set(0);\n      return;\n    }\n\n    setIsHolding(true);\n    startTimeRef.current = performance.now();\n\n    const tick = (now: number) => {\n      const elapsed = now - startTimeRef.current;\n      const pct = Math.min(100, (elapsed / holdDuration) * 100);\n      progress.set(pct);\n\n      if (pct >= 100) {\n        setIsHolding(false);\n        setIsConfirmed(true);\n        onConfirm?.();\n        rafRef.current = null;\n      } else {\n        rafRef.current = requestAnimationFrame(tick);\n      }\n    };\n\n    rafRef.current = requestAnimationFrame(tick);\n  };\n\n  useEffect(() => {\n    return () => {\n      if (rafRef.current !== null) {\n        cancelAnimationFrame(rafRef.current);\n      }\n    };\n  }, []);\n\n  const handleKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>) => {\n    if (disabled) return;\n    if ((e.key === \" \" || e.key === \"Enter\") && !e.repeat && !isHolding) {\n      e.preventDefault();\n      startHolding();\n    }\n  };\n\n  const handleKeyUp = (e: React.KeyboardEvent<HTMLButtonElement>) => {\n    if (e.key === \" \" || e.key === \"Enter\") {\n      e.preventDefault();\n      stopHolding();\n    }\n  };\n\n  return (\n    <div className=\"inline-flex flex-col items-center gap-2\">\n      <motion.button\n        type=\"button\"\n        disabled={disabled}\n        aria-live=\"polite\"\n        onPointerDown={startHolding}\n        onPointerUp={stopHolding}\n        onPointerLeave={stopHolding}\n        onPointerCancel={stopHolding}\n        onKeyDown={handleKeyDown}\n        onKeyUp={handleKeyUp}\n        animate={{\n          scale: isHolding && !shouldReduceMotion && !disabled ? 0.96 : 1,\n        }}\n        transition={{ type: \"spring\", stiffness: 400, damping: 22 }}\n        className={cn(\n          \"group relative inline-flex h-12 min-w-[220px] select-none touch-none items-center justify-center overflow-hidden rounded-2xl border px-6 text-xs font-semibold tracking-tight transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-black dark:focus-visible:ring-white disabled:cursor-not-allowed disabled:opacity-45\",\n          isConfirmed\n            ? variant === \"danger\"\n              ? \"border-red-600 bg-red-600 text-white dark:border-red-500 dark:bg-red-500 dark:text-white\"\n              : \"border-black bg-black text-white dark:border-white dark:bg-white dark:text-black\"\n            : variant === \"danger\"\n              ? \"border-red-300 bg-red-50/60 text-red-700 hover:border-red-500 dark:border-red-900/60 dark:bg-red-950/30 dark:text-red-300 dark:hover:border-red-700\"\n              : \"border-neutral-300 bg-white text-black hover:border-black dark:border-[#262626] dark:bg-[#0A0A0A] dark:text-white dark:hover:border-white\",\n          className\n        )}\n      >\n        {/* Spring-pressured progress fill overlay */}\n        {!isConfirmed && (\n          <motion.span\n            aria-hidden=\"true\"\n            style={{ width: fillWidth }}\n            className={cn(\n              \"pointer-events-none absolute inset-y-0 left-0\",\n              variant === \"danger\"\n                ? \"bg-red-500/20 dark:bg-red-500/25\"\n                : \"bg-black/10 dark:bg-white/15\"\n            )}\n          />\n        )}\n\n        <span className=\"relative z-10 flex items-center gap-2\">\n          {isConfirmed ? (\n            <>\n              <Check className=\"h-4 w-4\" />\n              <span>{confirmedLabel}</span>\n              <RotateCcw className=\"ml-1 h-3 w-3 opacity-60\" />\n            </>\n          ) : (\n            <>\n              <ShieldAlert\n                className={cn(\n                  \"h-4 w-4 transition-transform\",\n                  isHolding && !shouldReduceMotion && \"scale-110\"\n                )}\n              />\n              <span>{isHolding ? \"Keep Holding...\" : label}</span>\n            </>\n          )}\n        </span>\n      </motion.button>\n    </div>\n  );\n}\n\nexport default HoldToConfirmButton;\n",
      "type": "registry:ui",
      "target": "components/ui/hold-to-confirm-button.tsx"
    },
    {
      "path": "lib/utils.ts",
      "content": "import { type ClassValue, clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport async function copyToClipboard(text: string): Promise<boolean> {\n  if (typeof window === \"undefined\") return false;\n\n  try {\n    if (navigator.clipboard && window.isSecureContext) {\n      await navigator.clipboard.writeText(text);\n      return true;\n    }\n\n    const textArea = document.createElement(\"textarea\");\n    textArea.value = text;\n    textArea.style.position = \"fixed\";\n    textArea.style.left = \"-999999px\";\n    textArea.style.top = \"-999999px\";\n    document.body.appendChild(textArea);\n    textArea.focus();\n    textArea.select();\n    const succeeded = document.execCommand(\"copy\");\n    textArea.remove();\n    return succeeded;\n  } catch {\n    return false;\n  }\n}\n",
      "type": "registry:lib",
      "target": "lib/utils.ts"
    }
  ]
}
