{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "list-pinned-reorder",
  "type": "registry:component",
  "title": "List Pinned Reorder",
  "description": "A list where items pin to the top and move up or down with buttons or Alt and arrow keys, animated with a FLIP transition.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "lists",
    "list",
    "pin",
    "reorder",
    "flip",
    "sortable",
    "favorites"
  ],
  "docs": "https://hyperblur.dev/effects/list-pinned-reorder",
  "files": [
    {
      "path": "registry/hyperblur/list-pinned-reorder.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/list-pinned-reorder.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface PinnedItem {\n  id: string;\n  title: string;\n  subtitle?: string;\n  pinned?: boolean;\n  color?: string;\n}\n\nexport interface ListPinnedReorderProps\n  extends Omit<React.HTMLAttributes<HTMLUListElement>, \"onChange\" | \"children\" | \"defaultValue\"> {\n  value?: PinnedItem[];\n  defaultValue?: PinnedItem[];\n  onChange?: (items: PinnedItem[]) => void;\n  rowHeight?: number;\n  accent?: string;\n}\n\nconst defaultItems: PinnedItem[] = [\n  { id: \"p1\", title: \"Northwind redesign\", subtitle: \"12 tasks\", pinned: true, color: \"#2f7bff\" },\n  { id: \"p2\", title: \"Q4 planning\", subtitle: \"Due Fri\", color: \"#8b5cf6\" },\n  { id: \"p3\", title: \"Customer interviews\", subtitle: \"5 notes\", color: \"#30a46c\" },\n  { id: \"p4\", title: \"Pricing experiment\", subtitle: \"Draft\", color: \"#f5a623\" },\n  { id: \"p5\", title: \"Brand refresh\", subtitle: \"On hold\", color: \"#e5484d\" },\n];\n\nconst PALETTE = [\"#2f7bff\", \"#8b5cf6\", \"#30a46c\", \"#f5a623\", \"#e5484d\", \"#0ea5b7\", \"#d6409f\"];\n\nfunction hash(text: string) {\n  let h = 2166136261;\n  for (let i = 0; i < text.length; i++) h = Math.imul(h ^ text.charCodeAt(i), 16777619);\n  return h >>> 0;\n}\n\nfunction arrange(items: PinnedItem[]) {\n  return [...items.filter((i) => i.pinned), ...items.filter((i) => !i.pinned)];\n}\n\nconst css = `\n.hb-yc8 {\n  --hb-yc8-a: #2f7bff;\n  --hb-yc8-row: 36px;\n  box-sizing: border-box;\n  width: 100%;\n  margin: 0;\n  padding: 0;\n  list-style: none;\n  color: #ededed;\n  font-family: inherit;\n  font-size: 13px;\n  line-height: 18px;\n}\n.hb-yc8 *, .hb-yc8 *::before, .hb-yc8 *::after { box-sizing: border-box; }\n.hb-yc8-li {\n  position: relative;\n  display: flex;\n  align-items: center;\n  gap: 10px;\n  height: var(--hb-yc8-row);\n  padding: 0 4px 0 8px;\n  border: 1px solid #232323;\n  border-radius: 10px;\n  background: #121212;\n  will-change: transform;\n  transition: background-color .2s ease, border-color .2s ease, box-shadow .2s ease;\n}\n.hb-yc8-li + .hb-yc8-li { margin-top: 3px; }\n.hb-yc8-li:hover { background: #161616; border-color: #2c2c2c; }\n.hb-yc8-li[data-pinned=\"true\"] { background: color-mix(in srgb, var(--hb-yc8-a) 9%, #111111); border-color: color-mix(in srgb, var(--hb-yc8-a) 30%, #232323); }\n.hb-yc8-li[data-edge=\"true\"] { margin-bottom: 8px; }\n.hb-yc8-li[data-edge=\"true\"]::after {\n  content: \"\";\n  position: absolute;\n  right: 12px;\n  bottom: -5px;\n  left: 12px;\n  height: 1px;\n  background: repeating-linear-gradient(90deg, #333333 0 4px, transparent 4px 8px);\n}\n.hb-yc8-li[data-flash=\"true\"] { animation: hb-yc8-flash .7s ease-out; }\n.hb-yc8-tile { flex: none; width: 8px; height: 8px; border-radius: 3px; background: var(--hb-yc8-c, #2f7bff); box-shadow: 0 0 10px color-mix(in srgb, var(--hb-yc8-c, #2f7bff) 60%, transparent); }\n.hb-yc8-txt { display: flex; flex: 1; align-items: baseline; gap: 8px; min-width: 0; }\n.hb-yc8-name { min-width: 0; overflow: hidden; font-weight: var(--hb-yc8-w, 550); text-overflow: ellipsis; white-space: nowrap; }\n.hb-yc8-sub { flex: none; color: #77777f; font-size: 11.5px; white-space: nowrap; }\n.hb-yc8-acts { display: flex; flex: none; align-items: center; gap: 1px; }\n.hb-yc8-btn {\n  display: grid;\n  place-items: center;\n  width: 26px;\n  height: 26px;\n  margin: 0;\n  padding: 0;\n  border: 0;\n  border-radius: 7px;\n  background: transparent;\n  color: #6f6f6f;\n  cursor: pointer;\n  transition: background-color .15s ease, color .15s ease, transform .15s ease, opacity .15s ease;\n}\n.hb-yc8-btn svg { width: 15px; height: 15px; }\n.hb-yc8-btn:hover { background: #242424; color: #ededed; }\n.hb-yc8-btn:active { transform: scale(.9); }\n.hb-yc8-btn:focus-visible { outline: 2px solid var(--hb-yc8-a); outline-offset: 0; }\n.hb-yc8-btn[aria-disabled=\"true\"] { opacity: .28; cursor: default; }\n.hb-yc8-btn[aria-disabled=\"true\"]:hover { background: transparent; color: #6f6f6f; }\n.hb-yc8-pin svg { transition: transform .3s cubic-bezier(.3, 1.5, .5, 1); transform: rotate(45deg); }\n.hb-yc8-pin[aria-pressed=\"true\"] { color: var(--hb-yc8-a); }\n.hb-yc8-pin[aria-pressed=\"true\"] svg { transform: rotate(0); }\n.hb-yc8-pin[aria-pressed=\"true\"] svg path { fill: color-mix(in srgb, var(--hb-yc8-a) 35%, transparent); }\n.hb-yc8-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }\n@keyframes hb-yc8-flash {\n  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--hb-yc8-a) 55%, transparent); }\n  100% { box-shadow: 0 0 0 8px transparent; }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-yc8-li, .hb-yc8-btn, .hb-yc8-pin svg { transition: none; }\n  .hb-yc8-li[data-flash=\"true\"] { animation: none; }\n}\n`;\n\nexport function ListPinnedReorder({\n  value,\n  defaultValue = defaultItems,\n  onChange,\n  rowHeight = 36,\n  accent = \"#2f7bff\",\n  className = \"\",\n  style,\n  onKeyDown,\n  ...rest\n}: ListPinnedReorderProps) {\n  const rootRef = React.useRef<HTMLUListElement>(null);\n  const nodes = React.useRef(new Map<string, HTMLLIElement>());\n  const before = React.useRef<Map<string, number> | null>(null);\n  const restore = React.useRef<{ id: string; act: string } | null>(null);\n  const timer = React.useRef<number | null>(null);\n  const [inner, setInner] = React.useState(defaultValue);\n  const [focusId, setFocusId] = React.useState(\"\");\n  const [flashId, setFlashId] = React.useState(\"\");\n  const [note, setNote] = React.useState(\"\");\n  const list = arrange(value ?? inner);\n  const signature = list.map((i) => `${i.id}${i.pinned ? \"!\" : \"\"}`).join(\"|\");\n  const lastPinned = list.reduce((n, i, at) => (i.pinned ? at : n), -1);\n  const tabStop = list.some((i) => i.id === focusId) ? focusId : list[0]?.id;\n\n  React.useEffect(\n    () => () => {\n      if (timer.current) window.clearTimeout(timer.current);\n    },\n    [],\n  );\n\n  React.useLayoutEffect(() => {\n    const snapshot = before.current;\n    before.current = null;\n    const wanted = restore.current;\n    restore.current = null;\n    if (wanted) {\n      nodes.current.get(wanted.id)?.querySelector<HTMLElement>(`[data-yc8-act=\"${wanted.act}\"]`)?.focus({ preventScroll: true });\n    }\n    if (!snapshot || window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    nodes.current.forEach((el, id) => {\n      const from = snapshot.get(id);\n      if (from === undefined) return;\n      const delta = from - el.getBoundingClientRect().top;\n      if (Math.abs(delta) < 1) return;\n      el.style.transition = \"none\";\n      el.style.transform = `translateY(${delta.toFixed(1)}px)`;\n      void el.offsetHeight;\n      el.style.transition = \"transform .36s cubic-bezier(.2, .9, .2, 1), background-color .2s ease, border-color .2s ease\";\n      el.style.transform = \"\";\n    });\n  }, [signature]);\n\n  function commit(next: PinnedItem[], id: string, act: string, message: string) {\n    const snapshot = new Map<string, number>();\n    nodes.current.forEach((el, key) => snapshot.set(key, el.getBoundingClientRect().top));\n    before.current = snapshot;\n    restore.current = { id, act };\n    if (value === undefined) setInner(next);\n    onChange?.(next);\n    setFlashId(id);\n    setNote(message);\n    if (timer.current) window.clearTimeout(timer.current);\n    timer.current = window.setTimeout(() => setFlashId(\"\"), 750);\n  }\n\n  function togglePin(id: string) {\n    const at = list.findIndex((i) => i.id === id);\n    const item = list[at];\n    if (!item) return;\n    const rest = list.filter((i) => i.id !== id);\n    const pinnedCount = rest.filter((i) => i.pinned).length;\n    rest.splice(pinnedCount, 0, { ...item, pinned: !item.pinned });\n    commit(rest, id, \"pin\", `${item.title} ${item.pinned ? \"unpinned\" : \"pinned\"}`);\n  }\n\n  function move(id: string, dir: -1 | 1, act: string) {\n    const at = list.findIndex((i) => i.id === id);\n    const to = at + dir;\n    const item = list[at];\n    const other = list[to];\n    if (!item || !other || !!other.pinned !== !!item.pinned) return;\n    const next = [...list];\n    next[at] = other;\n    next[to] = item;\n    commit(next, id, act, `${item.title} moved to position ${to + 1} of ${list.length}`);\n  }\n\n  function handleKeyDown(e: React.KeyboardEvent<HTMLUListElement>) {\n    onKeyDown?.(e);\n    if (e.defaultPrevented) return;\n    const target = e.target as HTMLElement;\n    const act = target.dataset.yc8Act;\n    const id = target.closest<HTMLElement>(\"[data-yc8-id]\")?.dataset.yc8Id;\n    if (e.key === \"Escape\") {\n      target.blur();\n      return;\n    }\n    if (!act || !id) return;\n    if ((e.key === \"ArrowUp\" || e.key === \"ArrowDown\") && e.altKey) {\n      e.preventDefault();\n      move(id, e.key === \"ArrowUp\" ? -1 : 1, act);\n      return;\n    }\n    const at = list.findIndex((i) => i.id === id);\n    let next = -1;\n    if (e.key === \"ArrowDown\") next = Math.min(list.length - 1, at + 1);\n    else if (e.key === \"ArrowUp\") next = Math.max(0, at - 1);\n    else if (e.key === \"Home\") next = 0;\n    else if (e.key === \"End\") next = list.length - 1;\n    if (next >= 0) {\n      e.preventDefault();\n      const nextId = list[next]?.id;\n      if (!nextId) return;\n      setFocusId(nextId);\n      nodes.current.get(nextId)?.querySelector<HTMLElement>(`[data-yc8-act=\"${act}\"]`)?.focus();\n    }\n  }\n\n  const vars = { \"--hb-yc8-a\": accent, \"--hb-yc8-row\": `${rowHeight}px`, ...style } as React.CSSProperties;\n\n  return (\n    <ul ref={rootRef} className={`hb-yc8 ${className}`} role=\"list\" style={vars} onKeyDown={handleKeyDown} {...rest}>\n      <style href=\"hb-list-pinned-reorder\" precedence=\"default\">{css}</style>\n      {list.map((item, at) => {\n        const stop = item.id === tabStop ? 0 : -1;\n        const canUp = at > 0 && !!list[at - 1]?.pinned === !!item.pinned;\n        const canDown = at < list.length - 1 && !!list[at + 1]?.pinned === !!item.pinned;\n        return (\n          <li\n            key={item.id}\n            ref={(el) => {\n              if (el) nodes.current.set(item.id, el);\n              else nodes.current.delete(item.id);\n            }}\n            className=\"hb-yc8-li\"\n            data-yc8-id={item.id}\n            data-pinned={!!item.pinned}\n            data-edge={at === lastPinned && lastPinned < list.length - 1}\n            data-flash={flashId === item.id}\n          >\n            <span className=\"hb-yc8-tile\" style={{ \"--hb-yc8-c\": item.color ?? PALETTE[hash(item.title) % PALETTE.length] } as React.CSSProperties} aria-hidden=\"true\" />\n            <span className=\"hb-yc8-txt\">\n              <span className=\"hb-yc8-name\">{item.title}</span>\n              {item.subtitle ? <span className=\"hb-yc8-sub\">{item.subtitle}</span> : null}\n            </span>\n            <span className=\"hb-yc8-acts\">\n              <button\n                type=\"button\"\n                className=\"hb-yc8-btn hb-yc8-pin\"\n                data-yc8-act=\"pin\"\n                tabIndex={stop}\n                aria-pressed={!!item.pinned}\n                aria-label={`${item.pinned ? \"Unpin\" : \"Pin\"} ${item.title}`}\n                title={item.pinned ? \"Unpin\" : \"Pin to top\"}\n                onFocus={() => setFocusId(item.id)}\n                onClick={() => togglePin(item.id)}\n              >\n                <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.4\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                  <path d=\"M6 2.5h4l-.6 3.4 2.1 2.1v1H8.6V13.5h-1.2V9H4.5V8l2.1-2.1z\" />\n                </svg>\n              </button>\n              <button\n                type=\"button\"\n                className=\"hb-yc8-btn\"\n                data-yc8-act=\"up\"\n                tabIndex={stop}\n                aria-disabled={!canUp}\n                aria-label={`Move ${item.title} up`}\n                title=\"Move up\"\n                onFocus={() => setFocusId(item.id)}\n                onClick={() => canUp && move(item.id, -1, \"up\")}\n              >\n                <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                  <path d=\"M4 9.5L8 5.5l4 4\" />\n                </svg>\n              </button>\n              <button\n                type=\"button\"\n                className=\"hb-yc8-btn\"\n                data-yc8-act=\"down\"\n                tabIndex={stop}\n                aria-disabled={!canDown}\n                aria-label={`Move ${item.title} down`}\n                title=\"Move down\"\n                onFocus={() => setFocusId(item.id)}\n                onClick={() => canDown && move(item.id, 1, \"down\")}\n              >\n                <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                  <path d=\"M4 6.5l4 4 4-4\" />\n                </svg>\n              </button>\n            </span>\n          </li>\n        );\n      })}\n      <li className=\"hb-yc8-sr\" role=\"status\" aria-live=\"polite\">\n        {note}\n      </li>\n    </ul>\n  );\n}\n"
    }
  ]
}