{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "taxonomy-depth-limit",
  "title": "Taxonomy Depth Limit",
  "description": "A sortable tree that refuses any move, by pointer or keyboard, that would make it deeper than the allowed number of levels, and says so. Use this when menus or categories must stay shallow.",
  "registryDependencies": [
    "@tealmac/sortable-tree",
    "@tealmac/tree-view"
  ],
  "files": [
    {
      "path": "registry/patterns/taxonomy-depth-limit/taxonomy-depth-limit.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { SortableTree } from \"@/components/ui/sortable-tree\"\nimport {\n  findPath,\n  type DropPosition,\n  type TreeNode,\n} from \"@/components/ui/tree-view\"\n\nexport interface TaxonomyDepthLimitProps {\n  nodes: TreeNode[]\n  /** The most levels allowed. 1 is a flat list, 3 is parent, child and grandchild. */\n  maxDepth: number\n  onNodesChange?: (nodes: TreeNode[]) => void\n}\n\n/** Levels in a subtree, counting its root. */\nexport function heightOf(node: TreeNode): number {\n  return 1 + Math.max(0, ...(node.children ?? []).map(heightOf))\n}\n\n/**\n * The depth the source's deepest descendant would reach after the move:\n * the target's own level (plus one when dropping inside it) plus the source's\n * height, counting levels from 1.\n */\nexport function depthAfterMove(\n  nodes: TreeNode[],\n  source: TreeNode,\n  target: TreeNode,\n  position: DropPosition\n) {\n  const targetLevel = findPath(nodes, target.id)?.length ?? 1\n  const level = position === \"inside\" ? targetLevel + 1 : targetLevel\n  return level + heightOf(source) - 1\n}\n\n/**\n * `canDrop` is the only rule: the drop line does not show where a move is\n * not allowed, and an Alt+arrow move that would break the limit is refused\n * with a spoken message. A badge shows the current depth.\n */\nexport function TaxonomyDepthLimit({\n  nodes: initial,\n  maxDepth,\n  onNodesChange,\n}: TaxonomyDepthLimitProps) {\n  const [nodes, setNodes] = React.useState(initial)\n  const depth = Math.max(...nodes.map(heightOf))\n  return (\n    <div className=\"flex w-full flex-col gap-2\">\n      <p className=\"text-muted-foreground text-xs\" aria-live=\"polite\">\n        Depth {depth} of {maxDepth}\n      </p>\n      <div className=\"rounded-md border p-1\">\n        <SortableTree\n          aria-label=\"Menu\"\n          nodes={nodes}\n          defaultExpanded={nodes.map((n) => n.id)}\n          onNodesChange={(next) => {\n            setNodes(next)\n            onNodesChange?.(next)\n          }}\n          canDrop={(source, target, position) =>\n            depthAfterMove(nodes, source, target, position) <= maxDepth\n          }\n        />\n      </div>\n    </div>\n  )\n}\n",
      "type": "registry:block"
    }
  ],
  "type": "registry:block"
}