{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "message-branch",
  "type": "registry:ui",
  "title": "Message branch",
  "description": "React version switcher for AI chat replies: step through regenerated answers with ‹ 2 / 3 › buttons or Alt+Arrow keys, with a cross-fade and live count.",
  "author": "MiniDev UI <https://ui.minidev.pro>",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "https://ui.minidev.pro/r/icon-button.json",
    "utils"
  ],
  "files": [
    {
      "path": "registry/ui/message-branch.tsx",
      "type": "registry:ui",
      "target": "components/ui/message-branch.tsx",
      "content": "\"use client\"\nimport * as React from \"react\"\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\"\nimport { ChevronLeftIcon, ChevronRightIcon } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport { IconButton } from \"@/components/ui/icon-button\"\n\n/**\n * MessageBranch — the \"‹ 2 / 3 ›\" switcher that moves between the versions of an AI reply after a regenerate or an edited prompt.\n *\n * Wrap the assistant reply: pass every version in `versions` (or as children) and it shows one, with a\n * compact selector underneath. The selector hides itself while there is only one version, and in uncontrolled\n * mode a newly added version is shown straight away. Put MessageActions in `actions` to get the selector and\n * the copy, retry and vote buttons on one row. For the \"Not quite right?\" footer use RegenerateBar.\n *\n * @example\n * <MessageBranch\n *   versions={replies.map((r) => <Markdown key={r.id}>{r.text}</Markdown>)}\n *   actions={<MessageActions onRetry={regenerate} />}\n * />\n */\n\nconst SNAPPY = { type: \"spring\", bounce: 0.18, duration: 0.42 } as const\nconst MORPH = { type: \"spring\", bounce: 0.16, duration: 0.5 } as const\nconst INSTANT = { duration: 0 } as const\n\ntype MessageBranchProps = {\n  /** Every version of the reply, oldest first. When omitted, each child is one version. */\n  versions?: React.ReactNode[]\n  /** Versions as children, used when `versions` is not passed. */\n  children?: React.ReactNode\n  /** Controlled index of the version shown (0-based). */\n  index?: number\n  /** Starting index when uncontrolled. Defaults to the newest version. */\n  defaultIndex?: number\n  /** Fires with the new 0-based index when the user switches version. */\n  onIndexChange?: (index: number) => void\n  /** Hide the selector while there is only one version. */\n  hideWhenSingle?: boolean\n  /** What one version is called in the screen reader text: \"Response 2 of 3\", \"Previous response\". */\n  label?: string\n  /** Rendered on the selector row after the switcher, for example MessageActions. */\n  actions?: React.ReactNode\n  className?: string\n}\n\nfunction MessageBranch({\n  versions: versionsProp,\n  children,\n  index: indexProp,\n  defaultIndex,\n  onIndexChange,\n  hideWhenSingle = true,\n  label = \"Response\",\n  actions,\n  className,\n}: MessageBranchProps) {\n  const reduce = useReducedMotion()\n  const versions = versionsProp ?? React.Children.toArray(children)\n  const count = versions.length\n  const [inner, setInner] = React.useState(defaultIndex ?? Math.max(count - 1, 0))\n  // Uncontrolled: a version added after a regenerate becomes the one shown.\n  const [seen, setSeen] = React.useState(count)\n  if (count !== seen) {\n    setSeen(count)\n    if (count > seen) setInner(count - 1)\n  }\n  const index = Math.min(Math.max(indexProp ?? inner, 0), Math.max(count - 1, 0))\n\n  // Direction of travel, kept in state so later re-renders do not reset it mid-exit.\n  const [nav, setNav] = React.useState({ at: index, dir: 0 })\n  if (nav.at !== index) setNav({ at: index, dir: index > nav.at ? 1 : -1 })\n  const dir = nav.at !== index ? (index > nav.at ? 1 : -1) : nav.dir\n\n  // Height follows the content. It springs only right after a switch, so streaming text grows without lag.\n  const measure = React.useRef<HTMLDivElement>(null)\n  const springUntil = React.useRef(0)\n  const [box, setBox] = React.useState<{ h: number; spring: boolean } | null>(null)\n  const mounted = React.useRef(false)\n  React.useLayoutEffect(() => {\n    if (mounted.current) springUntil.current = performance.now() + 500\n    mounted.current = true\n  }, [index])\n  React.useEffect(() => {\n    const el = measure.current\n    if (!el) return\n    const ro = new ResizeObserver(() => setBox({ h: el.offsetHeight, spring: performance.now() < springUntil.current }))\n    ro.observe(el)\n    return () => ro.disconnect()\n  }, [])\n\n  const go = (next: number) => {\n    if (next < 0 || next >= count || next === index) return\n    if (indexProp === undefined) setInner(next)\n    onIndexChange?.(next)\n  }\n\n  const showSelector = count > 1 || (!hideWhenSingle && count === 1)\n  const noun = label.toLowerCase()\n\n  return (\n    <div\n      data-slot=\"message-branch\"\n      className={cn(\"flex flex-col gap-2\", className)}\n      onKeyDown={(e) => {\n        if (!e.altKey || count < 2) return\n        // Alt+Arrow moves by word in text fields (macOS); leave it to them.\n        if ((e.target as HTMLElement).closest(\"input, textarea, [contenteditable='true']\")) return\n        if (e.key === \"ArrowLeft\" || e.key === \"ArrowRight\") {\n          // Also stops Alt+Left from navigating the browser back while focus is in the reply.\n          e.preventDefault()\n          go(index + (e.key === \"ArrowRight\" ? 1 : -1))\n        }\n      }}\n    >\n      <motion.div\n        className=\"-m-1 self-stretch overflow-hidden\"\n        initial={false}\n        animate={{ height: box ? box.h : \"auto\" }}\n        transition={reduce || !box?.spring ? INSTANT : MORPH}\n      >\n        <div ref={measure} className=\"relative p-1\">\n          <AnimatePresence mode=\"popLayout\" initial={false} custom={dir}>\n            <motion.div\n              key={index}\n              custom={dir}\n              variants={{\n                enter: (d: number) => ({ opacity: 0, x: d * 6 }),\n                center: { opacity: 1, x: 0 },\n                exit: (d: number) => ({ opacity: 0, x: d * -6 }),\n              }}\n              initial=\"enter\"\n              animate=\"center\"\n              exit=\"exit\"\n              transition={reduce ? INSTANT : SNAPPY}\n            >\n              {versions[index]}\n            </motion.div>\n          </AnimatePresence>\n        </div>\n      </motion.div>\n      {showSelector || actions ? (\n        <div className=\"flex items-center gap-1\">\n          {showSelector ? (\n            <div data-slot=\"message-branch-selector\" role=\"group\" aria-label={`${label} versions`} className=\"flex items-center\">\n              <IconButton\n                type=\"button\"\n                size=\"icon-sm\"\n                variant=\"ghost\"\n                aria-label={`Previous ${noun}`}\n                disabled={index <= 0}\n                focusableWhenDisabled\n                onClick={() => go(index - 1)}\n              >\n                <ChevronLeftIcon />\n              </IconButton>\n              <span aria-live=\"polite\" aria-atomic=\"true\" className=\"min-w-10 px-0.5 text-center text-xs text-fg-muted tabular-nums\">\n                <span aria-hidden>\n                  {index + 1} / {count}\n                </span>\n                <span className=\"sr-only\">\n                  {label} {index + 1} of {count}\n                </span>\n              </span>\n              <IconButton\n                type=\"button\"\n                size=\"icon-sm\"\n                variant=\"ghost\"\n                aria-label={`Next ${noun}`}\n                disabled={index >= count - 1}\n                focusableWhenDisabled\n                onClick={() => go(index + 1)}\n              >\n                <ChevronRightIcon />\n              </IconButton>\n            </div>\n          ) : null}\n          {actions}\n        </div>\n      ) : null}\n    </div>\n  )\n}\n\nexport { MessageBranch }\nexport type { MessageBranchProps }\n"
    }
  ],
  "categories": [
    "ai-chat"
  ],
  "docs": "import { MessageBranch } from \"@/components/ui/message-branch\"\nNeeds the MiniDev tokens stylesheet (https://ui.minidev.pro/r/styles.css) imported after Tailwind.\nDocs: https://ui.minidev.pro/docs/message-branch · Markdown for agents: https://ui.minidev.pro/docs/message-branch.md",
  "meta": {
    "tier": "free",
    "kind": "ui",
    "category": "ai-chat",
    "import": "@/components/ui/message-branch",
    "npmImport": "minidev-ui-kit/ui/message-branch",
    "exports": [
      "MessageBranch"
    ],
    "related": [
      "message-actions",
      "regenerate-bar"
    ],
    "docsUrl": "https://ui.minidev.pro/docs/message-branch",
    "markdownUrl": "https://ui.minidev.pro/docs/message-branch.md",
    "keywords": [
      "shadcn ai branch",
      "ai chat message branch react",
      "regenerate response versions react"
    ]
  }
}
