{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sortable-list",
  "type": "registry:component",
  "title": "Sortable List",
  "description": "A drag-to-reorder list with pointer dragging, siblings that slide out of the way, and full keyboard reordering with live announcements.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "lists",
    "sortable",
    "drag",
    "reorder",
    "keyboard",
    "accessible",
    "tasks"
  ],
  "docs": "https://hyperblur.dev/effects/sortable-list",
  "files": [
    {
      "path": "registry/hyperblur/sortable-list.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/sortable-list.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type SortableTone = \"blue\" | \"green\" | \"amber\" | \"red\" | \"neutral\";\n\nexport interface SortableItem {\n  id: string;\n  title: string;\n  meta?: string;\n  tone?: SortableTone;\n}\n\nexport const defaultTasks: SortableItem[] = [\n  { id: \"s1\", title: \"Design review\", meta: \"High\", tone: \"red\" },\n  { id: \"s2\", title: \"Ship onboarding flow\", meta: \"Medium\", tone: \"amber\" },\n  { id: \"s3\", title: \"Write release notes\", meta: \"Low\", tone: \"green\" },\n  { id: \"s4\", title: \"Fix billing webhook\", meta: \"High\", tone: \"red\" },\n  { id: \"s5\", title: \"Plan Q4 roadmap\", meta: \"Medium\", tone: \"amber\" },\n];\n\nexport interface SortableListProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n  items?: SortableItem[];\n  label?: string;\n  accent?: string;\n  onReorder?: (items: SortableItem[], from: number, to: number) => void;\n}\n\nconst css = `\n.hb-srt {\n  --hb-srt-accent: #2f7bff;\n  --hb-srt-gap: 6px;\n  container-type: inline-size;\n  position: relative;\n  width: 100%;\n  min-width: 0;\n  box-sizing: border-box;\n  font-family: inherit;\n  font-size: 13px;\n  line-height: 1.4;\n  color: #ededed;\n}\n.hb-srt-list { display: flex; flex-direction: column; gap: var(--hb-srt-gap); margin: 0; padding: 0; list-style: none; }\n.hb-srt *, .hb-srt *::before, .hb-srt *::after { box-sizing: border-box; font-family: inherit; }\n.hb-srt-row {\n  position: relative;\n  display: flex;\n  align-items: center;\n  gap: 10px;\n  min-width: 0;\n  height: 42px;\n  padding: 0 12px 0 6px;\n  border: 1px solid #222222;\n  border-radius: 11px;\n  background: #131313;\n  user-select: none;\n  -webkit-user-select: none;\n  outline: 0;\n  transition: background-color .15s ease, border-color .15s ease, box-shadow .2s ease;\n}\n.hb-srt-row:hover { background: #171717; border-color: #2c2c2c; }\n.hb-srt-row:focus-visible { border-color: color-mix(in srgb, var(--hb-srt-accent) 85%, transparent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--hb-srt-accent) 22%, transparent); }\n.hb-srt[data-busy=\"true\"] .hb-srt-row { transition: transform .22s cubic-bezier(.2, .8, .2, 1), background-color .15s ease, border-color .15s ease, box-shadow .2s ease; }\n.hb-srt-row[data-lifted=\"true\"] {\n  z-index: 3;\n  background: #181d28;\n  border-color: color-mix(in srgb, var(--hb-srt-accent) 70%, transparent);\n  box-shadow: 0 18px 36px rgba(0, 0, 0, .55), 0 0 0 3px color-mix(in srgb, var(--hb-srt-accent) 18%, transparent);\n}\n.hb-srt-row[data-lifted=\"true\"][data-following=\"true\"] { transition: background-color .15s ease, border-color .15s ease, box-shadow .2s ease; }\n.hb-srt-grip {\n  flex: none;\n  display: grid;\n  place-items: center;\n  width: 26px;\n  height: 30px;\n  border-radius: 7px;\n  color: #5a5a5a;\n  cursor: grab;\n  touch-action: none;\n  transition: color .15s ease, background-color .15s ease;\n}\n.hb-srt-grip svg { width: 12px; height: 16px; }\n.hb-srt-row:hover .hb-srt-grip { color: #8f8f8f; }\n.hb-srt-grip:hover { background: rgba(255, 255, 255, .06); color: #ededed; }\n.hb-srt-row[data-lifted=\"true\"] .hb-srt-grip { cursor: grabbing; color: var(--hb-srt-accent); }\n.hb-srt-num { flex: none; width: 20px; font-size: 11px; font-variant-numeric: tabular-nums; color: #6b6b6b; letter-spacing: .02em; }\n.hb-srt-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }\n.hb-srt-chip {\n  flex: none;\n  padding: 1px 9px;\n  border-radius: 999px;\n  border: 1px solid var(--hb-srt-bd);\n  background: var(--hb-srt-bg);\n  color: var(--hb-srt-fg);\n  font-size: 11px;\n  font-weight: 500;\n}\n.hb-srt-chip[data-tone=\"neutral\"] { --hb-srt-bd: #2a2a2a; --hb-srt-bg: #181818; --hb-srt-fg: #a1a1a1; }\n.hb-srt-chip[data-tone=\"blue\"] { --hb-srt-bd: rgba(47, 123, 255, .35); --hb-srt-bg: rgba(47, 123, 255, .12); --hb-srt-fg: #7fb3ff; }\n.hb-srt-chip[data-tone=\"green\"] { --hb-srt-bd: rgba(62, 207, 142, .32); --hb-srt-bg: rgba(62, 207, 142, .11); --hb-srt-fg: #5fdca6; }\n.hb-srt-chip[data-tone=\"amber\"] { --hb-srt-bd: rgba(245, 165, 36, .32); --hb-srt-bg: rgba(245, 165, 36, .11); --hb-srt-fg: #f5b84a; }\n.hb-srt-chip[data-tone=\"red\"] { --hb-srt-bd: rgba(255, 107, 107, .32); --hb-srt-bg: rgba(255, 107, 107, .11); --hb-srt-fg: #ff8484; }\n.hb-srt-sr { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }\n@container (max-width: 260px) {\n  .hb-srt-num { display: none; }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-srt-row, .hb-srt-grip { transition: none !important; }\n}\n`;\n\nfunction moveItem<T>(list: T[], from: number, to: number) {\n  const next = list.slice();\n  const [picked] = next.splice(from, 1);\n  next.splice(to, 0, picked);\n  return next;\n}\n\ninterface DragState {\n  id: string;\n  step: number;\n  from: number;\n  to: number;\n  dy: number;\n  settling: boolean;\n}\n\ninterface GrabState {\n  id: string;\n  from: number;\n  snapshot: SortableItem[];\n}\n\nexport function SortableList({\n  items = defaultTasks,\n  label = \"Sortable list\",\n  accent = \"#2f7bff\",\n  onReorder,\n  className = \"\",\n  style,\n  ...rest\n}: SortableListProps) {\n  const uid = React.useId();\n  const sourceKey = items.map((item) => item.id).join(\"|\");\n  const [seenKey, setSeenKey] = React.useState(sourceKey);\n  const [list, setList] = React.useState<SortableItem[]>(items);\n  const [drag, setDrag] = React.useState<DragState | null>(null);\n  const [grab, setGrab] = React.useState<GrabState | null>(null);\n  const [focusId, setFocusId] = React.useState<string>(items[0]?.id ?? \"\");\n  const [message, setMessage] = React.useState(\"\");\n  const rows = React.useRef(new Map<string, HTMLLIElement>());\n  const before = React.useRef<Map<string, DOMRect> | null>(null);\n  const moving = React.useRef(false);\n  const session = React.useRef<{ id: string; from: number; startY: number; step: number; count: number } | null>(null);\n  const timers = React.useRef<ReturnType<typeof setTimeout>[]>([]);\n\n  if (seenKey !== sourceKey) {\n    setSeenKey(sourceKey);\n    setList(items);\n  }\n\n  React.useEffect(() => {\n    const pending = timers.current;\n    return () => pending.forEach(clearTimeout);\n  }, []);\n\n  React.useLayoutEffect(() => {\n    const first = before.current;\n    before.current = null;\n    if (!first) return;\n    rows.current.get(focusId)?.focus({ preventScroll: true });\n    moving.current = false;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    rows.current.forEach((el, id) => {\n      const from = first.get(id);\n      if (!from) return;\n      const to = el.getBoundingClientRect();\n      const dy = from.top - to.top;\n      if (!dy) return;\n      el.animate(\n        [{ transform: `translateY(${dy.toFixed(1)}px)` }, { transform: \"none\" }],\n        { duration: 240, easing: \"cubic-bezier(.2, .8, .2, 1)\" },\n      );\n    });\n  }, [list, grab, focusId]);\n\n  const finishDrop = (id: string, from: number, to: number) => {\n    const next = from === to ? list : moveItem(list, from, to);\n    setList(next);\n    setDrag(null);\n    const title = list[from]?.title ?? \"\";\n    setMessage(from === to ? `${title} dropped, position unchanged` : `${title} dropped at position ${to + 1} of ${list.length}`);\n    if (from !== to) onReorder?.(next, from, to);\n  };\n\n  const onGripDown = (event: React.PointerEvent<HTMLSpanElement>, id: string, index: number) => {\n    if (event.button !== 0 || grab) return;\n    const row = rows.current.get(id);\n    const parent = row?.parentElement;\n    if (!row || !parent) return;\n    event.preventDefault();\n    event.currentTarget.setPointerCapture(event.pointerId);\n    const gap = parseFloat(getComputedStyle(parent).rowGap) || 0;\n    session.current = { id, from: index, startY: event.clientY, step: row.offsetHeight + gap, count: list.length };\n    setFocusId(id);\n    row.focus({ preventScroll: true });\n    setDrag({ id, step: session.current.step, from: index, to: index, dy: 0, settling: false });\n    setMessage(`Picked up ${list[index].title}`);\n  };\n\n  const onGripMove = (event: React.PointerEvent<HTMLSpanElement>) => {\n    const s = session.current;\n    if (!s) return;\n    const dy = Math.max(-s.from * s.step, Math.min((s.count - 1 - s.from) * s.step, event.clientY - s.startY));\n    const to = Math.max(0, Math.min(s.count - 1, s.from + Math.round(dy / s.step)));\n    setDrag((prev) => (prev && !prev.settling ? { ...prev, dy, to } : prev));\n  };\n\n  const onGripUp = (event: React.PointerEvent<HTMLSpanElement>, cancel: boolean) => {\n    const s = session.current;\n    if (!s) return;\n    session.current = null;\n    if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId);\n    const current = drag;\n    if (!current) return;\n    const to = cancel ? s.from : current.to;\n    setDrag({ ...current, to, dy: (to - s.from) * s.step, settling: true });\n    const t = setTimeout(() => finishDrop(s.id, s.from, to), 210);\n    timers.current.push(t);\n  };\n\n  const capture = () => {\n    const snapshot = new Map<string, DOMRect>();\n    rows.current.forEach((el, id) => snapshot.set(id, el.getBoundingClientRect()));\n    before.current = snapshot;\n    moving.current = true;\n  };\n\n  const onRowKey = (event: React.KeyboardEvent<HTMLLIElement>, id: string, index: number) => {\n    if (drag || event.target !== event.currentTarget) return;\n    const key = event.key;\n    if (key === \" \" || key === \"Spacebar\") {\n      event.preventDefault();\n      if (!grab) {\n        setGrab({ id, from: index, snapshot: list });\n        setMessage(`Picked up ${list[index].title}. Position ${index + 1} of ${list.length}. Use the arrow keys to move, Space to drop, Escape to cancel.`);\n      } else {\n        const origin = grab.from;\n        setGrab(null);\n        setMessage(\n          origin === index\n            ? `${list[index].title} dropped, position unchanged`\n            : `${list[index].title} dropped at position ${index + 1} of ${list.length}`,\n        );\n        if (origin !== index) onReorder?.(list, origin, index);\n      }\n    } else if (key === \"ArrowUp\" || key === \"ArrowDown\") {\n      event.preventDefault();\n      const delta = key === \"ArrowUp\" ? -1 : 1;\n      const target = index + delta;\n      if (target < 0 || target >= list.length) return;\n      if (grab && grab.id === id) {\n        capture();\n        setList(moveItem(list, index, target));\n        setMessage(`${list[index].title} moved to position ${target + 1} of ${list.length}`);\n      } else if (!grab) {\n        rows.current.get(list[target].id)?.focus();\n      }\n    } else if (key === \"Escape\" && grab && grab.id === id) {\n      event.preventDefault();\n      capture();\n      setList(grab.snapshot);\n      setGrab(null);\n      setMessage(`Reorder cancelled. ${list[index].title} returned to position ${grab.from + 1}`);\n    }\n  };\n\n  const onRowBlur = (id: string, index: number) => {\n    if (!grab || grab.id !== id || moving.current) return;\n    const origin = grab.from;\n    setGrab(null);\n    setMessage(`${list[index].title} dropped at position ${index + 1} of ${list.length}`);\n    if (origin !== index) onReorder?.(list, origin, index);\n  };\n\n  const vars = { \"--hb-srt-accent\": accent, ...style } as React.CSSProperties;\n  return (\n    <div className={`hb-srt ${className}`} style={vars} data-busy={drag !== null} {...rest}>\n      <style href=\"hb-sortable-list\" precedence=\"default\">{css}</style>\n      <span className=\"hb-srt-sr\" id={`${uid}-help`}>\n        Focus an item and press Space to pick it up. Use the arrow keys to move it, Space to drop, Escape to cancel.\n      </span>\n      <ul className=\"hb-srt-list\" aria-label={label}>\n      {list.map((item, index) => {\n        const lifted = (drag && drag.id === item.id) || (grab && grab.id === item.id);\n        let transform: string | undefined;\n        if (drag) {\n          if (drag.id === item.id) {\n            transform = `translateY(${drag.dy.toFixed(1)}px) scale(${drag.settling ? 1 : 1.02})`;\n          } else if (drag.to > drag.from && index > drag.from && index <= drag.to) {\n            transform = `translateY(${(-drag.step).toFixed(1)}px)`;\n          } else if (drag.to < drag.from && index >= drag.to && index < drag.from) {\n            transform = `translateY(${drag.step.toFixed(1)}px)`;\n          }\n        } else if (grab && grab.id === item.id) {\n          transform = \"scale(1.02)\";\n        }\n        return (\n          <li\n            key={item.id}\n            ref={(el) => {\n              if (el) rows.current.set(item.id, el);\n              else rows.current.delete(item.id);\n            }}\n            className=\"hb-srt-row\"\n            tabIndex={item.id === focusId || (focusId === \"\" && index === 0) ? 0 : -1}\n            aria-roledescription=\"sortable item\"\n            aria-describedby={`${uid}-help`}\n            aria-label={`${item.title}${item.meta ? `, ${item.meta}` : \"\"}, ${index + 1} of ${list.length}${grab && grab.id === item.id ? \", grabbed\" : \"\"}`}\n            data-lifted={!!lifted}\n            data-following={!!drag && drag.id === item.id && !drag.settling}\n            style={transform ? { transform } : undefined}\n            onFocus={(event) => {\n              if (event.target === event.currentTarget) setFocusId(item.id);\n            }}\n            onKeyDown={(event) => onRowKey(event, item.id, index)}\n            onBlur={(event) => {\n              if (event.target === event.currentTarget) onRowBlur(item.id, index);\n            }}\n          >\n            <span\n              className=\"hb-srt-grip\"\n              aria-hidden=\"true\"\n              onPointerDown={(event) => onGripDown(event, item.id, index)}\n              onPointerMove={onGripMove}\n              onPointerUp={(event) => onGripUp(event, false)}\n              onPointerCancel={(event) => onGripUp(event, true)}\n            >\n              <svg viewBox=\"0 0 12 16\" fill=\"currentColor\">\n                <circle cx=\"3.5\" cy=\"3\" r=\"1.3\" />\n                <circle cx=\"8.5\" cy=\"3\" r=\"1.3\" />\n                <circle cx=\"3.5\" cy=\"8\" r=\"1.3\" />\n                <circle cx=\"8.5\" cy=\"8\" r=\"1.3\" />\n                <circle cx=\"3.5\" cy=\"13\" r=\"1.3\" />\n                <circle cx=\"8.5\" cy=\"13\" r=\"1.3\" />\n              </svg>\n            </span>\n            <span className=\"hb-srt-num\" aria-hidden=\"true\">\n              {String(index + 1).padStart(2, \"0\")}\n            </span>\n            <span className=\"hb-srt-title\">{item.title}</span>\n            {item.meta && (\n              <span className=\"hb-srt-chip\" data-tone={item.tone ?? \"neutral\"}>\n                {item.meta}\n              </span>\n            )}\n          </li>\n        );\n      })}\n      </ul>\n      <span className=\"hb-srt-sr\" role=\"status\" aria-live=\"assertive\">\n        {message}\n      </span>\n    </div>\n  );\n}\n"
    }
  ]
}