{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chat-thread",
  "title": "Chat Thread",
  "author": "VenusUI",
  "description": "A messaging conversation that plays out with typing indicators; viewers can send replies too.",
  "dependencies": [
    "lucide-react"
  ],
  "files": [
    {
      "path": "registry/default/ui/chat-thread.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { ArrowUp } from \"lucide-react\"\n\nimport { cn } from \"@/lib/utils\"\n\nexport interface ChatMessage {\n  id: string\n  from: \"me\" | \"them\"\n  text: string\n}\n\ninterface ChatThreadProps extends React.ComponentProps<\"div\"> {\n  contact: { name: string; avatar?: string; status?: string }\n  /** The conversation so far. With `autoplay` it plays out message by message. */\n  messages: ChatMessage[]\n  /** Answers the contact gives, in turn, when the viewer sends a message. */\n  replies?: string[]\n  autoplay?: boolean\n}\n\nconst TYPING_MS = 1300\nconst PAUSE_MS = 700\n\n/**\n * A messaging conversation. It plays out with typing indicators, then the\n * viewer can send their own messages and get replies. New messages are\n * announced to screen readers (`role=\"log\"`). With reduced motion the\n * conversation shows at once.\n */\nfunction ChatThread({ contact, messages, replies = [], autoplay = true, className, ...props }: ChatThreadProps) {\n  const [shown, setShown] = React.useState(autoplay ? 0 : messages.length)\n  const [extra, setExtra] = React.useState<ChatMessage[]>([])\n  const [typing, setTyping] = React.useState(false)\n  const [draft, setDraft] = React.useState(\"\")\n  const replyIndex = React.useRef(0)\n  const logRef = React.useRef<HTMLDivElement>(null)\n  const inputId = React.useId()\n  // Read the latest messages without restarting the timers when a new array\n  // with the same messages is passed on every render.\n  const messagesRef = React.useRef(messages)\n  React.useEffect(() => {\n    messagesRef.current = messages\n  })\n  const total = messages.length\n\n  // Play the conversation out, one message at a time.\n  React.useEffect(() => {\n    if (shown >= total) return\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches\n    if (reduced) {\n      const id = setTimeout(() => setShown(total), 0)\n      return () => clearTimeout(id)\n    }\n    const next = messagesRef.current[shown]\n    if (next.from === \"them\") {\n      const start = setTimeout(() => setTyping(true), PAUSE_MS)\n      const done = setTimeout(() => {\n        setTyping(false)\n        setShown((n) => n + 1)\n      }, PAUSE_MS + TYPING_MS)\n      return () => {\n        clearTimeout(start)\n        clearTimeout(done)\n      }\n    }\n    const id = setTimeout(() => setShown((n) => n + 1), PAUSE_MS + 300)\n    return () => clearTimeout(id)\n  }, [shown, total])\n\n  const all = [...messages.slice(0, shown), ...extra]\n\n  // Keep the newest message in view.\n  React.useEffect(() => {\n    const log = logRef.current\n    log?.scrollTo({ top: log.scrollHeight, behavior: \"smooth\" })\n  }, [all.length, typing])\n\n  const send = (event: React.FormEvent) => {\n    event.preventDefault()\n    const text = draft.trim()\n    if (!text) return\n    setDraft(\"\")\n    setExtra((e) => [...e, { id: `me-${Date.now()}`, from: \"me\", text }])\n    if (replies.length === 0) return\n    const reply = replies[replyIndex.current % replies.length]\n    replyIndex.current += 1\n    setTimeout(() => setTyping(true), PAUSE_MS)\n    setTimeout(() => {\n      setTyping(false)\n      setExtra((e) => [...e, { id: `them-${Date.now()}`, from: \"them\", text: reply }])\n    }, PAUSE_MS + TYPING_MS)\n  }\n\n  const lastMine = all.map((m) => m.from).lastIndexOf(\"me\")\n\n  return (\n    <div data-slot=\"chat-thread\" className={cn(\"flex h-[30rem] w-full max-w-sm flex-col overflow-hidden rounded-[1.75rem] border bg-background shadow-xl\", className)} {...props}>\n      {/* Header */}\n      <div className=\"flex flex-col items-center gap-1 border-b bg-muted/40 px-4 pt-4 pb-3 backdrop-blur\">\n        {contact.avatar ? (\n          <img src={contact.avatar} alt=\"\" className=\"size-11 rounded-full object-cover\" />\n        ) : (\n          <span className=\"grid size-11 place-items-center rounded-full bg-muted font-semibold\">{contact.name[0]}</span>\n        )}\n        <p className=\"text-sm font-semibold\">{contact.name}</p>\n        {contact.status ? (\n          <p className=\"flex items-center gap-1.5 text-xs text-muted-foreground\">\n            <span className=\"size-1.5 rounded-full bg-emerald-500\" />\n            {contact.status}\n          </p>\n        ) : null}\n      </div>\n\n      {/* Messages */}\n      <div ref={logRef} role=\"log\" aria-label={`Conversation with ${contact.name}`} className=\"flex flex-1 flex-col gap-1.5 overflow-y-auto px-3 py-4\">\n        {all.map((message, index) => {\n          const mine = message.from === \"me\"\n          const nextSame = all[index + 1]?.from === message.from\n          return (\n            <div key={message.id} className={cn(\"flex flex-col\", mine ? \"items-end\" : \"items-start\", !nextSame && \"mb-2\")}>\n              <p\n                className={cn(\n                  \"max-w-[78%] animate-in px-3.5 py-2 text-[15px] leading-snug duration-300 fade-in slide-in-from-bottom-2 motion-reduce:animate-none\",\n                  mine ? \"rounded-2xl bg-sky-500 text-white\" : \"rounded-2xl bg-muted text-foreground\",\n                  !nextSame && (mine ? \"rounded-br-md\" : \"rounded-bl-md\")\n                )}\n              >\n                <span className=\"sr-only\">{mine ? \"You: \" : `${contact.name}: `}</span>\n                {message.text}\n              </p>\n              {index === lastMine ? <span className=\"mt-1 mr-1 text-[11px] text-muted-foreground\">Delivered</span> : null}\n            </div>\n          )\n        })}\n        {typing ? (\n          <div className=\"flex animate-in items-center gap-1 self-start rounded-2xl rounded-bl-md bg-muted px-3.5 py-3 fade-in motion-reduce:animate-none\">\n            <span className=\"sr-only\">{contact.name} is typing</span>\n            {[0, 150, 300].map((delay) => (\n              <span key={delay} aria-hidden=\"true\" className=\"size-2 animate-bounce rounded-full bg-muted-foreground/60 motion-reduce:animate-none\" style={{ animationDelay: `${delay}ms` }} />\n            ))}\n          </div>\n        ) : null}\n      </div>\n\n      {/* Composer */}\n      <form onSubmit={send} className=\"flex items-center gap-2 border-t p-3\">\n        <label htmlFor={inputId} className=\"sr-only\">\n          Message {contact.name}\n        </label>\n        <input\n          id={inputId}\n          value={draft}\n          onChange={(event) => setDraft(event.target.value)}\n          placeholder=\"Message\"\n          autoComplete=\"off\"\n          className=\"h-9 min-w-0 flex-1 rounded-full border bg-background px-4 text-sm outline-none focus-visible:ring-3 focus-visible:ring-ring/50\"\n        />\n        <button\n          type=\"submit\"\n          aria-label=\"Send\"\n          disabled={!draft.trim()}\n          className=\"grid size-9 shrink-0 place-items-center rounded-full bg-sky-500 text-white outline-none focus-visible:ring-3 focus-visible:ring-ring/50 disabled:opacity-40\"\n        >\n          <ArrowUp aria-hidden=\"true\" className=\"size-4\" />\n        </button>\n      </form>\n    </div>\n  )\n}\n\nexport { ChatThread }\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "version": "0.1.0"
  },
  "categories": [
    "data-display",
    "interactive"
  ],
  "type": "registry:ui"
}