{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"hold-to-delete","type":"registry:component","title":"Hold to delete","description":"Fills slowly while you commit to it, snaps back the moment you let go.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"components/hold-to-delete.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport { motion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\ntype State = \"idle\" | \"holding\" | \"done\";\n\n// The lid slams, then the can swaps for a check once it has shut. Long\n// enough to see the lid land, short enough that \"Deleted\" still feels like\n// the answer to letting go.\nconst SWAP_DELAY = 0.22;\n\n// A one-shot, so a keyframe: the button gives a small gulp as the lid\n// shuts, the way a bin jolts when its lid drops.\nconst CSS = `\n@keyframes htd-gulp {\n  0% { scale: 1; }\n  35% { scale: 0.97; }\n  100% { scale: 1; }\n}\n.htd-gulp { animation: htd-gulp 320ms cubic-bezier(0.23, 1, 0.32, 1) 120ms; }\n@media (prefers-reduced-motion: reduce) {\n  .htd-gulp { animation: none; }\n}\n`;\n\nexport function HoldToDelete({\n  onDelete,\n  className,\n}: {\n  onDelete?: () => void;\n  className?: string;\n}) {\n  const [state, setState] = useState<State>(\"idle\");\n  const reset = useRef<ReturnType<typeof setTimeout>>(undefined);\n\n  useEffect(() => () => clearTimeout(reset.current), []);\n\n  const press = () => setState((s) => (s === \"idle\" ? \"holding\" : s));\n  const release = () => setState((s) => (s === \"holding\" ? \"idle\" : s));\n\n  return (\n    <button\n      type=\"button\"\n      className={cn(\n        \"relative h-10 touch-manipulation rounded-full bg-muted px-4 text-sm font-medium text-foreground shadow-raised transition-[scale] duration-150 ease-out select-none active:scale-[0.96] motion-reduce:transition-none\",\n        state === \"done\" && \"htd-gulp\",\n        className,\n      )}\n      onPointerDown={(e) => {\n        if (e.button === 0) press();\n      }}\n      onPointerUp={release}\n      onPointerLeave={release}\n      onPointerCancel={release}\n      onContextMenu={(e) => e.preventDefault()}\n      onKeyDown={(e) => {\n        if ((e.key === \" \" || e.key === \"Enter\") && !e.repeat) {\n          e.preventDefault();\n          press();\n        }\n      }}\n      onKeyUp={(e) => {\n        if (e.key === \" \" || e.key === \"Enter\") release();\n      }}\n    >\n      <style href=\"hold-to-delete\" precedence=\"default\">\n        {CSS}\n      </style>\n      <Label state={state} />\n      {/* Fills over 2s while held but snaps back in 200ms on release: slow\n          where the user is deciding, fast where the interface responds. */}\n      <span\n        aria-hidden\n        className={cn(\n          \"absolute inset-0 flex items-center justify-center rounded-full bg-destructive px-4 text-white\",\n          state === \"idle\" &&\n            \"[clip-path:inset(0_100%_0_0)] transition-[clip-path] duration-200 ease-out\",\n          state === \"holding\" &&\n            \"[clip-path:inset(0)] transition-[clip-path] duration-[2000ms] ease-linear\",\n          state === \"done\" && \"[clip-path:inset(0)]\",\n        )}\n        onTransitionEnd={(e) => {\n          if (e.propertyName !== \"clip-path\" || state !== \"holding\") return;\n          setState(\"done\");\n          onDelete?.();\n          navigator.vibrate?.(10);\n          reset.current = setTimeout(() => setState(\"idle\"), 2000);\n        }}\n      >\n        <Label state={state} />\n      </span>\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {state === \"done\" ? \"Deleted\" : \"\"}\n      </span>\n    </button>\n  );\n}\n\n// Both labels share one grid cell, so the button keeps the width of the\n// longer one and never jumps when they swap.\nfunction Label({ state }: { state: State }) {\n  const done = state === \"done\";\n  // Only the swap into \"Deleted\" waits for the lid; the way back is instant.\n  const delay = done ? SWAP_DELAY : 0;\n  return (\n    <span className=\"grid\">\n      <Variant visible={!done} delay={delay} icon={<Bin state={state} />}>\n        Hold to delete\n      </Variant>\n      <Variant\n        visible={done}\n        delay={delay}\n        icon={<path d=\"m3.5 8.5 3 3 6-7\" />}\n      >\n        Deleted\n      </Variant>\n    </span>\n  );\n}\n\n// The bin opens as you commit. The lid is hinged at its right end and lifts\n// on the same 2s hold as the fill, but eased out, so it cracks open at once\n// (the press was heard) and then creeps wider while you decide. Letting go\n// early lowers it in 200ms; finishing slams it shut with a small overshoot.\nfunction Bin({ state }: { state: State }) {\n  return (\n    <>\n      <g\n        className={cn(\n          // View-box units, so the hinge sits exactly on the lid's right end.\n          \"[transform-box:view-box] [transform-origin:13.25px_4.25px]\",\n          state === \"holding\"\n            ? \"translate-y-[-1px] rotate-[38deg] transition-[rotate,translate] duration-[2000ms] ease-[cubic-bezier(0.23,1,0.32,1)]\"\n            : state === \"done\"\n              ? \"transition-[rotate,translate] duration-[260ms] ease-[cubic-bezier(0.34,1.8,0.64,1)]\"\n              : \"transition-[rotate,translate] duration-200 ease-out\",\n          \"motion-reduce:translate-y-0 motion-reduce:rotate-0 motion-reduce:transition-none\",\n        )}\n      >\n        <path d=\"M2.75 4.25h10.5M6.25 4.25v-1.5h3.5v1.5\" />\n      </g>\n      <path d=\"M4 4.25l.6 8.1a1 1 0 0 0 1 .9h4.8a1 1 0 0 0 1-.9l.6-8.1M6.75 7v3.75M9.25 7v3.75\" />\n    </>\n  );\n}\n\nfunction Variant({\n  visible,\n  delay,\n  icon,\n  children,\n}: {\n  visible: boolean;\n  delay: number;\n  icon: React.ReactNode;\n  children: React.ReactNode;\n}) {\n  return (\n    <span className=\"col-start-1 row-start-1 flex items-center justify-center gap-2\">\n      <Icon visible={visible} delay={delay}>\n        {icon}\n      </Icon>\n      <span\n        style={{ transitionDelay: `${delay}s` }}\n        className={cn(\n          \"transition-[opacity,filter] duration-200 ease-out\",\n          !visible && \"opacity-0 blur-[4px]\",\n        )}\n      >\n        {children}\n      </span>\n    </span>\n  );\n}\n\nfunction Icon({\n  visible,\n  delay,\n  children,\n}: {\n  visible: boolean;\n  delay: number;\n  children: React.ReactNode;\n}) {\n  return (\n    <motion.svg\n      viewBox=\"0 0 16 16\"\n      // The open lid swings past the icon's box, so nothing may clip it.\n      className=\"size-4 shrink-0 overflow-visible\"\n      fill=\"none\"\n      stroke=\"currentColor\"\n      strokeWidth={1.5}\n      strokeLinecap=\"round\"\n      strokeLinejoin=\"round\"\n      initial={false}\n      animate={\n        visible\n          ? { scale: 1, opacity: 1, filter: \"blur(0px)\" }\n          : { scale: 0.25, opacity: 0, filter: \"blur(4px)\" }\n      }\n      transition={{ type: \"spring\", duration: 0.3, bounce: 0, delay }}\n    >\n      {children}\n    </motion.svg>\n  );\n}\n\nexport default function HoldToDeleteDemo() {\n  return <HoldToDelete />;\n}\n"}],"cssVars":{"light":{"shadow-raised":"0 0 0 1px oklch(0 0 0 / 0.06), 0 1px 2px oklch(0 0 0 / 0.06), 0 6px 16px -6px oklch(0 0 0 / 0.12)"},"dark":{"shadow-raised":"0 0 0 1px oklch(1 0 0 / 0.08), 0 1px 2px oklch(0 0 0 / 0.4), 0 6px 16px -6px oklch(0 0 0 / 0.6)"},"theme":{"shadow-raised":"var(--shadow-raised)"}},"categories":["buttons"],"docs":"From ui lab: https://lab.xevrion.dev/lab/hold-to-delete"}