{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"crumple-delete","type":"registry:ui","title":"Crumple Delete","description":"Deleting a note scrunches it into a paper ball that arcs into a bin, whose lid flips open and wobbles shut. Undo tosses it back out and smooths it flat.","dependencies":["motion","lucide-react"],"registryDependencies":[],"files":[{"path":"registry/ui/crumple-delete.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport { useAnimate, useReducedMotion } from \"motion/react\";\nimport { Trash2 } from \"lucide-react\";\n\nexport interface CrumpleDeleteProps {\n  /** Note title. */\n  title?: string;\n  /** Note text. */\n  body?: string;\n  /** Ms the Undo button stays available after binning. */\n  undoDuration?: number;\n  /** Called when the undo window closes, i.e. the delete is final. */\n  onDelete?: () => void;\n  /** Called when the note is restored with Undo. */\n  onUndo?: () => void;\n  /** Accent used on the bin and undo bar. */\n  color?: string;\n  className?: string;\n}\n\ntype Phase = \"idle\" | \"busy\" | \"binned\" | \"gone\";\n\n// Arc from the note to the bin, sampled so it reads as a real toss.\nconst arc = (dx: number, dy: number, h: number, reverse = false) => {\n  const ts = [0, 0.15, 0.3, 0.45, 0.6, 0.75, 0.9, 1].map((t) => (reverse ? 1 - t : t));\n  return { x: ts.map((t) => dx * t), y: ts.map((t) => dy * t - h * 4 * t * (1 - t)) };\n};\n\nexport function CrumpleDelete({\n  title = \"Grocery list\",\n  body = \"Oat milk, lemons, basil, the good sourdough, something for Sunday.\",\n  undoDuration = 5000,\n  onDelete,\n  onUndo,\n  color = \"#f87171\",\n  className,\n}: CrumpleDeleteProps) {\n  const [phase, setPhase] = useState<Phase>(\"idle\");\n  const [scope, animate] = useAnimate<HTMLDivElement>();\n  const reduce = useReducedMotion();\n  const delta = useRef({ dx: 0, dy: 0 });\n  const del = useRef<HTMLButtonElement>(null);\n  const undo = useRef<HTMLButtonElement>(null);\n  const onDeleteRef = useRef(onDelete);\n  useEffect(() => {\n    onDeleteRef.current = onDelete;\n  });\n\n  const s = (sec: number) => (reduce ? 0 : sec);\n  const springy = (stiffness: number, damping: number) =>\n    reduce ? { duration: 0 } : ({ type: \"spring\", stiffness, damping } as const);\n\n  useEffect(() => {\n    if (phase !== \"binned\") return;\n    undo.current?.focus();\n    const id = window.setTimeout(() => {\n      setPhase(\"gone\");\n      onDeleteRef.current?.();\n    }, undoDuration);\n    return () => window.clearTimeout(id);\n  }, [phase, undoDuration]);\n\n  const measure = () => {\n    const root = scope.current;\n    const b = root.querySelector(\"[data-ball]\")!.getBoundingClientRect();\n    const bin = root.querySelector(\"[data-bin]\")!.getBoundingClientRect();\n    delta.current = {\n      dx: bin.left + bin.width / 2 - (b.left + b.width / 2),\n      dy: bin.top + 10 - (b.top + b.height / 2),\n    };\n  };\n\n  const crumple = async () => {\n    if (phase !== \"idle\") return;\n    setPhase(\"busy\");\n    measure();\n    const { dx, dy } = delta.current;\n    // Anticipation: the note tenses up before it gets scrunched.\n    await animate(\"[data-card]\", { scaleX: 1.04, scaleY: 0.94, rotate: -2 }, { duration: s(0.12) });\n    animate(\"[data-crease]\", { opacity: 1 }, { duration: s(0.3) });\n    await animate(\n      \"[data-card]\",\n      { scaleX: 0.2, scaleY: 0.3, rotate: 28, borderRadius: 80 },\n      { duration: s(0.34), ease: [0.55, 0, 0.8, 0.45] },\n    );\n    animate(\"[data-card]\", { opacity: 0 }, { duration: s(0.06) });\n    await animate(\"[data-ball]\", { opacity: 1, scale: [0.6, 1.15, 1] }, { duration: s(0.2) });\n    animate(\"[data-lid]\", { rotate: 60 }, springy(420, 13));\n    const path = arc(dx, dy, 120);\n    await animate(\n      \"[data-ball]\",\n      { ...path, rotate: [0, 460] },\n      { duration: s(0.62), ease: \"linear\" },\n    );\n    await animate(\"[data-ball]\", { y: dy + 26, scale: 0.75, opacity: 0 }, { duration: s(0.16), ease: \"easeIn\" });\n    animate(\"[data-lid]\", { rotate: 0 }, springy(500, 16));\n    await animate(\"[data-bin]\", { rotate: [0, -8, 6, -3, 0], scaleY: [1, 0.9, 1.05, 1] }, { duration: s(0.45) });\n    setPhase(\"binned\");\n  };\n\n  const restore = async () => {\n    if (phase !== \"binned\") return;\n    setPhase(\"busy\");\n    const { dx, dy } = delta.current;\n    await animate(\"[data-lid]\", { rotate: 60 }, springy(420, 13));\n    animate(\"[data-bin]\", { scaleY: [1, 0.9, 1] }, { duration: s(0.25) });\n    await animate(\n      \"[data-ball]\",\n      { ...arc(dx, dy, 120, true), rotate: [460, 0], opacity: [1, 1], scale: [0.8, 1] },\n      { duration: s(0.55), ease: \"linear\" },\n    );\n    animate(\"[data-lid]\", { rotate: 0 }, springy(500, 16));\n    animate(\"[data-ball]\", { opacity: 0, scale: 1.2 }, { duration: s(0.12) });\n    animate(\"[data-card]\", { opacity: 1 }, { duration: s(0.08) });\n    animate(\"[data-crease]\", { opacity: 0 }, { duration: s(1.2), delay: s(0.2) });\n    // Smooth the paper back out with an overshoot.\n    await animate(\"[data-card]\", { scaleX: 1, scaleY: 1, rotate: 0, borderRadius: 16 }, springy(260, 14));\n    setPhase(\"idle\");\n    del.current?.focus();\n    onUndo?.();\n  };\n\n  return (\n    <div ref={scope} className={`relative h-[19rem] w-[22rem] max-w-full select-none ${className ?? \"\"}`}>\n      {phase !== \"gone\" && (\n        <div\n          data-card\n          className=\"absolute left-2 top-6 flex h-40 w-64 flex-col overflow-hidden border border-border bg-card p-5 text-foreground shadow-[0_18px_40px_-20px_rgb(0_0_0/.8)]\"\n          style={{ borderRadius: 16 }}\n          aria-hidden={phase !== \"idle\"}\n        >\n          <div className=\"flex items-start justify-between gap-3\">\n            <h3 className=\"font-semibold\">{title}</h3>\n            <button\n              ref={del}\n              type=\"button\"\n              onClick={crumple}\n              disabled={phase !== \"idle\"}\n              aria-label={`Delete note: ${title}`}\n              className=\"-m-1.5 grid size-8 shrink-0 place-items-center rounded-lg text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/60\"\n            >\n              <Trash2 className=\"size-4\" />\n            </button>\n          </div>\n          <p className=\"mt-2 line-clamp-3 text-sm text-muted-foreground\">{body}</p>\n          <svg data-crease aria-hidden className=\"pointer-events-none absolute inset-0 size-full\" style={{ opacity: 0 }} viewBox=\"0 0 256 160\" preserveAspectRatio=\"none\">\n            <g fill=\"none\" stroke=\"currentColor\" strokeWidth={1.2} className=\"text-muted-foreground/60\">\n              <path d=\"M0 40L70 62L120 30L190 70L256 48\" />\n              <path d=\"M30 160L64 100L140 118L170 60L230 160\" />\n              <path d=\"M0 110L60 100M140 118L256 104M120 30L140 0M190 70L210 0\" />\n            </g>\n          </svg>\n        </div>\n      )}\n\n      {/* The crumpled ball that gets tossed. */}\n      <div\n        data-ball\n        aria-hidden\n        className=\"pointer-events-none absolute left-28 top-20 size-12\"\n        style={{ opacity: 0 }}\n      >\n        <svg viewBox=\"0 0 48 48\" className=\"size-full overflow-visible drop-shadow-[0_6px_8px_rgb(0_0_0/.45)]\">\n          <path\n            d=\"M24 2L33 6L43 10L45 22L47 31L39 41L28 46L17 44L7 38L2 26L5 14L14 5Z\"\n            className=\"fill-muted stroke-muted-foreground/60\"\n            strokeWidth={1.5}\n            strokeLinejoin=\"round\"\n          />\n          <path\n            d=\"M14 5L20 18L5 14M20 18L33 6M20 18L26 30L45 22M26 30L17 44M26 30L39 41M2 26L20 18\"\n            fill=\"none\"\n            className=\"stroke-muted-foreground/70\"\n            strokeWidth={1.2}\n            strokeLinejoin=\"round\"\n          />\n        </svg>\n      </div>\n\n      {/* Bin */}\n      <div data-bin aria-hidden className=\"absolute bottom-4 right-4 h-20 w-16\" style={{ transformOrigin: \"50% 100%\" }}>\n        <div data-lid className=\"absolute -left-1 -right-1 top-0 h-3\" style={{ transformOrigin: \"100% 100%\" }}>\n          <span className=\"absolute left-1/2 -top-1.5 h-2 w-5 -translate-x-1/2 rounded-t-md border-2 border-b-0 border-muted-foreground/70\" />\n          <span className=\"absolute inset-0 rounded-md bg-muted-foreground/80\" />\n        </div>\n        <div\n          className=\"absolute inset-x-1 bottom-0 top-4 rounded-b-xl border-2 border-t-0 border-muted-foreground/70 bg-muted\"\n          style={{ clipPath: \"polygon(0 0,100% 0,90% 100%,10% 100%)\" }}\n        >\n          <div className=\"absolute inset-0 flex justify-evenly px-2 pt-2 pb-1\">\n            {[0, 1, 2].map((i) => (\n              <span key={i} className=\"w-1 rounded-full bg-muted-foreground/40\" />\n            ))}\n          </div>\n        </div>\n      </div>\n\n      {/* Undo bar */}\n      <div className=\"absolute bottom-4 left-2 right-24\" aria-live=\"polite\">\n        {phase === \"binned\" && (\n          <div className=\"relative flex items-center justify-between gap-3 overflow-hidden rounded-xl border border-border bg-card py-2 pl-3.5 pr-2 text-sm text-foreground shadow-lg\">\n            <span>Note binned</span>\n            <button\n              ref={undo}\n              type=\"button\"\n              onClick={restore}\n              className=\"rounded-lg px-2.5 py-1 font-semibold outline-none hover:bg-muted focus-visible:ring-[3px] focus-visible:ring-ring/60\"\n              style={{ color }}\n            >\n              Undo\n            </button>\n            <span\n              aria-hidden\n              className=\"absolute bottom-0 left-0 h-0.5 w-full origin-left\"\n              style={{ background: color, animation: `paper-crumple-timer ${undoDuration}ms linear forwards` }}\n            />\n            <style href=\"crumple-delete\" precedence=\"default\">\n              {`@keyframes paper-crumple-timer{from{transform:scaleX(1)}to{transform:scaleX(0)}}`}\n            </style>\n          </div>\n        )}\n        {phase === \"gone\" && <p className=\"py-2 text-sm text-muted-foreground\">Note deleted.</p>}\n      </div>\n    </div>\n  );\n}\n"}]}