{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"animated-list","type":"registry:ui","title":"Animated List","description":"A scrollable list whose rows pop in as they appear, with keyboard selection and edge fades.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/animated-list.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { motion, useReducedMotion } from \"motion/react\";\nimport { useCallback, useEffect, useId, useRef, useState, type KeyboardEvent } from \"react\";\n\nexport type AnimatedListItem = {\n  id: string;\n  label: string;\n  description?: string;\n  /** Short text on the right, such as a count or a date. */\n  meta?: string;\n};\n\ntype AnimatedListProps = {\n  /** Strings or { id, label, description?, meta? } objects. */\n  items: (string | AnimatedListItem)[];\n  onSelect?: (item: AnimatedListItem, index: number) => void;\n  /** Selected id, for controlled use. */\n  selectedId?: string | null;\n  defaultSelectedId?: string;\n  /** Fade the top and bottom edges while there is more to scroll. */\n  showGradients?: boolean;\n  /** Show the scrollbar. */\n  displayScrollbar?: boolean;\n  /** Maximum height of the scroll area in pixels. */\n  maxHeight?: number;\n  /** Color the edge fades blend into. Match it to the list background. */\n  fadeColor?: string;\n  /** Accessible name of the list. */\n  label?: string;\n  className?: string;\n};\n\nconst normalize = (item: string | AnimatedListItem, i: number): AnimatedListItem =>\n  typeof item === \"string\" ? { id: `${i}-${item}`, label: item } : item;\n\n/** A scrollable list whose rows pop in as they appear, with keyboard selection and edge fades. */\nexport function AnimatedList({\n  items,\n  onSelect,\n  selectedId,\n  defaultSelectedId,\n  showGradients = true,\n  displayScrollbar = true,\n  maxHeight = 360,\n  fadeColor = \"#0c0a09\",\n  label = \"Items\",\n  className = \"\",\n}: AnimatedListProps) {\n  const uid = useId();\n  const reduce = useReducedMotion();\n  const listRef = useRef<HTMLDivElement>(null);\n  const contentRef = useRef<HTMLUListElement>(null);\n  const [active, setActive] = useState(-1);\n  const [internal, setInternal] = useState<string | null>(defaultSelectedId ?? null);\n  const [edges, setEdges] = useState({ top: false, bottom: false });\n  const rows = items.map(normalize);\n  const selected = selectedId === undefined ? internal : selectedId;\n  const optionId = (i: number) => `${uid}-option-${i}`;\n\n  const measure = useCallback(() => {\n    const el = listRef.current;\n    if (!el) return;\n    const top = el.scrollTop > 2;\n    const bottom = el.scrollTop + el.clientHeight < el.scrollHeight - 2;\n    setEdges((e) => (e.top === top && e.bottom === bottom ? e : { top, bottom }));\n  }, []);\n\n  // Recheck the fades when the list or its content changes size.\n  useEffect(() => {\n    const el = listRef.current;\n    const content = contentRef.current;\n    if (!el || !content) return;\n    const observer = new ResizeObserver(measure);\n    observer.observe(el);\n    observer.observe(content);\n    return () => observer.disconnect();\n  }, [measure]);\n\n  /** Scroll the container, never the page, just enough to show the row. */\n  function reveal(index: number) {\n    const box = listRef.current;\n    const el = document.getElementById(optionId(index));\n    if (!box || !el) return;\n    const pad = 8;\n    const behavior = reduce ? \"auto\" : \"smooth\";\n    if (el.offsetTop - pad < box.scrollTop) box.scrollTo({ top: el.offsetTop - pad, behavior });\n    else if (el.offsetTop + el.offsetHeight + pad > box.scrollTop + box.clientHeight) {\n      box.scrollTo({ top: el.offsetTop + el.offsetHeight + pad - box.clientHeight, behavior });\n    }\n  }\n\n  function move(index: number) {\n    const next = Math.max(0, Math.min(rows.length - 1, index));\n    setActive(next);\n    reveal(next);\n  }\n\n  function choose(index: number) {\n    const row = rows[index];\n    if (!row) return;\n    setActive(index);\n    if (selectedId === undefined) setInternal(row.id);\n    onSelect?.(row, index);\n  }\n\n  function onKeyDown(e: KeyboardEvent<HTMLDivElement>) {\n    if (rows.length === 0) return;\n    const from = active < 0 ? Math.max(0, rows.findIndex((r) => r.id === selected)) : active;\n    const keys: Record<string, () => void> = {\n      ArrowDown: () => move(active < 0 ? from : from + 1),\n      ArrowUp: () => move(active < 0 ? from : from - 1),\n      PageDown: () => move(from + 5),\n      PageUp: () => move(from - 5),\n      Home: () => move(0),\n      End: () => move(rows.length - 1),\n      Enter: () => choose(from),\n      \" \": () => choose(from),\n    };\n    const run = keys[e.key];\n    if (!run) return;\n    e.preventDefault();\n    run();\n  }\n\n  const scrollbar = displayScrollbar ? \"thin-scroll\" : \"[scrollbar-width:none] [&::-webkit-scrollbar]:hidden\";\n\n  return (\n    <div className={`relative overflow-hidden rounded-3xl border border-ink-600 bg-ink-900 ${className}`}>\n      <div\n        ref={listRef}\n        role=\"listbox\"\n        aria-label={label}\n        aria-activedescendant={active >= 0 ? optionId(active) : undefined}\n        tabIndex={0}\n        onKeyDown={onKeyDown}\n        onScroll={measure}\n        onPointerLeave={() => setActive(-1)}\n        className={`relative overflow-y-auto p-2 focus-visible:-outline-offset-2 ${scrollbar}`}\n        style={{ maxHeight }}\n      >\n        <ul ref={contentRef} role=\"presentation\" className=\"space-y-1.5\">\n          {rows.map((row, i) => {\n            const isSelected = row.id === selected;\n            const isActive = i === active;\n            return (\n              <motion.li\n                key={row.id}\n                id={optionId(i)}\n                role=\"option\"\n                aria-selected={isSelected}\n                onPointerMove={() => i !== active && setActive(i)}\n                onClick={() => choose(i)}\n                initial={reduce ? false : { opacity: 0, scale: 0.86, y: 8 }}\n                whileInView={{ opacity: 1, scale: 1, y: 0 }}\n                viewport={{ root: listRef, amount: 0.35 }}\n                transition={{ type: \"spring\", stiffness: 420, damping: 30 }}\n                className={`relative flex cursor-pointer items-center gap-3 rounded-2xl border px-4 py-3 transition-colors ${\n                  isSelected ? \"border-ember/60 bg-ember/10\" : isActive ? \"border-ink-500 bg-ink-700\" : \"border-ink-700 bg-ink-850\"\n                }`}\n              >\n                <span className=\"min-w-0 flex-1\">\n                  <span className={`block truncate text-sm font-semibold ${isSelected || isActive ? \"text-bone\" : \"text-bone-muted\"}`}>\n                    {row.label}\n                  </span>\n                  {row.description ? <span className=\"mt-0.5 block truncate text-xs text-bone-dim\">{row.description}</span> : null}\n                </span>\n                {row.meta ? <span className=\"shrink-0 text-xs tabular-nums text-bone-dim\">{row.meta}</span> : null}\n                {isSelected ? (\n                  <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" className=\"size-4 shrink-0 text-ember\" aria-hidden=\"true\">\n                    <path d=\"m3.5 8.5 3 3 6-7\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n                  </svg>\n                ) : null}\n              </motion.li>\n            );\n          })}\n        </ul>\n      </div>\n      {showGradients ? (\n        <>\n          <motion.div\n            aria-hidden=\"true\"\n            className=\"pointer-events-none absolute inset-x-0 top-0 h-14\"\n            style={{ background: `linear-gradient(to bottom, ${fadeColor}, transparent)` }}\n            initial={false}\n            animate={{ opacity: edges.top ? 1 : 0 }}\n            transition={{ duration: 0.2 }}\n          />\n          <motion.div\n            aria-hidden=\"true\"\n            className=\"pointer-events-none absolute inset-x-0 bottom-0 h-20\"\n            style={{ background: `linear-gradient(to top, ${fadeColor}, transparent)` }}\n            initial={false}\n            animate={{ opacity: edges.bottom ? 1 : 0 }}\n            transition={{ duration: 0.2 }}\n          />\n        </>\n      ) : null}\n    </div>\n  );\n}\n"}]}