{
  "name": "ai-send",
  "type": "registry:component",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "files": [
    {
      "type": "registry:component",
      "content": "\"use client\";\n\n/**\n * @author: @kokonutui\n * @description: AI Send\n * @version: 1.1.0\n * @date: 2026-10-31\n * @license: MIT\n * @website: https://kokonutui.com\n * @github: https://github.com/kokonut-labs/kokonutui\n */\n\nimport { Check, Mail } from \"lucide-react\";\nimport { AnimatePresence, MotionConfig, motion } from \"motion/react\";\nimport {\n  type ReactNode,\n  type Ref,\n  useEffect,\n  useId,\n  useRef,\n  useState,\n} from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface DraftEmail {\n  from: string;\n  toName: string;\n  toEmail: string;\n  subject: string;\n  body: string;\n}\n\nexport interface AISendProps {\n  /** The user's message, drawn as a bubble. Only the demo draws one by default. */\n  prompt?: string;\n  /** What the assistant says above the draft. */\n  message?: string;\n  /**\n   * The email, usually a tool's input. It can stream in: the body follows\n   * it until the person edits it. Leave it out for the demo.\n   */\n  draft?: DraftEmail;\n  /** The model is still writing the draft, so Send waits. */\n  writing?: boolean;\n  /** Seconds you have to undo before it really sends. */\n  undoSeconds?: number;\n  /**\n   * Called once the undo window has passed, with the final text. Return a\n   * Promise to show it sending until it resolves; throw and the draft comes\n   * back with the error's message.\n   */\n  onSend?: (draft: DraftEmail) => void | Promise<void>;\n  className?: string;\n}\n\ntype Status = \"draft\" | \"pending\" | \"sending\" | \"sent\";\n\nconst EASE_OUT = [0.23, 1, 0.32, 1] as const;\nconst EASE_DRAWER = [0.32, 0.72, 0, 1] as const;\nconst RING = 2 * Math.PI * 8;\n\nconst DEMO_DRAFT: DraftEmail = {\n  from: \"mara@northwind.co\",\n  toName: \"Sam Rivera\",\n  toEmail: \"sam@northwind.co\",\n  subject: \"Launch review moved to Thursday\",\n  body: \"Hi Sam,\\n\\nQuick heads up: the launch review is now Thursday at 2pm, same room. Design needs the extra day to finish the flows.\\n\\nThanks,\\nMara\",\n};\n\nconst DEMO_TEXT = {\n  prompt: \"Tell Sam the launch review moved to Thursday at 2\",\n  message: \"Here’s a draft. Change anything you like before it goes.\",\n};\n\nconst reasonOf = (error: unknown) =>\n  error instanceof Error && error.message\n    ? error.message\n    : \"It didn’t send. Try again.\";\n\nexport default function AISend({\n  prompt,\n  message,\n  draft: draftProp,\n  writing = false,\n  undoSeconds = 5,\n  onSend,\n  className,\n}: AISendProps) {\n  const isDemo = draftProp === undefined;\n  const draft = draftProp ?? DEMO_DRAFT;\n  const shownPrompt = isDemo ? (prompt ?? DEMO_TEXT.prompt) : prompt;\n  const shownMessage = isDemo ? (message ?? DEMO_TEXT.message) : message;\n  const id = useId();\n  const [status, setStatus] = useState<Status>(\"draft\");\n  // Follows the draft as it streams in, until the person edits it.\n  const [edited, setEdited] = useState<string>();\n  const body = edited ?? draft.body;\n  const [left, setLeft] = useState(undoSeconds);\n  const [error, setError] = useState<string>();\n  const sendRef = useRef<HTMLButtonElement>(null);\n  const final = useRef<DraftEmail>(draft);\n  final.current = { ...draft, body };\n  const onSendRef = useRef(onSend);\n  onSendRef.current = onSend;\n\n  // Counts down the undo window, then sends for real.\n  useEffect(() => {\n    if (status !== \"pending\") {\n      return;\n    }\n    if (left > 0) {\n      const t = setTimeout(() => setLeft((n) => n - 1), 1000);\n      return () => clearTimeout(t);\n    }\n    setStatus(\"sending\");\n    Promise.resolve(onSendRef.current?.(final.current)).then(\n      () => setStatus(\"sent\"),\n      (e: unknown) => {\n        setError(reasonOf(e));\n        setStatus(\"draft\");\n      }\n    );\n  }, [status, left]);\n\n  const send = () => {\n    setLeft(undoSeconds);\n    setError(undefined);\n    setStatus(\"pending\");\n  };\n\n  const undo = () => {\n    setStatus(\"draft\");\n    requestAnimationFrame(() => sendRef.current?.focus());\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={writing ? \"writing\" : status}\n      >\n        {shownPrompt ? (\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            {shownPrompt}\n          </p>\n        ) : null}\n        {shownMessage ? (\n          <p className=\"text-[15px] text-zinc-800 leading-7 dark:text-zinc-200\">\n            {shownMessage}\n          </p>\n        ) : null}\n\n        <AutoHeight className=\"rounded-2xl bg-white ring-1 ring-black/[0.08] dark:bg-zinc-900 dark:ring-white/10\">\n          <AnimatePresence initial={false} mode=\"popLayout\">\n            {status === \"sent\" ? (\n              <motion.div\n                animate={{ opacity: 1, filter: \"blur(0px)\" }}\n                className=\"flex items-center gap-3 px-4 py-3\"\n                initial={{ opacity: 0, filter: \"blur(3px)\" }}\n                key=\"sent\"\n                transition={{ duration: 0.3, ease: EASE_OUT, delay: 0.08 }}\n              >\n                <motion.span\n                  animate={{ scale: 1 }}\n                  className=\"flex size-7 shrink-0 items-center justify-center rounded-full bg-blue-600 text-white dark:bg-blue-500\"\n                  initial={{ scale: 0.6 }}\n                  transition={{\n                    type: \"spring\",\n                    stiffness: 500,\n                    damping: 26,\n                    delay: 0.1,\n                  }}\n                >\n                  <Check\n                    aria-hidden=\"true\"\n                    className=\"size-3.5\"\n                    strokeWidth={2.5}\n                  />\n                </motion.span>\n                <div className=\"min-w-0 flex-1\">\n                  <p className=\"truncate text-[13.5px] text-zinc-900 dark:text-zinc-100\">\n                    <span className=\"font-medium\">Sent to {draft.toName}</span>\n                    <span className=\"text-zinc-500 dark:text-zinc-400\">\n                      {\" \"}\n                      · just now\n                    </span>\n                  </p>\n                  <p className=\"mt-0.5 truncate text-[12px] text-zinc-500 dark:text-zinc-400\">\n                    {draft.subject}\n                  </p>\n                </div>\n              </motion.div>\n            ) : (\n              <motion.div\n                exit={{ opacity: 0, filter: \"blur(3px)\" }}\n                key=\"draft\"\n                transition={{ duration: 0.2 }}\n              >\n                <dl className=\"divide-y divide-black/[0.06] px-4 text-[13px] dark:divide-white/[0.08]\">\n                  <Field label=\"To\">\n                    <span className=\"text-zinc-900 dark:text-zinc-100\">\n                      {draft.toName}\n                    </span>{\" \"}\n                    <span className=\"text-zinc-400 dark:text-zinc-500\">\n                      {draft.toEmail}\n                    </span>\n                  </Field>\n                  <Field label=\"Subject\">\n                    <span className=\"text-zinc-900 dark:text-zinc-100\">\n                      {draft.subject}\n                    </span>\n                  </Field>\n                </dl>\n                <label className=\"sr-only\" htmlFor={id}>\n                  Message\n                </label>\n                <textarea\n                  className={cn(\n                    \"field-sizing-content block w-full resize-none border-black/[0.06] border-t bg-transparent px-4 pt-3 pb-4 text-[14px] text-zinc-800 leading-6 outline-hidden transition-opacity duration-200 dark:border-white/[0.08] dark:text-zinc-200\",\n                    status !== \"draft\" && \"opacity-50\"\n                  )}\n                  id={id}\n                  onChange={(e) => setEdited(e.target.value)}\n                  readOnly={status !== \"draft\" || writing}\n                  rows={6}\n                  value={body}\n                />\n                <div className=\"flex items-center gap-2 border-black/[0.06] border-t px-3 py-2.5 dark:border-white/[0.08]\">\n                  <span className=\"flex items-center gap-1.5 pl-1 text-[12px] text-zinc-500 dark:text-zinc-400\">\n                    <Mail aria-hidden=\"true\" className=\"size-3.5\" />\n                    {draft.from}\n                  </span>\n                  <div className=\"ml-auto flex items-center gap-1.5\">\n                    <AnimatePresence initial={false}>\n                      {status === \"pending\" && (\n                        <motion.button\n                          animate={{ opacity: 1, x: 0 }}\n                          className=\"h-8 rounded-lg px-3 font-medium text-[13px] text-zinc-900 outline-hidden transition-colors duration-100 hover:bg-zinc-100 focus-visible:ring-2 focus-visible:ring-blue-500/40 active:bg-zinc-200 dark:text-zinc-100 dark:active:bg-white/[0.11] dark:hover:bg-white/[0.07]\"\n                          exit={{ opacity: 0, x: 6 }}\n                          initial={{ opacity: 0, x: 6 }}\n                          onClick={undo}\n                          transition={{ duration: 0.18, ease: EASE_OUT }}\n                          type=\"button\"\n                        >\n                          Undo\n                        </motion.button>\n                      )}\n                    </AnimatePresence>\n                    <SendButton\n                      disabled={writing}\n                      left={left}\n                      onSend={send}\n                      pending={status === \"pending\" || status === \"sending\"}\n                      ref={sendRef}\n                      total={undoSeconds}\n                    />\n                  </div>\n                </div>\n                {error && status === \"draft\" ? (\n                  <p\n                    className=\"px-4 pb-3 text-[12.5px] text-rose-600 dark:text-rose-400\"\n                    role=\"alert\"\n                  >\n                    {error}\n                  </p>\n                ) : null}\n              </motion.div>\n            )}\n          </AnimatePresence>\n        </AutoHeight>\n        <p aria-live=\"polite\" className=\"sr-only\">\n          {status === \"pending\" ? `Sending in ${left} seconds` : \"\"}\n          {status === \"sent\" ? \"Sent\" : \"\"}\n        </p>\n      </div>\n    </MotionConfig>\n  );\n}\n\nfunction Field({ label, children }: { label: string; children: ReactNode }) {\n  return (\n    <div className=\"flex h-10 items-center gap-3\">\n      <dt className=\"w-14 shrink-0 text-zinc-500 dark:text-zinc-400\">\n        {label}\n      </dt>\n      <dd className=\"min-w-0 truncate\">{children}</dd>\n    </div>\n  );\n}\n\n/** Send, then a ring that drains while the undo window is open. */\nfunction SendButton({\n  pending,\n  disabled,\n  left,\n  total,\n  onSend,\n  ref,\n}: {\n  pending: boolean;\n  disabled: boolean;\n  left: number;\n  total: number;\n  onSend: () => void;\n  ref: Ref<HTMLButtonElement>;\n}) {\n  return (\n    <button\n      className={cn(\n        \"flex h-8 min-w-[76px] items-center justify-center gap-2 rounded-lg px-3 font-medium text-[13px] outline-hidden transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-blue-500/50 focus-visible:ring-offset-2 dark:focus-visible:ring-offset-zinc-900\",\n        disabled && !pending && \"opacity-50\",\n        pending\n          ? \"bg-zinc-100 text-zinc-700 dark:bg-white/[0.08] dark:text-zinc-200\"\n          : \"bg-blue-600 text-white hover:bg-blue-700 dark:bg-blue-500 dark:hover:bg-blue-600\"\n      )}\n      disabled={pending || disabled}\n      onClick={onSend}\n      ref={ref}\n      type=\"button\"\n    >\n      <AnimatePresence initial={false} mode=\"popLayout\">\n        {pending ? (\n          <motion.span\n            animate={{ opacity: 1 }}\n            className=\"flex items-center gap-2\"\n            exit={{ opacity: 0 }}\n            initial={{ opacity: 0 }}\n            key=\"pending\"\n            transition={{ duration: 0.15 }}\n          >\n            <span className=\"relative flex size-5 items-center justify-center\">\n              <svg\n                aria-hidden=\"true\"\n                className=\"absolute inset-0 -rotate-90\"\n                viewBox=\"0 0 20 20\"\n              >\n                <circle\n                  className=\"stroke-zinc-300 dark:stroke-zinc-600\"\n                  cx=\"10\"\n                  cy=\"10\"\n                  fill=\"none\"\n                  r=\"8\"\n                  strokeWidth=\"1.75\"\n                />\n                <motion.circle\n                  animate={{ strokeDashoffset: RING }}\n                  className=\"stroke-blue-600 dark:stroke-blue-400\"\n                  cx=\"10\"\n                  cy=\"10\"\n                  fill=\"none\"\n                  initial={{ strokeDashoffset: 0 }}\n                  r=\"8\"\n                  strokeDasharray={RING}\n                  strokeLinecap=\"round\"\n                  strokeWidth=\"1.75\"\n                  transition={{ duration: total, ease: \"linear\" }}\n                />\n              </svg>\n              <span className=\"relative overflow-hidden font-mono text-[10.5px] tabular-nums\">\n                <AnimatePresence initial={false} mode=\"popLayout\">\n                  <motion.span\n                    animate={{ y: 0, opacity: 1 }}\n                    className=\"block\"\n                    exit={{ y: -6, opacity: 0 }}\n                    initial={{ y: 6, opacity: 0 }}\n                    key={left}\n                    transition={{ duration: 0.2, ease: EASE_OUT }}\n                  >\n                    {left > 0 ? left : \"\"}\n                  </motion.span>\n                </AnimatePresence>\n              </span>\n            </span>\n            Sending\n          </motion.span>\n        ) : (\n          <motion.span\n            animate={{ opacity: 1 }}\n            exit={{ opacity: 0 }}\n            initial={{ opacity: 0 }}\n            key=\"send\"\n            transition={{ duration: 0.15 }}\n          >\n            Send\n          </motion.span>\n        )}\n      </AnimatePresence>\n    </button>\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 ref={innerRef}>{children}</div>\n    </motion.div>\n  );\n}\n",
      "path": "/components/kokonutui/ai-send.tsx",
      "target": "components/kokonutui/ai-send.tsx"
    }
  ]
}