{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"reorder-list","type":"registry:ui","title":"Reorder List","description":"A list you drag into a new order by its handles: the picked-up row lifts with a shadow while the others spring aside. Handles also move rows with the arrow keys, announced to screen readers.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/reorder-list.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { GripVertical } from \"lucide-react\";\nimport { MotionConfig, Reorder, useDragControls } from \"motion/react\";\nimport { useRef, useState, type KeyboardEvent, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface ReorderItem {\n  id: string;\n  label: ReactNode;\n  /** Optional second line. */\n  description?: ReactNode;\n  /** Optional element before the label, e.g. an icon. */\n  leading?: ReactNode;\n}\n\nexport interface ReorderListProps {\n  /** Controlled order. */\n  items?: ReorderItem[];\n  defaultItems?: ReorderItem[];\n  onChange?: (items: ReorderItem[]) => void;\n  /** Accessible name of the list. */\n  label?: string;\n  className?: string;\n}\n\nfunction Row({\n  item,\n  index,\n  count,\n  move,\n}: {\n  item: ReorderItem;\n  index: number;\n  count: number;\n  move: (from: number, to: number) => void;\n}) {\n  const controls = useDragControls();\n\n  const onKey = (e: KeyboardEvent) => {\n    const to = e.key === \"ArrowUp\" ? index - 1 : e.key === \"ArrowDown\" ? index + 1 : -1;\n    if (to < 0 || to >= count) return;\n    e.preventDefault();\n    move(index, to);\n  };\n\n  return (\n    <Reorder.Item\n      value={item}\n      dragListener={false}\n      dragControls={controls}\n      className=\"relative flex select-none items-center gap-2.5 rounded-xl border border-border bg-card py-2.5 pl-2 pr-3.5\"\n      whileDrag={{ scale: 1.03, boxShadow: \"0 18px 40px -12px rgb(0 0 0 / 0.45)\", zIndex: 10 }}\n      transition={{ type: \"spring\", bounce: 0.25, duration: 0.4 }}\n    >\n      <button\n        type=\"button\"\n        aria-label={`Move ${typeof item.label === \"string\" ? item.label : \"item\"}, position ${index + 1} of ${count}. Use arrow keys.`}\n        onPointerDown={(e) => controls.start(e)}\n        onKeyDown={onKey}\n        className=\"grid size-7 shrink-0 cursor-grab touch-none place-items-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring active:cursor-grabbing\"\n      >\n        <GripVertical className=\"size-4\" />\n      </button>\n      {item.leading}\n      <div className=\"min-w-0 flex-1\">\n        <div className=\"truncate text-sm font-medium text-foreground\">{item.label}</div>\n        {item.description && <div className=\"truncate text-xs text-muted-foreground\">{item.description}</div>}\n      </div>\n      <span className=\"text-xs tabular-nums text-muted-foreground\">{index + 1}</span>\n    </Reorder.Item>\n  );\n}\n\n/**\n * A list you can drag into a new order by its handles. The picked-up row lifts\n * with a shadow while the others spring out of its way, and the handles also\n * move rows with the arrow keys, announced to screen readers.\n */\nexport function ReorderList({ items, defaultItems = [], onChange, label = \"Reorderable list\", className }: ReorderListProps) {\n  const [inner, setInner] = useState(defaultItems);\n  const [announce, setAnnounce] = useState(\"\");\n  const group = useRef<HTMLUListElement>(null);\n  const list = items ?? inner;\n\n  const set = (next: ReorderItem[]) => {\n    if (items === undefined) setInner(next);\n    onChange?.(next);\n  };\n\n  const move = (from: number, to: number) => {\n    const next = [...list];\n    const [it] = next.splice(from, 1);\n    next.splice(to, 0, it);\n    set(next);\n    setAnnounce(`Moved to position ${to + 1} of ${next.length}`);\n    // Keep focus on the moved row's handle after it re-renders.\n    requestAnimationFrame(() => group.current?.querySelectorAll<HTMLElement>(\":scope > li > button\")[to]?.focus());\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <Reorder.Group axis=\"y\" values={list} onReorder={set} ref={group} aria-label={label} className={cn(\"grid grid-cols-[minmax(0,1fr)] gap-2\", className)}>\n        {list.map((item, i) => (\n          <Row key={item.id} item={item} index={i} count={list.length} move={move} />\n        ))}\n      </Reorder.Group>\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {announce}\n      </span>\n    </MotionConfig>\n  );\n}\n"}]}