{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sortable",
  "type": "registry:ui",
  "title": "SortableList",
  "description": "فهرست کشیدنی",
  "dependencies": [
    "@kashi-ui/persian@^2.26.0"
  ],
  "registryDependencies": [
    "https://kashi.sepantapouya.com/r/cx.json",
    "https://kashi.sepantapouya.com/r/dnd.json",
    "https://kashi.sepantapouya.com/r/kashi-provider.json",
    "https://kashi.sepantapouya.com/r/utils.json"
  ],
  "files": [
    {
      "path": "registry/kashi/Sortable.tsx",
      "type": "registry:ui",
      "target": "components/kashi/Sortable.tsx",
      "content": "\"use client\";\n\nimport { useRef, useState, type KeyboardEvent, type PointerEvent, type ReactNode } from \"react\";\nimport { cx } from \"./cx\";\nimport { move, useFlip } from \"./dnd\";\nimport { useIsomorphicLayoutEffect } from \"./utils\";\nimport { useKashiLocale, useMessages } from \"./KashiProvider\";\nimport { faSortable } from \"@kashi-ui/persian\";\n\nexport interface SortableRenderState {\n  /** دستگیرهٔ کشیدن؛ هر جای مورد که بخواهید بگذاریدش */\n  handle: ReactNode;\n  /** این مورد در حال کشیدن است */\n  dragging: boolean;\n  index: number;\n}\n\nexport interface SortableListProps<T> {\n  items: T[];\n  getId: (item: T) => string;\n  /** نام مورد برای صفحه‌خوان، مثل «کار ۳» */\n  getLabel: (item: T) => string;\n  /** با رها کردن؛ آرایهٔ مرتب تازه */\n  onReorder: (items: T[]) => void;\n  /** محتوای هر مورد؛ بدون آن متن getLabel با دستگیره نشان داده می‌شود */\n  renderItem?: (item: T, state: SortableRenderState) => ReactNode;\n  /** نام فهرست برای صفحه‌خوان */\n  \"aria-label\"?: string;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst Grip = () => (\n  <svg viewBox=\"0 0 20 20\" fill=\"currentColor\" aria-hidden=\"true\">\n    {[6, 10, 14].flatMap((y) => [<circle key={`a${y}`} cx=\"7.5\" cy={y} r=\"1.3\" />, <circle key={`b${y}`} cx=\"12.5\" cy={y} r=\"1.3\" />])}\n  </svg>\n);\n\n/**\n * فهرست کشیدنی: با ماوس و لمس از دستگیره بکشید، یا با کیبورد: فاصله برای برداشتن، کلیدهای جهت برای حرکت،\n * فاصلهٔ دوباره برای رها کردن و Escape برای لغو. هر حرکت برای صفحه‌خوان اعلام می‌شود.\n */\nexport function SortableList<T>({ items, getId, getLabel, onReorder, renderItem, \"aria-label\": ariaLabel, disabled, className }: SortableListProps<T>) {\n  const M = useMessages({ sortable: faSortable });\n  const { n } = useKashiLocale();\n  const S = M.sortable;\n  const root = useRef<HTMLUListElement>(null);\n  // ترتیب پیش‌نمایش هنگام کشیدن؛ null یعنی همان items\n  const [preview, setPreview] = useState<T[] | null>(null);\n  const [active, setActive] = useState<string | null>(null);\n  const [mode, setMode] = useState<\"pointer\" | \"keyboard\" | null>(null);\n  const [live, setLive] = useState(\"\");\n  const drag = useRef<{ id: string; grabY: number; pointer: number } | null>(null);\n  const list = preview ?? items;\n  useFlip(root, list.map(getId).join(\"|\"));\n\n  const total = n(list.length);\n  const say = (fn: (item: string, pos: string, total: string) => string, item: T, pos: number) => setLive(fn(getLabel(item), n(pos + 1), total));\n\n  const finish = (commit: boolean) => {\n    const current = preview;\n    const it = current?.find((x) => getId(x) === active);\n    if (it && current) {\n      if (commit) {\n        onReorder(current);\n        say(S.dropped, it, current.indexOf(it));\n      } else setLive(S.cancelled(getLabel(it)));\n    }\n    const el = active ? root.current?.querySelector<HTMLElement>(`[data-flip=\"${CSS.escape(active)}\"]`) : null;\n    if (el) el.style.translate = \"\";\n    drag.current = null;\n    setPreview(null);\n    setActive(null);\n    setMode(null);\n  };\n\n  const finishRef = useRef(finish);\n  finishRef.current = finish;\n  const refocusing = useRef(false);\n  useIsomorphicLayoutEffect(() => {\n    if (mode !== \"keyboard\" || !active) return;\n    const el = root.current?.querySelector<HTMLElement>(`[data-flip=\"${CSS.escape(active)}\"] .k-sortable__handle`);\n    if (el && document.activeElement !== el) el.focus();\n    refocusing.current = false;\n  }, [preview, mode, active]);\n  // اگر فوکوس از فهرست بیرون رفت، جابه‌جایی کیبوردی لغو می‌شود (جابه‌جایی DOM خودش blur موقت می‌سازد)\n  const onBlur = () =>\n    setTimeout(() => {\n      if (!refocusing.current && !root.current?.contains(document.activeElement)) finishRef.current(false);\n    }, 0);\n\n  // جای مورد کشیده‌شده زیر اشاره‌گر\n  const follow = (clientY: number) => {\n    const d = drag.current;\n    const el = d && root.current?.querySelector<HTMLElement>(`[data-flip=\"${CSS.escape(d.id)}\"]`);\n    if (!d || !el || !preview) return;\n    const base = el.getBoundingClientRect().top - (parseFloat(el.style.translate.split(\" \")[1] ?? \"0\") || 0);\n    el.style.translate = `0 ${clientY - d.grabY - base}px`;\n    // جای تازه: مورد زیر مرکز مورد کشیده‌شده\n    const center = clientY - d.grabY + el.offsetHeight / 2;\n    const others = Array.from(root.current!.querySelectorAll<HTMLElement>(\"[data-flip]\"));\n    let to = 0;\n    for (const o of others) {\n      if (o === el) continue;\n      const r = o.getBoundingClientRect();\n      const top = r.top - (parseFloat(o.style.translate.split(\" \")[1] ?? \"0\") || 0);\n      if (center > top + r.height / 2) to++;\n    }\n    const from = preview.findIndex((x) => getId(x) === d.id);\n    if (to !== from) setPreview(move(preview, from, to));\n  };\n\n  const onPointerDown = (e: PointerEvent<HTMLButtonElement>, item: T) => {\n    if (disabled || e.button !== 0) return;\n    const el = (e.currentTarget.closest(\"[data-flip]\") as HTMLElement)!;\n    e.currentTarget.setPointerCapture(e.pointerId);\n    drag.current = { id: getId(item), grabY: e.clientY - el.getBoundingClientRect().top, pointer: e.pointerId };\n    setPreview(items);\n    setActive(getId(item));\n    setMode(\"pointer\");\n  };\n\n  const onKeyDown = (e: KeyboardEvent<HTMLButtonElement>, item: T) => {\n    if (disabled) return;\n    const id = getId(item);\n    if (e.key === \" \" || e.key === \"Enter\") {\n      e.preventDefault();\n      if (active === id && mode === \"keyboard\") finish(true);\n      else {\n        setPreview(items);\n        setActive(id);\n        setMode(\"keyboard\");\n        say(S.grabbed, item, items.indexOf(item));\n      }\n      return;\n    }\n    if (active !== id || mode !== \"keyboard\" || !preview) return;\n    if (e.key === \"Escape\") {\n      e.preventDefault();\n      finish(false);\n      return;\n    }\n    const from = preview.findIndex((x) => getId(x) === id);\n    const to = e.key === \"ArrowUp\" || e.key === \"ArrowLeft\" ? from - 1 : e.key === \"ArrowDown\" || e.key === \"ArrowRight\" ? from + 1 : e.key === \"Home\" ? 0 : e.key === \"End\" ? preview.length - 1 : -2;\n    if (to === -2) return;\n    e.preventDefault();\n    if (to < 0 || to >= preview.length || to === from) return;\n    const next = move(preview, from, to);\n    refocusing.current = true;\n    setPreview(next);\n    say(S.moved, item, to);\n  };\n\n  return (\n    <>\n      <ul ref={root} className={cx(\"k-sortable\", className)} aria-label={ariaLabel} data-dragging={active ? \"\" : undefined}>\n        {list.map((item, index) => {\n          const id = getId(item);\n          const dragging = active === id;\n          const handle = (\n            <button\n              type=\"button\"\n              className=\"k-sortable__handle\"\n              aria-label={S.handle(getLabel(item))}\n              aria-pressed={dragging && mode === \"keyboard\"}\n              title={S.instructions}\n              disabled={disabled}\n              onPointerDown={(e) => onPointerDown(e, item)}\n              onPointerMove={(e) => drag.current?.pointer === e.pointerId && follow(e.clientY)}\n              onPointerUp={(e) => drag.current?.pointer === e.pointerId && finish(true)}\n              onPointerCancel={() => drag.current && finish(false)}\n              onKeyDown={(e) => onKeyDown(e, item)}\n              onBlur={() => mode === \"keyboard\" && dragging && onBlur()}\n            >\n              <Grip />\n            </button>\n          );\n          return (\n            <li key={id} className=\"k-sortable__item\" data-flip={id} data-dragging={dragging ? \"\" : undefined} data-mode={dragging ? mode ?? undefined : undefined}>\n              {renderItem ? renderItem(item, { handle, dragging, index }) : (<>{handle}<span className=\"k-sortable__label\">{getLabel(item)}</span></>)}\n            </li>\n          );\n        })}\n      </ul>\n      <span className=\"k-sr-only\" aria-live=\"assertive\">{live}</span>\n    </>\n  );\n}\n"
    }
  ]
}