{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"reel","type":"registry:ui","title":"Reel","description":"A vertical short-video reel that snaps clip to clip on scroll or drag, with story-style progress bars, slow Ken Burns motion, hold-to-pause and double-tap likes.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/reel.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, useState, type KeyboardEvent, type MouseEvent, type PointerEvent } from \"react\";\nimport { AnimatePresence, motion } from \"motion/react\";\nimport { Heart, MessageCircle, Music2, Pause, Send } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface ReelItem {\n  /** Poster image for the clip. */\n  src: string;\n  author: string;\n  avatar?: string;\n  caption: string;\n  /** Soundtrack line, e.g. \"Original audio · nova\". */\n  audio?: string;\n  likes?: number;\n  comments?: number;\n}\n\nexport interface ReelProps {\n  items: ReelItem[];\n  /** Ms each clip plays before advancing. */\n  duration?: number;\n  /** Advance automatically when a clip's progress bar fills. */\n  autoPlay?: boolean;\n  /** Jump back to the first clip after the last one. */\n  loop?: boolean;\n  /** Height in px; width follows a 9:16 frame. */\n  height?: number;\n  className?: string;\n}\n\nconst css = `\n@keyframes reel-fill{from{transform:scaleX(0)}to{transform:scaleX(1)}}\n@keyframes reel-zoom{from{transform:scale(1.02)}to{transform:scale(1.16) translateY(-2%)}}\n.reel-fill{transform-origin:left;animation:reel-fill var(--reel-d) linear forwards}\n.reel-card[data-active] .reel-img{animation:reel-zoom var(--reel-d) linear forwards}\n.reel-root[data-paused] .reel-fill,.reel-root[data-paused] .reel-img{animation-play-state:paused!important}\n@media (prefers-reduced-motion:reduce){.reel-card[data-active] .reel-img{animation:none}}\n`;\n\nconst compact = (v: number) =>\n  v >= 1e6 ? `${(v / 1e6).toFixed(1).replace(/\\.0$/, \"\")}M` : v >= 1e3 ? `${(v / 1e3).toFixed(1).replace(/\\.0$/, \"\")}K` : String(v);\n\nexport function Reel({ items, duration = 6000, autoPlay = true, loop = true, height = 440, className }: ReelProps) {\n  const root = useRef<HTMLDivElement>(null);\n  const scroller = useRef<HTMLDivElement>(null);\n  const drag = useRef<{ y: number; top: number; index: number; moved: boolean } | null>(null);\n  const [active, setActive] = useState(0);\n  const [cycle, setCycle] = useState(0);\n  const [holding, setHolding] = useState(false);\n  const [manualPause, setManualPause] = useState(false);\n  const [visible, setVisible] = useState(true);\n  const [liked, setLiked] = useState<Set<number>>(() => new Set());\n  const [burst, setBurst] = useState<{ id: number; x: number; y: number } | null>(null);\n  const n = items.length;\n  const width = Math.round((height * 9) / 16);\n  const paused = holding || manualPause || !visible;\n\n  useEffect(() => {\n    const el = scroller.current;\n    const box = root.current;\n    if (!el || !box) return;\n    const cards = Array.from(el.children) as HTMLElement[];\n    const io = new IntersectionObserver(\n      (entries) => {\n        for (const e of entries) if (e.isIntersecting) setActive(cards.indexOf(e.target as HTMLElement));\n      },\n      { root: el, threshold: 0.6 },\n    );\n    cards.forEach((c) => io.observe(c));\n    const vis = new IntersectionObserver(([e]) => setVisible(e.isIntersecting));\n    vis.observe(box);\n    return () => {\n      io.disconnect();\n      vis.disconnect();\n    };\n  }, [n]);\n\n  const go = (i: number) => {\n    const el = scroller.current;\n    if (!el) return;\n    const smooth = !window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    el.scrollTo({ top: Math.max(0, Math.min(n - 1, i)) * el.clientHeight, behavior: smooth ? \"smooth\" : \"auto\" });\n    setCycle((c) => c + 1);\n  };\n  const next = () => {\n    if (active < n - 1) go(active + 1);\n    else if (loop) go(0);\n  };\n\n  const toggleLike = (i: number, force?: boolean) =>\n    setLiked((s) => {\n      const out = new Set(s);\n      if (force ?? !out.has(i)) out.add(i);\n      else out.delete(i);\n      return out;\n    });\n\n  const onKey = (e: KeyboardEvent) => {\n    if (e.key === \"ArrowDown\" || e.key === \"PageDown\") go(active + 1);\n    else if (e.key === \"ArrowUp\" || e.key === \"PageUp\") go(active - 1);\n    else if (e.key === \" \") setManualPause((p) => !p);\n    else if (e.key.toLowerCase() === \"l\") toggleLike(active);\n    else return;\n    e.preventDefault();\n  };\n\n  // Press and hold pauses; a mouse drag flicks between clips (touch scrolls natively).\n  const onDown = (e: PointerEvent<HTMLDivElement>) => {\n    if (e.button !== 0) return;\n    setHolding(true);\n    if (e.pointerType === \"mouse\") drag.current = { y: e.clientY, top: e.currentTarget.scrollTop, index: active, moved: false };\n  };\n  const onMove = (e: PointerEvent<HTMLDivElement>) => {\n    const d = drag.current;\n    if (!d) return;\n    const dy = e.clientY - d.y;\n    if (!d.moved && Math.abs(dy) > 5) {\n      d.moved = true;\n      e.currentTarget.setPointerCapture(e.pointerId);\n      e.currentTarget.style.scrollSnapType = \"none\";\n    }\n    if (d.moved) e.currentTarget.scrollTop = d.top - dy;\n  };\n  const onUp = (e: PointerEvent<HTMLDivElement>) => {\n    setHolding(false);\n    const d = drag.current;\n    drag.current = null;\n    if (!d?.moved) return;\n    e.currentTarget.style.scrollSnapType = \"\";\n    const dy = e.clientY - d.y;\n    go(Math.abs(dy) > 50 ? d.index - Math.sign(dy) : d.index);\n  };\n\n  const onDouble = (e: MouseEvent<HTMLElement>, i: number) => {\n    const r = e.currentTarget.getBoundingClientRect();\n    toggleLike(i, true);\n    setBurst({ id: e.timeStamp, x: e.clientX - r.left, y: e.clientY - r.top });\n  };\n\n  return (\n    <div\n      ref={root}\n      data-paused={paused || undefined}\n      className={cn(\"reel-root relative overflow-hidden rounded-[28px] border border-border bg-black shadow-[0_40px_90px_-30px_rgba(0,0,0,.8)]\", className)}\n      style={{ width, height, [\"--reel-d\" as string]: `${duration}ms` }}\n    >\n      <style href=\"reel\" precedence=\"default\">\n        {css}\n      </style>\n\n      <div\n        ref={scroller}\n        role=\"region\"\n        aria-roledescription=\"carousel\"\n        aria-label=\"Short videos. Arrow keys to switch, space to pause, L to like.\"\n        tabIndex={0}\n        onKeyDown={onKey}\n        onPointerDown={onDown}\n        onPointerMove={onMove}\n        onPointerUp={onUp}\n        onPointerCancel={onUp}\n        onPointerLeave={() => !drag.current && setHolding(false)}\n        className=\"h-full snap-y snap-mandatory overflow-y-auto overscroll-contain outline-none [scrollbar-width:none] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring [&::-webkit-scrollbar]:hidden\"\n      >\n        {items.map((it, i) => {\n          const isLiked = liked.has(i);\n          return (\n            <article\n              key={it.src + i}\n              data-active={i === active || undefined}\n              aria-roledescription=\"slide\"\n              aria-label={`${i + 1} of ${n}: ${it.author}`}\n              onDoubleClick={(e) => onDouble(e, i)}\n              className=\"reel-card relative h-full snap-start snap-always overflow-hidden text-white\"\n            >\n              {/* eslint-disable-next-line @next/next/no-img-element */}\n              <img src={it.src} alt=\"\" draggable={false} className=\"reel-img absolute inset-0 h-full w-full select-none object-cover\" />\n              <div aria-hidden className=\"absolute inset-0 bg-gradient-to-b from-black/45 via-transparent to-black/80\" />\n\n              <div className=\"absolute bottom-16 right-2.5 flex flex-col items-center gap-3.5 text-[11px] font-medium\">\n                <button\n                  type=\"button\"\n                  aria-label={isLiked ? \"Unlike\" : \"Like\"}\n                  aria-pressed={isLiked}\n                  onPointerDown={(e) => e.stopPropagation()}\n                  onClick={() => toggleLike(i)}\n                  className=\"flex flex-col items-center gap-0.5 rounded-full outline-none focus-visible:ring-2 focus-visible:ring-white\"\n                >\n                  <motion.span animate={isLiked ? { scale: [1, 1.35, 1] } : { scale: 1 }} transition={{ duration: 0.35 }}>\n                    <Heart className={cn(\"size-6 drop-shadow\", isLiked && \"fill-rose-500 text-rose-500\")} />\n                  </motion.span>\n                  {compact((it.likes ?? 0) + (isLiked ? 1 : 0))}\n                </button>\n                <span className=\"flex flex-col items-center gap-0.5\">\n                  <MessageCircle className=\"size-6 drop-shadow\" aria-hidden />\n                  <span className=\"sr-only\">Comments:</span>\n                  {compact(it.comments ?? 0)}\n                </span>\n                <Send className=\"size-5 drop-shadow\" aria-hidden />\n              </div>\n\n              <div className=\"absolute inset-x-0 bottom-0 p-3.5 pr-12\">\n                <div className=\"flex items-center gap-2\">\n                  {it.avatar && (\n                    // eslint-disable-next-line @next/next/no-img-element\n                    <img src={it.avatar} alt=\"\" draggable={false} className=\"size-7 rounded-full object-cover ring-2 ring-white/80\" />\n                  )}\n                  <span className=\"text-[13px] font-semibold\">{it.author}</span>\n                  <span className=\"rounded-md border border-white/50 px-1.5 py-px text-[10px] font-semibold\">Follow</span>\n                </div>\n                <p className=\"mt-2 line-clamp-2 text-[12.5px] leading-snug text-white/90\">{it.caption}</p>\n                {it.audio && (\n                  <p className=\"mt-1.5 flex items-center gap-1.5 overflow-hidden text-[11px] text-white/80\">\n                    <Music2 className=\"size-3 shrink-0\" aria-hidden />\n                    <span className=\"truncate\">{it.audio}</span>\n                  </p>\n                )}\n              </div>\n            </article>\n          );\n        })}\n      </div>\n\n      {/* Story-style progress bars. */}\n      <div aria-hidden className=\"pointer-events-none absolute inset-x-3 top-3 flex gap-1\">\n        {items.map((it, i) => (\n          <span key={it.src + i} className=\"h-[3px] flex-1 overflow-hidden rounded-full bg-white/25\">\n            {i < active || (i === active && !autoPlay) ? (\n              <span className=\"block h-full w-full bg-white\" />\n            ) : i === active ? (\n              <span key={cycle} className=\"reel-fill block h-full w-full bg-white\" onAnimationEnd={next} />\n            ) : null}\n          </span>\n        ))}\n      </div>\n      <p aria-hidden className=\"pointer-events-none absolute left-3.5 top-6 text-sm font-semibold text-white drop-shadow\">\n        Reels\n      </p>\n\n      <AnimatePresence>\n        {manualPause && (\n          <motion.span\n            key=\"pause\"\n            initial={{ opacity: 0, scale: 0.8 }}\n            animate={{ opacity: 1, scale: 1 }}\n            exit={{ opacity: 0, scale: 1.2 }}\n            className=\"pointer-events-none absolute left-1/2 top-1/2 grid size-14 -translate-x-1/2 -translate-y-1/2 place-items-center rounded-full bg-black/40 text-white backdrop-blur\"\n          >\n            <Pause className=\"size-6 fill-white\" aria-hidden />\n          </motion.span>\n        )}\n        {burst && (\n          <motion.span\n            key={burst.id}\n            initial={{ opacity: 0, scale: 0.3, rotate: -15 }}\n            animate={{ opacity: [0, 1, 1, 0], scale: [0.3, 1.2, 1, 1.4], rotate: [-15, 0, 0, 0], y: [0, 0, 0, -40] }}\n            transition={{ duration: 0.9, times: [0, 0.25, 0.6, 1] }}\n            onAnimationComplete={() => setBurst(null)}\n            className=\"pointer-events-none absolute -ml-10 -mt-10\"\n            style={{ left: burst.x, top: burst.y }}\n          >\n            <Heart className=\"size-20 fill-rose-500 text-rose-500 drop-shadow-[0_6px_20px_rgba(244,63,94,.6)]\" aria-hidden />\n          </motion.span>\n        )}\n      </AnimatePresence>\n    </div>\n  );\n}\n"}]}