{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"peel-decal","type":"registry:ui","title":"Peel Decal","description":"A glossy sticker whose corner curls back on hover and follows your drag, showing its paper back and a soft lifted shadow.","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"registry/ui/peel-decal.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, type PointerEvent, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport type StickerCorner = \"bottom-right\" | \"bottom-left\" | \"top-right\" | \"top-left\";\n\nexport interface PeelDecalProps {\n  /** The sticker face. Should fill the box. */\n  children: ReactNode;\n  /** Corner that lifts. */\n  corner?: StickerCorner;\n  /** Px the corner is lifted at rest, as a hint. */\n  restPeel?: number;\n  /** Px the corner lifts on hover or focus. */\n  hoverPeel?: number;\n  /** Color of the sticker's paper back. */\n  backColor?: string;\n  /** Corner radius of the sticker in px; match your face. */\n  radius?: number;\n  /** Accessible name. */\n  label?: string;\n  className?: string;\n}\n\ntype Pt = [number, number];\n\n// Keep the part of a polygon where side(p) >= 0 (one Sutherland–Hodgman pass).\nfunction clip(poly: Pt[], side: (p: Pt) => number) {\n  const out: Pt[] = [];\n  poly.forEach((a, i) => {\n    const b = poly[(i + 1) % poly.length];\n    const fa = side(a);\n    const fb = side(b);\n    if (fa >= 0) out.push(a);\n    if (fa >= 0 !== fb >= 0) {\n      const t = fa / (fa - fb);\n      out.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]);\n    }\n  });\n  return out;\n}\n\nconst poly = (pts: Pt[]) => (pts.length < 3 ? \"polygon(0 0)\" : `polygon(${pts.map(([x, y]) => `${x}px ${y}px`).join(\",\")})`);\n\nexport function PeelDecal({\n  children,\n  corner = \"bottom-right\",\n  restPeel = 18,\n  hoverPeel = 90,\n  backColor = \"#f4f4f5\",\n  radius = 28,\n  label = \"Sticker. Press Enter to peel\",\n  className,\n}: PeelDecalProps) {\n  const root = useRef<HTMLDivElement>(null);\n  const face = useRef<HTMLDivElement>(null);\n  const flap = useRef<HTMLDivElement>(null);\n  const api = useRef<{ hover: (on: boolean) => void; drag: (x: number, y: number) => void; release: () => void; toggle: () => void }>(null);\n\n  useEffect(() => {\n    const el = root.current!;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const cornerPt = (): Pt => [corner.includes(\"right\") ? el.offsetWidth : 0, corner.includes(\"bottom\") ? el.offsetHeight : 0];\n    // A point `amount` px in from the corner along the diagonal.\n    const inset = (amount: number): Pt => {\n      const [cx, cy] = cornerPt();\n      const dx = el.offsetWidth / 2 - cx;\n      const dy = el.offsetHeight / 2 - cy;\n      const l = Math.hypot(dx, dy);\n      return [cx + (dx / l) * amount, cy + (dy / l) * amount];\n    };\n\n    let p = inset(restPeel);\n    let target = p;\n    let hovering = false;\n    let dragging = false;\n    let open = false;\n    let raf = 0;\n\n    const render = () => {\n      if (!face.current || !flap.current) return; // unmounted\n      const W = el.offsetWidth;\n      const H = el.offsetHeight;\n      const [cx, cy] = cornerPt();\n      const len = Math.hypot(cx - p[0], cy - p[1]);\n      const rect: Pt[] = [[0, 0], [W, 0], [W, H], [0, H]];\n      if (len < 0.5) {\n        face.current!.style.clipPath = \"\";\n        flap.current!.style.clipPath = \"polygon(0 0)\";\n        return;\n      }\n      // The fold is the perpendicular bisector between the corner and where it is pulled to.\n      const nx = (cx - p[0]) / len;\n      const ny = (cy - p[1]) / len;\n      const k = ((cx + p[0]) / 2) * nx + ((cy + p[1]) / 2) * ny;\n      face.current!.style.clipPath = poly(clip(rect, ([x, y]) => k - (x * nx + y * ny)));\n      const f = flap.current!;\n      f.style.clipPath = poly(clip(rect, ([x, y]) => x * nx + y * ny - k));\n      // Mirror the folded part across the fold line.\n      f.style.transform = `matrix(${1 - 2 * nx * nx},${-2 * nx * ny},${-2 * nx * ny},${1 - 2 * ny * ny},${2 * k * nx},${2 * k * ny})`;\n      // Shade the back: dark crease at the fold, a highlight just past it, soft falloff to the tip.\n      const L = Math.abs(W * nx) + Math.abs(H * ny);\n      const c = (W / 2) * nx + (H / 2) * ny;\n      const tf = ((k - c) / L + 0.5) * 100;\n      const tc = ((cx * nx + cy * ny - c) / L + 0.5) * 100;\n      const at = (s: number) => `${tf + (tc - tf) * s}%`;\n      const angle = (Math.atan2(nx, -ny) * 180) / Math.PI;\n      f.style.backgroundImage = `linear-gradient(${angle}deg, rgba(0,0,0,.28) ${at(0)}, rgba(255,255,255,.7) ${at(0.12)}, rgba(255,255,255,0) ${at(0.5)}, rgba(0,0,0,.14) ${at(1)})`;\n    };\n\n    const tick = () => {\n      const ease = dragging ? 0.45 : 0.16;\n      p = [p[0] + (target[0] - p[0]) * ease, p[1] + (target[1] - p[1]) * ease];\n      render();\n      raf = Math.hypot(target[0] - p[0], target[1] - p[1]) > 0.3 ? requestAnimationFrame(tick) : 0;\n    };\n    const go = (t: Pt) => {\n      target = t;\n      if (reduced) {\n        p = t;\n        render();\n      } else if (!raf) raf = requestAnimationFrame(tick);\n    };\n    const settle = () => go(inset(open ? Math.min(el.offsetWidth, el.offsetHeight) * 0.7 : hovering ? hoverPeel : restPeel));\n\n    api.current = {\n      hover: (on) => {\n        hovering = on;\n        if (!dragging) settle();\n      },\n      drag: (x, y) => {\n        dragging = true;\n        const [cx, cy] = cornerPt();\n        const max = Math.max(el.offsetWidth, el.offsetHeight) * 1.6;\n        const d = Math.hypot(x - cx, y - cy);\n        const s = d > max ? max / d : 1;\n        go([cx + (x - cx) * s, cy + (y - cy) * s]);\n      },\n      release: () => {\n        dragging = false;\n        settle();\n      },\n      toggle: () => {\n        open = !open;\n        settle();\n      },\n    };\n\n    const ro = new ResizeObserver(() => {\n      p = target = inset(restPeel);\n      render();\n    });\n    ro.observe(el);\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      api.current = null;\n    };\n  }, [corner, restPeel, hoverPeel]);\n\n  const local = (e: PointerEvent) => {\n    const r = root.current!.getBoundingClientRect();\n    return [e.clientX - r.left, e.clientY - r.top] as const;\n  };\n\n  return (\n    <div\n      ref={root}\n      role=\"button\"\n      aria-label={label}\n      tabIndex={0}\n      className={cn(\n        \"relative inline-block cursor-grab touch-none select-none outline-none focus-visible:ring-2 focus-visible:ring-ring active:cursor-grabbing\",\n        className,\n      )}\n      style={{ borderRadius: radius }}\n      onPointerEnter={() => api.current?.hover(true)}\n      onPointerLeave={() => api.current?.hover(false)}\n      onPointerDown={(e) => {\n        e.currentTarget.setPointerCapture(e.pointerId);\n        api.current?.drag(...local(e));\n      }}\n      onPointerMove={(e) => e.buttons & 1 && api.current?.drag(...local(e))}\n      onPointerUp={() => api.current?.release()}\n      onPointerCancel={() => api.current?.release()}\n      onFocus={() => api.current?.hover(true)}\n      onBlur={() => api.current?.hover(false)}\n      onKeyDown={(e) => {\n        if (e.key === \"Enter\" || e.key === \" \") {\n          e.preventDefault();\n          api.current?.toggle();\n        }\n      }}\n    >\n      <div style={{ filter: \"drop-shadow(0 6px 10px rgba(0,0,0,.35))\" }}>\n        <div ref={face}>{children}</div>\n      </div>\n      <div aria-hidden className=\"pointer-events-none absolute inset-0\" style={{ filter: \"drop-shadow(0 10px 14px rgba(0,0,0,.4))\" }}>\n        <div\n          ref={flap}\n          className=\"absolute inset-0\"\n          style={{ borderRadius: radius, backgroundColor: backColor, transformOrigin: \"0 0\", clipPath: \"polygon(0 0)\" }}\n        />\n      </div>\n    </div>\n  );\n}\n"}]}