{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "peel-off-sticker",
  "title": "Peel-off Sticker",
  "description": "A sticker you can actually peel: grab a corner and it lifts along a fold that follows your pointer, showing the paper backing in the sticker's own shape with a soft shadow; let go and it settles back, or pull far enough and it comes off and sticks itself back on.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/peel-off-sticker/peel-off-sticker.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type PeelCorner = \"bottom-right\" | \"bottom-left\" | \"top-right\" | \"top-left\";\n\nexport interface PeelOffStickerProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  children: React.ReactNode;\n  /** The corner you grab to peel. */\n  corner?: PeelCorner;\n  /** How far the corner lifts at rest, as a hint that it can be peeled, in px. */\n  curl?: number;\n  /** Peel past this share of the sticker and it comes off completely (0–1). 1 = never comes off. */\n  removeAt?: number;\n  /** After coming off, stick back on after this many ms (0 = stay off). */\n  restickAfter?: number;\n  /** Accessible name for the sticker. */\n  label?: string;\n  onPeelOff?: () => void;\n}\n\ntype Pt = { x: number; y: number };\n\n/** Clip a polygon to the half-plane dot(p - m, n) >= 0 (Sutherland–Hodgman, one edge). */\nfunction clip(poly: Pt[], m: Pt, n: Pt, keepPositive: boolean): Pt[] {\n  const side = (p: Pt) => ((p.x - m.x) * n.x + (p.y - m.y) * n.y) * (keepPositive ? 1 : -1);\n  const out: Pt[] = [];\n  for (let i = 0; i < poly.length; i++) {\n    const a = poly[i];\n    const b = poly[(i + 1) % poly.length];\n    const sa = side(a);\n    const sb = side(b);\n    if (sa >= 0) out.push(a);\n    if (sa >= 0 !== sb >= 0) {\n      const t = sa / (sa - sb);\n      out.push({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t });\n    }\n  }\n  return out;\n}\n\nconst area = (poly: Pt[]) => Math.abs(poly.reduce((s, p, i) => s + p.x * poly[(i + 1) % poly.length].y - poly[(i + 1) % poly.length].x * p.y, 0)) / 2;\nconst toPath = (poly: Pt[]) => (poly.length > 2 ? `polygon(${poly.map((p) => `${p.x.toFixed(2)}px ${p.y.toFixed(2)}px`).join(\", \")})` : \"polygon(0 0, 0 0, 0 0)\");\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\nexport function PeelOffSticker({ children, corner = \"bottom-right\", curl = 24, removeAt = 0.5, restickAfter = 1400, label = \"Sticker\", onPeelOff, className, style, ...props }: PeelOffStickerProps) {\n  const reduce = useReducedMotion();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const frontRef = React.useRef<HTMLDivElement>(null);\n  const flapRef = React.useRef<HTMLDivElement>(null);\n  const [gone, setGone] = React.useState(false);\n  const s = React.useRef({ w: 0, h: 0, p: { x: 0, y: 0 } as Pt, goal: { x: 0, y: 0 } as Pt, dragging: false, raf: 0 });\n\n  const cornerPt = React.useCallback(\n    (w: number, h: number): Pt => ({ x: corner.includes(\"right\") ? w : 0, y: corner.includes(\"bottom\") ? h : 0 }),\n    [corner],\n  );\n  // At rest the corner is lifted a little along the diagonal, so it reads as peelable.\n  const restPt = React.useCallback(\n    (w: number, h: number): Pt => {\n      const c = cornerPt(w, h);\n      const k = reduce ? 0 : curl;\n      return { x: c.x + (c.x ? -k : k), y: c.y + (c.y ? -k : k) };\n    },\n    [cornerPt, curl, reduce],\n  );\n\n  const render = React.useCallback(() => {\n    const st = s.current;\n    const { w, h, p } = st;\n    if (!w || !frontRef.current || !flapRef.current) return 0;\n    const c = cornerPt(w, h);\n    const dx = p.x - c.x;\n    const dy = p.y - c.y;\n    const len = Math.hypot(dx, dy);\n    const rect: Pt[] = [\n      { x: 0, y: 0 },\n      { x: w, y: 0 },\n      { x: w, y: h },\n      { x: 0, y: h },\n    ];\n    if (len < 0.5) {\n      frontRef.current.style.clipPath = \"none\";\n      flapRef.current.style.clipPath = \"polygon(0 0, 0 0, 0 0)\";\n      return 0;\n    }\n    // The fold is the perpendicular bisector of the corner and where it's being pulled to.\n    const n = { x: dx / len, y: dy / len };\n    const m = { x: (c.x + p.x) / 2, y: (c.y + p.y) / 2 };\n    const front = clip(rect, m, n, true);\n    const peeled = clip(rect, m, n, false);\n    // The peeled part folds over: reflect it across the fold line.\n    frontRef.current.style.clipPath = toPath(front);\n    // The flap is a copy of the sticker clipped to the peeled part, then mirrored across the fold line\n    // (x' = x − 2((x − m)·n)n), so it always has the sticker's real shape, round or tilted.\n    const t = 2 * (m.x * n.x + m.y * n.y);\n    flapRef.current.style.clipPath = toPath(peeled);\n    flapRef.current.style.transform = `matrix(${1 - 2 * n.x * n.x}, ${-2 * n.x * n.y}, ${-2 * n.x * n.y}, ${1 - 2 * n.y * n.y}, ${t * n.x}, ${t * n.y})`;\n    return area(peeled) / (w * h);\n  }, [cornerPt]);\n\n  const animateTo = React.useCallback(\n    (goal: Pt, speed = 0.2, done?: () => void) => {\n      const st = s.current;\n      st.goal = goal;\n      cancelAnimationFrame(st.raf);\n      const tick = () => {\n        st.p.x += (st.goal.x - st.p.x) * speed;\n        st.p.y += (st.goal.y - st.p.y) * speed;\n        render();\n        if (Math.hypot(st.goal.x - st.p.x, st.goal.y - st.p.y) > 0.4) st.raf = requestAnimationFrame(tick);\n        else {\n          st.p = { ...st.goal };\n          render();\n          done?.();\n        }\n      };\n      st.raf = requestAnimationFrame(tick);\n    },\n    [render],\n  );\n\n  React.useEffect(() => {\n    const el = rootRef.current;\n    if (!el) return;\n    const st = s.current;\n    const ro = new ResizeObserver(() => {\n      st.w = el.offsetWidth;\n      st.h = el.offsetHeight;\n      if (!st.dragging) st.p = restPt(st.w, st.h);\n      render();\n    });\n    ro.observe(el);\n    return () => {\n      ro.disconnect();\n      cancelAnimationFrame(st.raf);\n    };\n  }, [restPt, render]);\n\n  const local = (event: React.PointerEvent): Pt => {\n    const r = rootRef.current!.getBoundingClientRect();\n    const scale = r.width / rootRef.current!.offsetWidth || 1;\n    return { x: (event.clientX - r.left) / scale, y: (event.clientY - r.top) / scale };\n  };\n\n  const release = () => {\n    const st = s.current;\n    if (!st.dragging) return;\n    st.dragging = false;\n    const share = render();\n    if (share >= removeAt && removeAt < 1) {\n      // Off it comes: the corner carries on past the far side, then the sticker fades.\n      const c = cornerPt(st.w, st.h);\n      animateTo({ x: st.w - c.x + (st.w - c.x ? 1 : -1) * st.w * 0.6, y: st.h - c.y + (st.h - c.y ? 1 : -1) * st.h * 0.6 }, 0.16, () => {\n        setGone(true);\n        onPeelOff?.();\n        if (restickAfter > 0)\n          window.setTimeout(() => {\n            st.p = cornerPt(st.w, st.h);\n            render();\n            setGone(false);\n            animateTo(restPt(st.w, st.h), 0.12);\n          }, restickAfter);\n      });\n    } else animateTo(restPt(st.w, st.h), 0.18);\n  };\n\n  return (\n    <div\n      ref={rootRef}\n      role=\"img\"\n      aria-label={label}\n      className={cn(\"relative inline-block touch-none select-none\", className)}\n      style={{ transition: \"opacity 400ms, transform 400ms\", opacity: gone ? 0 : 1, transform: gone ? \"translateY(-12px) scale(0.96)\" : \"none\", ...style }}\n      onPointerDown={(event) => {\n        if (gone || reduce || event.button !== 0) return;\n        event.currentTarget.setPointerCapture(event.pointerId);\n        cancelAnimationFrame(s.current.raf);\n        s.current.dragging = true;\n        s.current.p = local(event);\n        render();\n      }}\n      onPointerMove={(event) => {\n        if (!s.current.dragging) return;\n        s.current.p = local(event);\n        render();\n      }}\n      onPointerUp={release}\n      onPointerCancel={release}\n      {...props}\n    >\n      <div ref={frontRef} className=\"relative cursor-grab active:cursor-grabbing\">\n        {children}\n      </div>\n      {/* The folded flap: the sticker's silhouette in the backing colour, casting a soft shadow. */}\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ filter: \"drop-shadow(0 6px 8px rgba(0,0,0,.35))\" }}>\n        <div ref={flapRef} inert className=\"absolute inset-0 origin-top-left\" style={{ clipPath: \"polygon(0 0, 0 0, 0 0)\" }}>\n          {/* Every pixel of the sticker turned to the warm white of its paper backing. */}\n          <div style={{ filter: \"brightness(0) invert(0.95) sepia(0.12)\" }}>{children}</div>\n        </div>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}