{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-reorder-list",
  "title": "Reorder list",
  "description": "A list reordered by a drag handle, with move up and move down buttons and keyboard reordering.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/icons.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "components/core/reorder-list.tsx",
      "content": "'use client';\n\nimport { Reorder, type Transition, useDragControls } from 'motion/react';\nimport { useId, useRef, useState } from 'react';\n\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport {\n  ChevronDownIcon,\n  ChevronUpIcon,\n  GripVerticalIcon,\n} from '@/registry/ndeko/lib/icons';\nimport { Button } from '@/registry/ndeko/ui/button';\n\nconst SETTLE: Transition = { type: 'spring', bounce: 0, duration: 0.3 };\n\nconst INSTANT: Transition = { duration: 0 };\n\nconst LIFT = { scale: 1.02 };\n\nconst ROW = [\n  'relative flex items-center gap-1 rounded-xl bg-card p-1.5 text-card-foreground',\n  'hairline-ring ring-border shadow-[0_1px_2px_rgb(0_0_0/0.04)]',\n  'outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring',\n].join(' ');\n\nconst HANDLE = [\n  'grid size-7 shrink-0 cursor-grab touch-none place-items-center rounded-md text-muted-foreground',\n  'transition-colors hover:bg-foreground/5 hover:text-foreground active:cursor-grabbing',\n].join(' ');\n\nfunction moveItem<T>(items: T[], from: number, to: number) {\n  const next = [...items];\n  const [moved] = next.splice(from, 1);\n  next.splice(to, 0, moved);\n  return next;\n}\n\nfunction settleOf(prefersReducedMotion: boolean | null) {\n  if (prefersReducedMotion) {\n    return INSTANT;\n  }\n  return SETTLE;\n}\n\nfunction liftOf(prefersReducedMotion: boolean | null) {\n  if (prefersReducedMotion) {\n    return undefined;\n  }\n  return LIFT;\n}\n\nfunction positionMessage(label: string, index: number, count: number) {\n  const position = index + 1;\n  return `${label} moved to position ${position} of ${count}`;\n}\n\ntype ReorderRowProps<T> = {\n  item: T;\n  index: number;\n  count: number;\n  label: string;\n  hintId: string;\n  groupRef: React.RefObject<HTMLUListElement | null>;\n  onMove: (from: number, to: number) => void;\n  onDragStart: () => void;\n  onDragEnd: () => void;\n  children: React.ReactNode;\n};\n\nfunction ReorderRow<T>({\n  item,\n  index,\n  count,\n  label,\n  hintId,\n  groupRef,\n  onMove,\n  onDragStart,\n  onDragEnd,\n  children,\n}: ReorderRowProps<T>) {\n  const prefersReducedMotion = usePrefersReducedMotion();\n  const controls = useDragControls();\n  const isFirst = index === 0;\n  const lastIndex = count - 1;\n  const isLast = index === lastIndex;\n\n  function startDrag(event: React.PointerEvent) {\n    onDragStart();\n    controls.start(event);\n  }\n\n  function moveUp() {\n    onMove(index, index - 1);\n  }\n\n  function moveDown() {\n    onMove(index, index + 1);\n  }\n\n  return (\n    <Reorder.Item\n      value={item}\n      dragListener={false}\n      dragControls={controls}\n      dragConstraints={groupRef}\n      dragElastic={0.08}\n      onDragEnd={onDragEnd}\n      whileDrag={liftOf(prefersReducedMotion)}\n      transition={settleOf(prefersReducedMotion)}\n      aria-describedby={hintId}\n      className={ROW}\n    >\n      <span aria-hidden='true' className={HANDLE} onPointerDown={startDrag}>\n        <GripVerticalIcon className='size-4' />\n      </span>\n      <div className='min-w-0 flex-1 px-1'>{children}</div>\n      <Button\n        type='button'\n        variant='ghost'\n        size='icon-sm'\n        className='text-muted-foreground'\n        aria-label={`Move ${label} up`}\n        disabled={isFirst}\n        focusableWhenDisabled\n        onClick={moveUp}\n      >\n        <ChevronUpIcon />\n      </Button>\n      <Button\n        type='button'\n        variant='ghost'\n        size='icon-sm'\n        className='text-muted-foreground'\n        aria-label={`Move ${label} down`}\n        disabled={isLast}\n        focusableWhenDisabled\n        onClick={moveDown}\n      >\n        <ChevronDownIcon />\n      </Button>\n    </Reorder.Item>\n  );\n}\n\nexport type ReorderListProps<T> = {\n  items: T[];\n  onReorder: (items: T[]) => void;\n  getKey: (item: T) => string;\n  getLabel: (item: T) => string;\n  renderItem: (item: T) => React.ReactNode;\n  className?: string;\n  'aria-label'?: string;\n};\n\nexport function ReorderList<T>({\n  items,\n  onReorder,\n  getKey,\n  getLabel,\n  renderItem,\n  className,\n  'aria-label': ariaLabel,\n}: ReorderListProps<T>) {\n  const groupRef = useRef<HTMLUListElement>(null);\n  const isPointerDragging = useRef(false);\n  const draggedItem = useRef<T | null>(null);\n  const [announcement, setAnnouncement] = useState('');\n  const hintId = useId();\n  const count = items.length;\n\n  function announce(item: T, nextItems: T[]) {\n    const index = nextItems.indexOf(item);\n    const label = getLabel(item);\n    const message = positionMessage(label, index, count);\n    setAnnouncement(message);\n  }\n\n  function reorderFromGroup(nextItems: T[], moved: T) {\n    onReorder(nextItems);\n    draggedItem.current = moved;\n    if (isPointerDragging.current) {\n      return;\n    }\n    announce(moved, nextItems);\n  }\n\n  function moveByButton(from: number, to: number) {\n    const moved = items[from];\n    const nextItems = moveItem(items, from, to);\n    onReorder(nextItems);\n    announce(moved, nextItems);\n  }\n\n  function beginPointerDrag() {\n    isPointerDragging.current = true;\n    draggedItem.current = null;\n  }\n\n  function endPointerDrag() {\n    isPointerDragging.current = false;\n    const moved = draggedItem.current;\n    if (moved === null) {\n      return;\n    }\n    announce(moved, items);\n  }\n\n  return (\n    <>\n      <Reorder.Group\n        ref={groupRef}\n        axis='y'\n        values={items}\n        onReorder={reorderFromGroup}\n        aria-label={ariaLabel}\n        className={cn('flex flex-col gap-2', className)}\n      >\n        {items.map((item, index) => (\n          <ReorderRow\n            key={getKey(item)}\n            item={item}\n            index={index}\n            count={count}\n            label={getLabel(item)}\n            hintId={hintId}\n            groupRef={groupRef}\n            onMove={moveByButton}\n            onDragStart={beginPointerDrag}\n            onDragEnd={endPointerDrag}\n          >\n            {renderItem(item)}\n          </ReorderRow>\n        ))}\n      </Reorder.Group>\n      <p id={hintId} className='sr-only'>\n        Drag the handle, use the move buttons, or press Space to pick up the\n        row, the arrow keys to move it and Space to drop it.\n      </p>\n      <p aria-live='polite' className='sr-only'>\n        {announcement}\n      </p>\n    </>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/core/reorder-list.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}