{
  "name": "ai-handoff",
  "type": "registry:component",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "files": [
    {
      "type": "registry:component",
      "content": "\"use client\";\n\n/**\n * @author: @kokonutui\n * @description: AI Handoff\n * @version: 1.1.0\n * @date: 2026-11-05\n * @license: MIT\n * @website: https://kokonutui.com\n * @github: https://github.com/kokonut-labs/kokonutui\n */\n\nimport { Headset } from \"lucide-react\";\nimport { AnimatePresence, MotionConfig, motion } from \"motion/react\";\nimport { type ReactNode, useEffect, useId, useRef, useState } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface HandoffAgent {\n  name: string;\n  initials: string;\n  /** Tailwind classes for the avatar's colours. */\n  tone: string;\n}\n\n/** Where the handoff is, from your support queue. */\nexport type HandoffStatus = \"waiting\" | \"joined\";\n\nexport interface AIHandoffProps {\n  /** The user's message, drawn as a bubble. Only the demo draws one by default. */\n  prompt?: string;\n  /** What the assistant says before handing over. */\n  message?: string;\n  team?: string;\n  /** What the person taking over gets, so nobody asks twice. */\n  shared?: string[];\n  /** The team. The first one is who picks it up, so put them first. */\n  agents?: HandoffAgent[];\n  /** Place in line. Live, pass each new place; the demo counts down from it. */\n  position?: number;\n  /**\n   * Pass it to drive the card from your queue: `waiting` shows `position`,\n   * `joined` brings the first agent in. Leave it out and the demo plays on\n   * `stepMs`.\n   */\n  status?: HandoffStatus;\n  /**\n   * The agent's first message. Live, they show as typing until you pass it;\n   * the demo types for a moment, then shows it.\n   */\n  reply?: string;\n  /** One line under the team while waiting. */\n  waitHint?: string;\n  /** Demo only: ms per place in line. */\n  stepMs?: number;\n  /** Shows \"Keep chatting with AI\" while waiting. */\n  onStay?: () => void;\n  className?: string;\n}\n\ntype Status = \"waiting\" | \"picking\" | \"joined\" | \"stayed\";\n\nconst EASE_OUT = [0.23, 1, 0.32, 1] as const;\nconst EASE_IN_OUT = [0.77, 0, 0.175, 1] as const;\nconst EASE_DRAWER = [0.32, 0.72, 0, 1] as const;\nconst TYPING_MS = 1600;\n/** How long the picked agent is shown before they join. */\nconst PICK_MS = 900;\n/** How long the agent avatar takes to fly into the joined line, in s. */\nconst FLIGHT_S = 0.5;\n\nconst DEMO_AGENTS: HandoffAgent[] = [\n  {\n    name: \"Nora\",\n    initials: \"N\",\n    tone: \"bg-rose-100 text-rose-800 dark:bg-rose-500/20 dark:text-rose-200\",\n  },\n  {\n    name: \"Theo\",\n    initials: \"T\",\n    tone: \"bg-sky-100 text-sky-800 dark:bg-sky-500/20 dark:text-sky-200\",\n  },\n  {\n    name: \"Ama\",\n    initials: \"A\",\n    tone: \"bg-amber-100 text-amber-800 dark:bg-amber-500/20 dark:text-amber-200\",\n  },\n];\n\nconst DEMO_SHARED = [\n  \"Two charges of $24.00 on Nov 3\",\n  \"Invoice INV-2291, card ending 4242\",\n  \"You’d like one of them refunded\",\n];\n\nconst place = (n: number) => {\n  if (n <= 1) {\n    return \"Next in line\";\n  }\n  const suffix = { 2: \"nd\", 3: \"rd\" }[n] ?? \"th\";\n  return `${n}${suffix} in line`;\n};\n\nconst DEMO = {\n  prompt: \"I got charged twice for October\",\n  message:\n    \"I can see both charges. Refunds need someone on the billing team, so I’m handing you over with the details.\",\n  reply:\n    \"Hi Mara, Nora here. I can see the duplicate and I’ve refunded $24.00. It’ll be back on your card in 3 to 5 days.\",\n};\n\n/** The chat text around the card: the demo's only when it's the demo. */\nfunction shownText(\n  text: Pick<AIHandoffProps, \"prompt\" | \"message\" | \"reply\">,\n  isDemo: boolean\n) {\n  if (!isDemo) {\n    return text;\n  }\n  return {\n    prompt: text.prompt ?? DEMO.prompt,\n    message: text.message ?? DEMO.message,\n    reply: text.reply ?? DEMO.reply,\n  };\n}\n\n/**\n * Where the handoff is. Live it follows `status` and `position`; the demo\n * moves up the line on `stepMs`. Either way the first agent is shown picking\n * it up for a moment before they join.\n */\nfunction useQueue(\n  statusProp: HandoffStatus | undefined,\n  position: number,\n  stepMs: number\n) {\n  const driven = statusProp !== undefined;\n  const [status, setStatus] = useState<Status>(\"waiting\");\n  const [counted, setLine] = useState(position);\n  const line = driven ? position : counted;\n\n  // Live: the queue says they've joined, so they're picked, then join.\n  useEffect(() => {\n    if (statusProp === \"joined\" && status === \"waiting\") {\n      setStatus(\"picking\");\n    }\n  }, [statusProp, status]);\n\n  // Demo: moves up the line, then the first agent joins.\n  useEffect(() => {\n    if (status !== \"waiting\" || driven) {\n      return;\n    }\n    const t = setTimeout(() => {\n      if (line > 1) {\n        setLine((n) => n - 1);\n      } else {\n        setStatus(\"picking\");\n      }\n    }, stepMs);\n    return () => clearTimeout(t);\n  }, [status, line, stepMs, driven]);\n\n  // Holds on who picked it up, then they join.\n  useEffect(() => {\n    if (status !== \"picking\") {\n      return;\n    }\n    const t = setTimeout(() => setStatus(\"joined\"), PICK_MS);\n    return () => clearTimeout(t);\n  }, [status]);\n\n  return { status, setStatus, line };\n}\n\nexport default function AIHandoff({\n  prompt,\n  message,\n  team = \"Billing\",\n  shared = DEMO_SHARED,\n  agents = DEMO_AGENTS,\n  position = 3,\n  status: statusProp,\n  reply,\n  waitHint = \"Usually replies in about 2 min\",\n  stepMs = 1300,\n  onStay,\n  className,\n}: AIHandoffProps) {\n  const id = useId();\n  const driven = statusProp !== undefined;\n  const shown = shownText({ prompt, message, reply }, !driven);\n  const { status, setStatus, line } = useQueue(statusProp, position, stepMs);\n  const [typingDemo, setTyping] = useState(true);\n  // Live, the agent types until you pass their first message.\n  const typing = driven ? shown.reply === undefined : typingDemo;\n  const [landed, setLanded] = useState(false);\n  const agent = agents[0];\n  const picking = status === \"picking\";\n  const open = status === \"waiting\" || picking;\n\n  // They type for a moment before the first message lands.\n  useEffect(() => {\n    if (status !== \"joined\") {\n      return;\n    }\n    const land = setTimeout(() => setLanded(true), FLIGHT_S * 1000 + 50);\n    const t = setTimeout(() => setTyping(false), TYPING_MS);\n    return () => {\n      clearTimeout(land);\n      clearTimeout(t);\n    };\n  }, [status]);\n\n  const stay = () => {\n    setStatus(\"stayed\");\n    onStay?.();\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div\n        className={cn(\"flex w-full max-w-md flex-col gap-4\", className)}\n        data-state={status}\n      >\n        {shown.prompt ? (\n          <p className=\"max-w-[85%] self-end rounded-2xl rounded-br-md bg-zinc-100 px-3.5 py-2 text-[14px] text-zinc-800 leading-snug dark:bg-zinc-800 dark:text-zinc-100\">\n            {shown.prompt}\n          </p>\n        ) : null}\n        {shown.message ? (\n          <p className=\"text-[15px] text-zinc-800 leading-7 dark:text-zinc-200\">\n            {shown.message}\n          </p>\n        ) : null}\n\n        <AutoHeight className=\"rounded-[22px] bg-black/[0.04] dark:bg-white/[0.06]\">\n          <AnimatePresence initial={false} mode=\"popLayout\">\n            {open ? (\n              <motion.div\n                exit={{ opacity: 0, filter: \"blur(4px)\" }}\n                key=\"card\"\n                transition={{ duration: 0.2 }}\n              >\n                <div className=\"flex h-8 items-center gap-2 px-3 text-[12px] text-zinc-500 dark:text-zinc-400\">\n                  <Headset aria-hidden=\"true\" className=\"size-3.5\" />\n                  <span className=\"min-w-0 truncate\">\n                    Handing over to{\" \"}\n                    <span className=\"font-medium text-zinc-700 dark:text-zinc-300\">\n                      {team}\n                    </span>\n                  </span>\n                  <span className=\"relative ml-auto h-4 shrink-0 overflow-hidden tabular-nums\">\n                    <AnimatePresence initial={false} mode=\"popLayout\">\n                      <motion.span\n                        animate={{ y: 0, opacity: 1 }}\n                        className=\"block leading-4\"\n                        exit={{ y: -10, opacity: 0 }}\n                        initial={{ y: 10, opacity: 0 }}\n                        key={picking ? \"picked\" : line}\n                        transition={{ duration: 0.25, ease: EASE_OUT }}\n                      >\n                        {picking ? `${agent.name} is joining` : place(line)}\n                      </motion.span>\n                    </AnimatePresence>\n                  </span>\n                </div>\n\n                <div className=\"rounded-[18px] bg-white shadow-[0_1px_2px_rgb(0_0_0/0.04)] ring-1 ring-black/[0.06] dark:bg-zinc-900 dark:ring-white/[0.08]\">\n                  <p className=\"px-3.5 pt-3 font-medium text-[11.5px] text-zinc-400 dark:text-zinc-500\">\n                    They’ll see this, so you won’t repeat yourself\n                  </p>\n                  <ul className=\"space-y-1.5 px-3.5 pt-2 pb-3 text-[13px]\">\n                    {shared.map((item, i) => (\n                      <li\n                        className=\"flex items-center gap-2.5 text-zinc-700 dark:text-zinc-300\"\n                        key={item}\n                      >\n                        <DrawnCheck delay={0.25 + i * 0.22} />\n                        <span className=\"min-w-0 truncate\">{item}</span>\n                      </li>\n                    ))}\n                  </ul>\n                  <div className=\"flex items-center gap-2.5 border-black/[0.06] border-t px-3.5 py-2.5 dark:border-white/[0.08]\">\n                    <span className=\"flex\">\n                      {agents.map((a, i) => (\n                        <motion.span\n                          animate={{ opacity: picking && i > 0 ? 0.25 : 1 }}\n                          className={cn(\n                            \"-ml-1 flex size-6 items-center justify-center rounded-full font-semibold text-[10px] ring-2 transition-[box-shadow] duration-200 first:ml-0\",\n                            picking && i === 0\n                              ? \"ring-zinc-900 dark:ring-zinc-100\"\n                              : \"ring-white dark:ring-zinc-900\",\n                            a.tone\n                          )}\n                          initial={false}\n                          key={a.name}\n                          layoutId={i === 0 ? `${id}-agent` : undefined}\n                          style={{ zIndex: agents.length - i }}\n                          title={a.name}\n                          transition={{ duration: 0.3, ease: EASE_OUT }}\n                        >\n                          {a.initials}\n                        </motion.span>\n                      ))}\n                    </span>\n                    <span className=\"min-w-0 flex-1 truncate text-[12px] text-zinc-500 dark:text-zinc-400\">\n                      {picking ? `${agent.name} picked it up` : waitHint}\n                    </span>\n                  </div>\n                </div>\n\n                <div\n                  className={cn(\n                    \"flex justify-end px-1 pt-1\",\n                    driven && !onStay && \"hidden\"\n                  )}\n                >\n                  <button\n                    className=\"h-8 rounded-full px-3 font-medium text-[12.5px] text-zinc-600 outline-hidden transition-colors duration-150 hover:bg-black/[0.04] hover:text-zinc-900 focus-visible:ring-2 focus-visible:ring-blue-500/40 active:bg-black/[0.07] disabled:opacity-50 dark:text-zinc-400 dark:active:bg-white/[0.1] dark:hover:bg-white/[0.06] dark:hover:text-zinc-100\"\n                    disabled={picking}\n                    onClick={stay}\n                    type=\"button\"\n                  >\n                    Keep chatting with AI\n                  </button>\n                </div>\n              </motion.div>\n            ) : (\n              <motion.div\n                animate={{ opacity: 1, filter: \"blur(0px)\" }}\n                className=\"flex h-9 items-center gap-2 px-3 text-[12.5px] text-zinc-500 dark:text-zinc-400\"\n                initial={{ opacity: 0, filter: \"blur(3px)\" }}\n                key=\"folded\"\n                transition={{ duration: 0.3, ease: EASE_OUT, delay: 0.1 }}\n              >\n                <Headset aria-hidden=\"true\" className=\"size-3.5\" />\n                <span className=\"min-w-0 truncate\">\n                  {status === \"joined\"\n                    ? `Handed over to ${team} with ${shared.length} details`\n                    : \"Handoff cancelled\"}\n                </span>\n              </motion.div>\n            )}\n          </AnimatePresence>\n        </AutoHeight>\n\n        <AnimatePresence initial={false} mode=\"popLayout\">\n          {status === \"joined\" && (\n            <motion.div\n              animate={{ opacity: 1 }}\n              className=\"flex flex-col gap-3\"\n              initial={{ opacity: 0 }}\n              key=\"joined\"\n              transition={{ duration: 0.2 }}\n            >\n              <div className=\"flex items-center gap-3 text-[12px] text-zinc-500 dark:text-zinc-400\">\n                <span className=\"h-px flex-1 bg-black/[0.08] dark:bg-white/10\" />\n                <span className=\"flex items-center gap-2\">\n                  <motion.span\n                    className={cn(\n                      \"flex size-5 items-center justify-center rounded-full font-semibold text-[9px]\",\n                      agent.tone\n                    )}\n                    // Shared only for the flight in; once landed it stays put\n                    // while the messages below it change height.\n                    layoutId={landed ? undefined : `${id}-agent`}\n                    transition={{ duration: FLIGHT_S, ease: EASE_IN_OUT }}\n                  >\n                    {agent.initials}\n                  </motion.span>\n                  <span>\n                    <span className=\"font-medium text-zinc-700 dark:text-zinc-300\">\n                      {agent.name}\n                    </span>{\" \"}\n                    from {team} joined\n                  </span>\n                </span>\n                <span className=\"h-px flex-1 bg-black/[0.08] dark:bg-white/10\" />\n              </div>\n\n              <motion.div\n                animate={{ opacity: 1, y: 0 }}\n                className=\"flex items-end gap-2\"\n                initial={{ opacity: 0, y: 6 }}\n                transition={{ duration: 0.3, ease: EASE_OUT, delay: 0.35 }}\n              >\n                <span\n                  className={cn(\n                    \"flex size-7 shrink-0 items-center justify-center rounded-full font-semibold text-[11px]\",\n                    agent.tone\n                  )}\n                >\n                  {agent.initials}\n                </span>\n                {/* Typing dots and the reply are two bubbles that crossfade,\n                    so the bubble never stretches its text mid-way. */}\n                <AnimatePresence initial={false} mode=\"popLayout\">\n                  {typing ? (\n                    <motion.span\n                      className=\"flex h-9 items-center gap-1 rounded-2xl rounded-bl-md bg-white px-3.5 ring-1 ring-black/[0.08] dark:bg-zinc-900 dark:ring-white/10\"\n                      exit={{ opacity: 0, filter: \"blur(2px)\" }}\n                      key=\"typing\"\n                      transition={{ duration: 0.15 }}\n                    >\n                      <span className=\"sr-only\">{agent.name} is typing</span>\n                      {[0, 1, 2].map((d) => (\n                        <motion.span\n                          animate={{ y: [0, -3, 0], opacity: [0.4, 1, 0.4] }}\n                          className=\"size-1.5 rounded-full bg-zinc-400 dark:bg-zinc-500\"\n                          key={d}\n                          transition={{\n                            duration: 0.9,\n                            delay: d * 0.15,\n                            repeat: Number.POSITIVE_INFINITY,\n                          }}\n                        />\n                      ))}\n                    </motion.span>\n                  ) : (\n                    <motion.p\n                      animate={{ opacity: 1, filter: \"blur(0px)\", y: 0 }}\n                      className=\"min-w-0 max-w-[85%] rounded-2xl rounded-bl-md bg-white px-3.5 py-2 text-[14px] text-zinc-800 leading-6 ring-1 ring-black/[0.08] dark:bg-zinc-900 dark:text-zinc-200 dark:ring-white/10\"\n                      initial={{ opacity: 0, filter: \"blur(4px)\", y: 4 }}\n                      key=\"reply\"\n                      transition={{ duration: 0.3, ease: EASE_OUT }}\n                    >\n                      {shown.reply}\n                    </motion.p>\n                  )}\n                </AnimatePresence>\n              </motion.div>\n            </motion.div>\n          )}\n\n          {status === \"stayed\" && (\n            <motion.p\n              animate={{ opacity: 1, filter: \"blur(0px)\", y: 0 }}\n              className=\"text-[15px] text-zinc-800 leading-7 dark:text-zinc-200\"\n              initial={{ opacity: 0, filter: \"blur(4px)\", y: 4 }}\n              key=\"stayed\"\n              transition={{ duration: 0.35, ease: EASE_OUT }}\n            >\n              Okay, I’ll stay with you. I can’t refund it myself, but I can\n              draft the request so it’s ready whenever you want to send it.\n            </motion.p>\n          )}\n        </AnimatePresence>\n        <p aria-live=\"polite\" className=\"sr-only\">\n          {status === \"waiting\" ? place(line) : \"\"}\n          {status === \"joined\" ? `${agent.name} from ${team} joined` : \"\"}\n        </p>\n      </div>\n    </MotionConfig>\n  );\n}\n\nfunction DrawnCheck({ delay }: { delay: number }) {\n  return (\n    <span className=\"flex size-4 shrink-0 items-center justify-center rounded-full bg-emerald-50 dark:bg-emerald-500/15\">\n      <svg\n        aria-hidden=\"true\"\n        className=\"size-2.5 text-emerald-600 dark:text-emerald-400\"\n        fill=\"none\"\n        viewBox=\"0 0 16 16\"\n      >\n        <motion.path\n          animate={{ pathLength: 1, opacity: 1 }}\n          d=\"M3.5 8.5l3 3 6-7\"\n          initial={{ pathLength: 0, opacity: 0 }}\n          stroke=\"currentColor\"\n          strokeLinecap=\"round\"\n          strokeLinejoin=\"round\"\n          strokeWidth=\"2.5\"\n          transition={{ duration: 0.3, ease: EASE_OUT, delay }}\n        />\n      </svg>\n    </span>\n  );\n}\n\n/** Eases the card to its content's height when the content swaps. */\nfunction AutoHeight({\n  className,\n  children,\n}: {\n  className?: string;\n  children: ReactNode;\n}) {\n  const innerRef = useRef<HTMLDivElement>(null);\n  const [height, setHeight] = useState<number | \"auto\">(\"auto\");\n\n  useEffect(() => {\n    const inner = innerRef.current;\n    if (!inner) {\n      return;\n    }\n    const observer = new ResizeObserver(() => setHeight(inner.offsetHeight));\n    observer.observe(inner);\n    return () => observer.disconnect();\n  }, []);\n\n  return (\n    <motion.div\n      animate={{ height }}\n      className={cn(\"overflow-hidden\", className)}\n      initial={false}\n      transition={{ duration: 0.3, ease: EASE_DRAWER }}\n    >\n      <div className=\"p-1\" ref={innerRef}>\n        {children}\n      </div>\n    </motion.div>\n  );\n}\n",
      "path": "/components/kokonutui/ai-handoff.tsx",
      "target": "components/kokonutui/ai-handoff.tsx"
    }
  ]
}