{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "kanban",
  "type": "registry:ui",
  "title": "Kanban",
  "description": "Cards in columns (to do, doing, done) that move between and within columns three ways: dragged with a mouse, lifted with Space and carried with the arrow keys, or sent by each card's \"Move to\" menu; every move is said aloud.",
  "categories": [
    "ui"
  ],
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/announce",
    "@rivier-ui/cn",
    "@rivier-ui/direction",
    "@rivier-ui/dropdown-menu",
    "@rivier-ui/use-controllable-state"
  ],
  "files": [
    {
      "path": "registry/ui/kanban.tsx",
      "type": "registry:ui",
      "target": "components/ui/kanban.tsx",
      "content": "\"use client\";\n/**\n * @name kanban\n * @category ui\n * @description Cards in columns (to do, doing, done) that move between and within columns three ways: dragged with a mouse, lifted with Space and carried with the arrow keys, or sent by each card's \"Move to\" menu; every move is said aloud.\n * @when Work that passes through stages: tasks, applications, orders. For one ordered list use sortable-list; for a schedule over time use gantt-chart; for records to sort and filter use data-grid.\n * @example\n * <Kanban\n *   aria-label=\"Tasks\"\n *   columns={[{ id: \"todo\", title: \"To do\" }, { id: \"doing\", title: \"Doing\" }, { id: \"done\", title: \"Done\" }]}\n *   defaultCards={[{ id: \"1\", column: \"todo\", title: \"Write the brief\" }, { id: \"2\", column: \"doing\", title: \"Draw the map\" }]}\n * />\n */\nimport { MoreHorizontal } from \"lucide-react\";\nimport { useRef, useState, type ComponentProps, type DragEvent, type KeyboardEvent, type ReactNode } from \"react\";\nimport { announce } from \"@/lib/announce\";\nimport { cn } from \"@/lib/cn\";\nimport { useControllableState } from \"@/lib/use-controllable-state\";\nimport { inlineStep } from \"@/lib/direction\";\nimport { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuTrigger } from \"@/components/ui/dropdown-menu\";\n\nexport type KanbanColumn = { id: string; title: string };\nexport type KanbanCard = { id: string; column: string; title: string; description?: string };\n\nexport type KanbanLabels = {\n  lifted: (card: string, column: string, position: number, count: number) => string;\n  moved: (card: string, column: string, position: number, count: number) => string;\n  dropped: (card: string, column: string, position: number, count: number) => string;\n  cancelled: (card: string) => string;\n  moveTo: string;\n  moveMenu: (card: string) => string;\n  instructions: string;\n  count: (count: number) => string;\n  /** What a card is called, for screen readers. */\n  card: string;\n  /** Added to a card's name while it is lifted. */\n  liftedState: string;\n};\n\nconst LABELS: KanbanLabels = {\n  lifted: (card, column, position, count) => `${card} lifted, in ${column}, ${position} of ${count}. Arrow keys move it, Space drops it, Escape puts it back.`,\n  moved: (card, column, position, count) => `${card}: ${column}, ${position} of ${count}`,\n  dropped: (card, column, position, count) => `${card} dropped in ${column}, ${position} of ${count}`,\n  cancelled: (card) => `${card} put back`,\n  moveTo: \"Move to\",\n  moveMenu: (card) => `Move ${card}`,\n  instructions: \"Space lifts a card to move it.\",\n  count: (count) => `${count} ${count === 1 ? \"card\" : \"cards\"}`,\n  card: \"card\",\n  liftedState: \"lifted\",\n};\n\nexport type KanbanProps = Omit<ComponentProps<\"div\">, \"defaultValue\" | \"onChange\"> & {\n  columns: readonly KanbanColumn[];\n  /** The cards, each with its column; their order in the list is their order in the column. Controlled with `onCardsChange`. */\n  cards?: readonly KanbanCard[];\n  defaultCards?: readonly KanbanCard[];\n  onCardsChange?: (cards: readonly KanbanCard[]) => void;\n  /** Draws a card's body; the default is its title and description. */\n  renderCard?: (card: KanbanCard) => ReactNode;\n  labels?: Partial<KanbanLabels>;\n};\n\n/** Moves a card to a column at a place in it (0-based), keeping every other card's order. */\nfunction place(cards: readonly KanbanCard[], id: string, column: string, index: number): KanbanCard[] {\n  const card = cards.find((c) => c.id === id);\n  if (!card) return [...cards];\n  const rest = cards.filter((c) => c.id !== id);\n  const inColumn = rest.filter((c) => c.column === column);\n  const anchor = inColumn[index];\n  const moved = { ...card, column };\n  if (!anchor) {\n    const last = inColumn[inColumn.length - 1];\n    const at = last ? rest.indexOf(last) + 1 : rest.length;\n    return [...rest.slice(0, at), moved, ...rest.slice(at)];\n  }\n  const at = rest.indexOf(anchor);\n  return [...rest.slice(0, at), moved, ...rest.slice(at)];\n}\n\nexport function Kanban({ columns, cards: cardsProp, defaultCards = [], onCardsChange, renderCard, labels: given, className, ...props }: KanbanProps) {\n  const labels = { ...LABELS, ...given };\n  const [cards, setCards] = useControllableState<readonly KanbanCard[]>({ value: cardsProp, defaultValue: defaultCards, onChange: onCardsChange });\n  const [lifted, setLifted] = useState<{ id: string; before: readonly KanbanCard[] } | null>(null);\n  const [dragging, setDragging] = useState<string | null>(null);\n  const [over, setOver] = useState<string | null>(null);\n  const refs = useRef(new Map<string, HTMLDivElement | null>());\n  const titleOf = (column: string): string => columns.find((c) => c.id === column)?.title ?? column;\n  const where = (list: readonly KanbanCard[], id: string): { column: string; index: number; count: number } => {\n    const card = list.find((c) => c.id === id);\n    const inColumn = list.filter((c) => c.column === card?.column);\n    return { column: card?.column ?? \"\", index: inColumn.findIndex((c) => c.id === id), count: inColumn.length };\n  };\n  const refocus = (id: string): void => {\n    requestAnimationFrame(() => refs.current.get(id)?.focus());\n  };\n  const say = (kind: \"moved\" | \"dropped\", list: readonly KanbanCard[], id: string): void => {\n    const card = list.find((c) => c.id === id);\n    const at = where(list, id);\n    announce(labels[kind](card?.title ?? \"\", titleOf(at.column), at.index + 1, at.count));\n  };\n  const moveTo = (id: string, column: string, index: number, kind: \"moved\" | \"dropped\"): void => {\n    const next = place(cards, id, column, index);\n    setCards(next);\n    say(kind, next, id);\n    refocus(id);\n  };\n\n  const onCardKeyDown = (card: KanbanCard, event: KeyboardEvent<HTMLDivElement>): void => {\n    if (event.target !== event.currentTarget) return;\n    const at = where(cards, card.id);\n    if (event.key === \" \" || event.key === \"Enter\") {\n      event.preventDefault();\n      if (lifted?.id === card.id) {\n        setLifted(null);\n        say(\"dropped\", cards, card.id);\n      } else {\n        setLifted({ id: card.id, before: cards });\n        announce(labels.lifted(card.title, titleOf(at.column), at.index + 1, at.count));\n      }\n      return;\n    }\n    if (event.key === \"Escape\" && lifted?.id === card.id) {\n      event.preventDefault();\n      setCards(lifted.before);\n      setLifted(null);\n      announce(labels.cancelled(card.title));\n      refocus(card.id);\n      return;\n    }\n    const columnIndex = columns.findIndex((c) => c.id === at.column);\n    const step = inlineStep(event.key, event.currentTarget);\n    if (lifted?.id === card.id) {\n      if (event.key === \"ArrowUp\" && at.index > 0) moveTo(card.id, at.column, at.index - 1, \"moved\");\n      else if (event.key === \"ArrowDown\" && at.index < at.count - 1) moveTo(card.id, at.column, at.index + 1, \"moved\");\n      else if (step !== 0 && columns[columnIndex + step]) {\n        const target = columns[columnIndex + step]?.id ?? at.column;\n        moveTo(card.id, target, Math.min(at.index, cards.filter((c) => c.column === target).length), \"moved\");\n      } else return;\n      event.preventDefault();\n      return;\n    }\n    // Not lifted: the arrow keys move focus between cards.\n    const inColumn = cards.filter((c) => c.column === at.column);\n    let target: KanbanCard | undefined;\n    if (event.key === \"ArrowUp\") target = inColumn[at.index - 1];\n    else if (event.key === \"ArrowDown\") target = inColumn[at.index + 1];\n    else if (step !== 0) {\n      for (let i = columnIndex + step; i >= 0 && i < columns.length; i += step) {\n        const list = cards.filter((c) => c.column === columns[i]?.id);\n        if (list.length > 0) {\n          target = list[Math.min(at.index, list.length - 1)];\n          break;\n        }\n      }\n    }\n    if (!target) return;\n    event.preventDefault();\n    refs.current.get(target.id)?.focus();\n  };\n\n  const onDrop = (column: string, event: DragEvent<HTMLElement>): void => {\n    event.preventDefault();\n    const id = event.dataTransfer.getData(\"text/plain\") || dragging;\n    setOver(null);\n    setDragging(null);\n    if (!id) return;\n    // Dropped among the cards: before the first card whose middle is below the pointer.\n    const list = cards.filter((c) => c.column === column && c.id !== id);\n    const index = list.findIndex((c) => {\n      const box = refs.current.get(c.id)?.getBoundingClientRect();\n      return box ? event.clientY < box.top + box.height / 2 : false;\n    });\n    moveTo(id, column, index < 0 ? list.length : index, \"dropped\");\n  };\n\n  return (\n    <div data-slot=\"kanban\" role=\"group\" className={cn(\"flex w-full min-w-0 gap-3 overflow-x-auto pb-2\", className)} {...props}>\n      <p className=\"sr-only\">{labels.instructions}</p>\n      {columns.map((column) => {\n        const list = cards.filter((c) => c.column === column.id);\n        return (\n          <section\n            key={column.id}\n            data-slot=\"kanban-column\"\n            aria-label={column.title}\n            className={cn(\"flex w-64 shrink-0 flex-col gap-2 rounded-surface border bg-muted p-2 @md:w-72\", over === column.id && \"outline-2 outline-ring\")}\n            onDragOver={(event) => {\n              event.preventDefault();\n              setOver(column.id);\n            }}\n            onDragLeave={() => setOver((current) => (current === column.id ? null : current))}\n            onDrop={(event) => onDrop(column.id, event)}\n          >\n            <h3 className=\"flex items-center justify-between px-1 text-sm font-medium\">\n              {column.title}\n              <span className=\"text-xs font-normal tabular-nums text-muted-foreground\">{labels.count(list.length)}</span>\n            </h3>\n            <ul className=\"flex min-h-12 flex-col gap-2\">\n              {list.map((card) => {\n                const isLifted = lifted?.id === card.id;\n                return (\n                  <li key={card.id}>\n                    <div\n                      ref={(node) => {\n                        refs.current.set(card.id, node);\n                      }}\n                      data-slot=\"kanban-card\"\n                      data-lifted={isLifted ? \"\" : undefined}\n                      tabIndex={0}\n                      role=\"group\"\n                      aria-roledescription={labels.card}\n                      aria-label={isLifted ? `${card.title}, ${labels.liftedState}` : card.title}\n                      draggable\n                      onDragStart={(event) => {\n                        event.dataTransfer.setData(\"text/plain\", card.id);\n                        event.dataTransfer.effectAllowed = \"move\";\n                        setDragging(card.id);\n                      }}\n                      onDragEnd={() => setDragging(null)}\n                      onKeyDown={(event) => onCardKeyDown(card, event)}\n                      onBlur={() => {\n                        if (isLifted) setLifted(null);\n                      }}\n                      className={cn(\n                        \"flex items-start gap-2 rounded-surface border bg-card p-3 text-sm transition-transform duration-instant ease-out motion-reduce:transition-none\",\n                        isLifted && \"-translate-y-0.5 border-border-strong outline-2 outline-dashed outline-ring\",\n                        dragging === card.id && \"opacity-50\",\n                      )}\n                    >\n                      <div className=\"min-w-0 flex-1\">\n                        {renderCard ? (\n                          renderCard(card)\n                        ) : (\n                          <>\n                            <p className=\"font-medium\" dir=\"auto\">\n                              {card.title}\n                            </p>\n                            {card.description ? (\n                              <p className=\"mt-1 text-muted-foreground\" dir=\"auto\">\n                                {card.description}\n                              </p>\n                            ) : null}\n                          </>\n                        )}\n                      </div>\n                      <DropdownMenu>\n                        <DropdownMenuTrigger\n                          aria-label={labels.moveMenu(card.title)}\n                          className=\"-me-1 -mt-1 flex size-8 shrink-0 items-center justify-center rounded-control text-muted-foreground hover:bg-accent pointer-coarse:size-10\"\n                        >\n                          <MoreHorizontal aria-hidden=\"true\" className=\"size-4\" />\n                        </DropdownMenuTrigger>\n                        <DropdownMenuContent align=\"end\">\n                          <DropdownMenuLabel>{labels.moveTo}</DropdownMenuLabel>\n                          {columns\n                            .filter((c) => c.id !== card.column)\n                            .map((c) => (\n                              <DropdownMenuItem key={c.id} onSelect={() => moveTo(card.id, c.id, cards.filter((x) => x.column === c.id).length, \"dropped\")}>\n                                {c.title}\n                              </DropdownMenuItem>\n                            ))}\n                        </DropdownMenuContent>\n                      </DropdownMenu>\n                    </div>\n                  </li>\n                );\n              })}\n            </ul>\n          </section>\n        );\n      })}\n    </div>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "Work that passes through stages: tasks, applications, orders. For one ordered list use sortable-list; for a schedule over time use gantt-chart; for records to sort and filter use data-grid.",
    "example": "<Kanban\n  aria-label=\"Tasks\"\n  columns={[{ id: \"todo\", title: \"To do\" }, { id: \"doing\", title: \"Doing\" }, { id: \"done\", title: \"Done\" }]}\n  defaultCards={[{ id: \"1\", column: \"todo\", title: \"Write the brief\" }, { id: \"2\", column: \"doing\", title: \"Draw the map\" }]}\n/>",
    "client": true
  }
}
