{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"draggable-avatar","type":"registry:ui","title":"Draggable Avatar","description":"An avatar you can drag and fling around its container; it springs to the nearest corner with the throw's momentum.","dependencies":["motion"],"registryDependencies":[],"files":[{"path":"registry/ui/draggable-avatar.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, useState, type KeyboardEvent } from \"react\";\nimport { animate, motion, useMotionValue, useReducedMotion, type PanInfo } from \"motion/react\";\n\ntype Corner = \"top-left\" | \"top-right\" | \"bottom-left\" | \"bottom-right\";\n\nexport interface DraggableAvatarProps {\n  src: string;\n  /** Accessible name, e.g. the person's name. */\n  alt?: string;\n  /** Avatar size in px. */\n  size?: number;\n  /** Corner it starts in (remount with a new `key` to reset). */\n  corner?: Corner;\n  /** Px gap between the avatar and the container edge. */\n  padding?: number;\n  /** Circle or rounded square. */\n  shape?: \"circle\" | \"rounded\";\n  /** Spring stiffness of the snap. */\n  stiffness?: number;\n  /** Spring damping of the snap. Lower is bouncier. */\n  damping?: number;\n  /** Green presence dot. */\n  status?: boolean;\n  onCornerChange?: (corner: Corner) => void;\n  className?: string;\n}\n\nconst LABEL: Record<Corner, string> = {\n  \"top-left\": \"top left\",\n  \"top-right\": \"top right\",\n  \"bottom-left\": \"bottom left\",\n  \"bottom-right\": \"bottom right\",\n};\n\n/**\n * Drop inside any `position: relative` container. The avatar can be dragged and\n * flung anywhere in it and springs to the nearest corner, carrying its velocity.\n */\nexport function DraggableAvatar({\n  src,\n  alt = \"Avatar\",\n  size = 88,\n  corner: initial = \"bottom-right\",\n  padding = 16,\n  shape = \"circle\",\n  stiffness = 380,\n  damping = 26,\n  status = true,\n  onCornerChange,\n  className,\n}: DraggableAvatarProps) {\n  const area = useRef<HTMLDivElement>(null);\n  const [corner, setCorner] = useState<Corner>(initial);\n  const cornerRef = useRef(corner);\n  const [dragging, setDragging] = useState(false);\n  const x = useMotionValue(0);\n  const y = useMotionValue(0);\n  // Hidden until measured, so SSR never flashes it in the wrong corner.\n  const opacity = useMotionValue(0);\n  const reduced = useReducedMotion();\n\n  const target = (c: Corner) => {\n    const el = area.current;\n    const w = el?.clientWidth ?? 0;\n    const h = el?.clientHeight ?? 0;\n    return {\n      x: c.endsWith(\"left\") ? padding : Math.max(padding, w - size - padding),\n      y: c.startsWith(\"top\") ? padding : Math.max(padding, h - size - padding),\n    };\n  };\n\n  const snap = (c: Corner, vx = 0, vy = 0) => {\n    const t = target(c);\n    const spring = reduced ? { duration: 0 } : { type: \"spring\" as const, stiffness, damping };\n    animate(x, t.x, { ...spring, velocity: vx });\n    animate(y, t.y, { ...spring, velocity: vy });\n  };\n\n  const move = (c: Corner) => {\n    cornerRef.current = c;\n    setCorner(c);\n    snap(c);\n    onCornerChange?.(c);\n  };\n\n  // Place instantly on mount, resize and size changes; real moves animate via snap().\n  useEffect(() => {\n    const el = area.current;\n    if (!el) return;\n    const place = () => {\n      const t = target(cornerRef.current);\n      x.jump(t.x);\n      y.jump(t.y);\n    };\n    place();\n    opacity.set(1);\n    const ro = new ResizeObserver(place);\n    ro.observe(el);\n    return () => ro.disconnect();\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [size, padding]);\n\n  const onDragEnd = (_: unknown, info: PanInfo) => {\n    setDragging(false);\n    const el = area.current;\n    if (!el) return;\n    // Project where the fling would coast to, then pick the closest corner.\n    const px = x.get() + info.velocity.x * 0.2 + size / 2;\n    const py = y.get() + info.velocity.y * 0.2 + size / 2;\n    const c = `${py < el.clientHeight / 2 ? \"top\" : \"bottom\"}-${px < el.clientWidth / 2 ? \"left\" : \"right\"}` as Corner;\n    cornerRef.current = c;\n    setCorner(c);\n    snap(c, info.velocity.x, info.velocity.y);\n    if (c !== corner) onCornerChange?.(c);\n  };\n\n  const onKey = (e: KeyboardEvent) => {\n    const [v, h] = corner.split(\"-\");\n    let next: Corner | null = null;\n    if (e.key === \"ArrowLeft\") next = `${v}-left` as Corner;\n    else if (e.key === \"ArrowRight\") next = `${v}-right` as Corner;\n    else if (e.key === \"ArrowUp\") next = `top-${h}` as Corner;\n    else if (e.key === \"ArrowDown\") next = `bottom-${h}` as Corner;\n    if (!next) return;\n    e.preventDefault();\n    if (next !== corner) move(next);\n  };\n\n  const round = shape === \"circle\" ? \"9999px\" : `${Math.round(size * 0.28)}px`;\n\n  return (\n    <div ref={area} className={`pointer-events-none absolute inset-0 ${className ?? \"\"}`}>\n      <motion.button\n        type=\"button\"\n        aria-label={`${alt}. In the ${LABEL[corner]} corner. Use arrow keys to move.`}\n        drag\n        dragMomentum={false}\n        dragConstraints={area}\n        dragElastic={0.18}\n        onDragStart={() => setDragging(true)}\n        onDragEnd={onDragEnd}\n        onKeyDown={onKey}\n        whileHover={reduced ? undefined : { scale: 1.04 }}\n        whileDrag={{ scale: 1.1, cursor: \"grabbing\" }}\n        className=\"pointer-events-auto absolute left-0 top-0 cursor-grab touch-none outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-4 focus-visible:ring-offset-background\"\n        style={{ x, y, width: size, height: size, borderRadius: round, opacity }}\n      >\n        <span\n          className=\"block size-full overflow-hidden border-2 border-white/80 bg-muted transition-shadow duration-300\"\n          style={{\n            borderRadius: round,\n            boxShadow: dragging\n              ? \"0 30px 60px -12px rgba(0,0,0,.6), 0 12px 24px -8px rgba(0,0,0,.4)\"\n              : \"0 14px 30px -10px rgba(0,0,0,.55), 0 4px 10px -4px rgba(0,0,0,.3)\",\n          }}\n        >\n          {/* eslint-disable-next-line @next/next/no-img-element */}\n          <img src={src} alt=\"\" draggable={false} className=\"size-full select-none object-cover\" />\n        </span>\n        {status && (\n          <span\n            aria-hidden\n            className=\"absolute rounded-full border-2 border-background bg-emerald-400\"\n            style={{\n              width: Math.max(10, size * 0.2),\n              height: Math.max(10, size * 0.2),\n              right: shape === \"circle\" ? size * 0.04 : -2,\n              bottom: shape === \"circle\" ? size * 0.04 : -2,\n            }}\n          />\n        )}\n      </motion.button>\n    </div>\n  );\n}\n"}]}