{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"message-bubble","type":"registry:component","title":"Message Bubble","description":"User and assistant chat bubbles that spring in, group consecutive messages and reveal copy, retry and feedback actions on hover or focus.","author":"GetUrUI <hello@geturui.dev>","dependencies":["motion","lucide-react"],"registryDependencies":["@geturui/copy-button"],"files":[{"path":"registry/geturui/ai/message-bubble.tsx","type":"registry:component","target":"components/ai/message-bubble.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { motion, useReducedMotion } from \"motion/react\";\nimport { RotateCcw, Sparkles, ThumbsDown, ThumbsUp } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\nimport { CopyButton } from \"@/components/ui/copy-button\";\n\nexport type MessageRole = \"user\" | \"assistant\";\nexport type MessageFeedback = \"up\" | \"down\" | null;\n/** Where a bubble sits inside a run of messages from the same sender. */\nexport type MessagePosition = \"single\" | \"first\" | \"middle\" | \"last\";\n\nexport interface MessageBubbleProps {\n  role: MessageRole;\n  children: React.ReactNode;\n  /** Sender name, used for the avatar label and screen readers. */\n  name?: string;\n  /** Custom avatar; defaults to initials (user) or a spark glyph (assistant). */\n  avatar?: React.ReactNode;\n  timestamp?: string;\n  position?: MessagePosition;\n  /** Plain text copied by the copy action. Defaults to `children` when it is a string. */\n  copyText?: string;\n  onRetry?: () => void;\n  feedback?: MessageFeedback;\n  onFeedbackChange?: (value: MessageFeedback) => void;\n  /** Hide the actions row entirely. */\n  hideActions?: boolean;\n  className?: string;\n}\n\nconst radius: Record<MessageRole, Record<MessagePosition, string>> = {\n  user: {\n    single: \"rounded-[20px] rounded-br-[6px]\",\n    first: \"rounded-[20px] rounded-br-[6px]\",\n    middle: \"rounded-[20px] rounded-r-[6px]\",\n    last: \"rounded-[20px] rounded-tr-[6px]\",\n  },\n  assistant: {\n    single: \"rounded-[20px] rounded-bl-[6px]\",\n    first: \"rounded-[20px] rounded-bl-[6px]\",\n    middle: \"rounded-[20px] rounded-l-[6px]\",\n    last: \"rounded-[20px] rounded-tl-[6px]\",\n  },\n};\n\nfunction initials(name?: string) {\n  if (!name) return \"You\";\n  return name\n    .split(/\\s+/)\n    .map((p) => p[0])\n    .join(\"\")\n    .slice(0, 2)\n    .toUpperCase();\n}\n\nfunction ActionButton({ label, pressed, onClick, children }: { label: string; pressed?: boolean; onClick?: () => void; children: React.ReactNode }) {\n  return (\n    <motion.button\n      type=\"button\"\n      whileTap={{ scale: 0.88 }}\n      aria-label={label}\n      aria-pressed={pressed}\n      onClick={onClick}\n      className={cn(\n        \"grid size-7 place-items-center rounded-[8px] outline-none transition-colors focus-visible:shadow-[var(--focus-ring)]\",\n        pressed ? \"bg-volt-soft text-volt-ink\" : \"text-ink-subtle hover:bg-surface-hover hover:text-ink\",\n      )}\n    >\n      {children}\n    </motion.button>\n  );\n}\n\n/** Chat bubble with a spring entrance, avatar, timestamp and an actions row (copy, retry, feedback) revealed on hover or focus. */\nexport function MessageBubble({\n  role,\n  children,\n  name,\n  avatar,\n  timestamp,\n  position = \"single\",\n  copyText,\n  onRetry,\n  feedback,\n  onFeedbackChange,\n  hideActions,\n  className,\n}: MessageBubbleProps) {\n  const reduce = useReducedMotion();\n  const [innerFeedback, setInnerFeedback] = React.useState<MessageFeedback>(null);\n  const fb = feedback !== undefined ? feedback : innerFeedback;\n  const isUser = role === \"user\";\n  const showAvatar = position === \"single\" || position === \"last\";\n  const showMeta = showAvatar;\n  const text = copyText ?? (typeof children === \"string\" ? children : undefined);\n\n  const setFb = (v: MessageFeedback) => {\n    const next = fb === v ? null : v;\n    if (feedback === undefined) setInnerFeedback(next);\n    onFeedbackChange?.(next);\n  };\n\n  const hasActions = !hideActions && (text !== undefined || onRetry || !isUser);\n\n  return (\n    <motion.div\n      initial={reduce ? { opacity: 0 } : { opacity: 0, y: 12, scale: 0.96, filter: \"blur(4px)\" }}\n      animate={{ opacity: 1, y: 0, scale: 1, filter: \"blur(0px)\" }}\n      transition={{ type: \"spring\", stiffness: 380, damping: 30 }}\n      style={{ transformOrigin: isUser ? \"bottom right\" : \"bottom left\" }}\n      className={cn(\n        \"group/msg flex w-full items-end gap-2.5\",\n        isUser ? \"flex-row-reverse\" : \"flex-row\",\n        position === \"first\" || position === \"middle\" ? \"mb-0.5\" : \"mb-3\",\n        className,\n      )}\n    >\n      <div className=\"w-8 shrink-0\" aria-hidden={!showAvatar}>\n        {showAvatar &&\n          (avatar ?? (\n            <span\n              aria-hidden\n              className={cn(\n                \"grid size-8 place-items-center rounded-full text-[11px] font-semibold\",\n                isUser ? \"bg-surface text-ink-muted ring-1 ring-border\" : \"bg-volt text-on-volt\",\n              )}\n            >\n              {isUser ? initials(name) : <Sparkles className=\"size-4\" />}\n            </span>\n          ))}\n      </div>\n\n      <div className={cn(\"flex min-w-0 max-w-[80%] flex-col\", isUser ? \"items-end\" : \"items-start\")}>\n        <div\n          className={cn(\n            \"px-4 py-2.5 text-[14.5px] leading-[22px] [overflow-wrap:anywhere]\",\n            radius[role][position],\n            isUser ? \"bg-ink text-bg\" : \"border border-border bg-surface text-ink\",\n          )}\n        >\n          <span className=\"sr-only\">{isUser ? (name ?? \"You\") : (name ?? \"Assistant\")} said: </span>\n          {children}\n        </div>\n\n        {(showMeta || hasActions) && (\n          <div className={cn(\"mt-1 flex h-7 items-center gap-1 px-1\", isUser ? \"flex-row-reverse\" : \"flex-row\")}>\n            {showMeta && timestamp && <span className=\"px-1 text-[11.5px] text-ink-subtle tabular-nums\">{timestamp}</span>}\n            {hasActions && (\n              <div\n                className={cn(\n                  \"flex items-center gap-0.5 transition-[opacity,transform] duration-200 ease-[var(--ease-spring-soft)]\",\n                  \"opacity-0 translate-y-0.5 group-hover/msg:translate-y-0 group-hover/msg:opacity-100 group-focus-within/msg:translate-y-0 group-focus-within/msg:opacity-100\",\n                  \"[@media(hover:none)]:opacity-100 [@media(hover:none)]:translate-y-0\",\n                  fb && \"opacity-100 translate-y-0\",\n                )}\n              >\n                {text !== undefined && <CopyButton value={text} label=\"Copy message\" className=\"size-7 rounded-[8px] text-ink-subtle [&_svg]:size-3.5\" />}\n                {onRetry && (\n                  <ActionButton label=\"Retry\" onClick={onRetry}>\n                    <RotateCcw aria-hidden className=\"size-3.5\" />\n                  </ActionButton>\n                )}\n                {!isUser && (\n                  <>\n                    <ActionButton label=\"Good response\" pressed={fb === \"up\"} onClick={() => setFb(\"up\")}>\n                      <ThumbsUp aria-hidden className=\"size-3.5\" />\n                    </ActionButton>\n                    <ActionButton label=\"Bad response\" pressed={fb === \"down\"} onClick={() => setFb(\"down\")}>\n                      <ThumbsDown aria-hidden className=\"size-3.5\" />\n                    </ActionButton>\n                  </>\n                )}\n              </div>\n            )}\n          </div>\n        )}\n      </div>\n    </motion.div>\n  );\n}\n\nexport interface ThreadMessage {\n  id: string;\n  role: MessageRole;\n  content: React.ReactNode;\n  name?: string;\n  timestamp?: string;\n  copyText?: string;\n}\n\nexport interface MessageThreadProps {\n  messages: ThreadMessage[];\n  onRetry?: (id: string) => void;\n  onFeedbackChange?: (id: string, value: MessageFeedback) => void;\n  className?: string;\n}\n\n/** Positions consecutive messages from the same sender into a group: one avatar and timestamp per run, tighter corners in between. */\nexport function groupPositions(messages: { role: MessageRole }[]): MessagePosition[] {\n  return messages.map((m, i) => {\n    const prev = messages[i - 1]?.role === m.role;\n    const next = messages[i + 1]?.role === m.role;\n    if (prev && next) return \"middle\";\n    if (prev) return \"last\";\n    if (next) return \"first\";\n    return \"single\";\n  });\n}\n\n/** A list of messages with consecutive messages from the same sender grouped. */\nexport function MessageThread({ messages, onRetry, onFeedbackChange, className }: MessageThreadProps) {\n  const positions = groupPositions(messages);\n  return (\n    <div role=\"log\" aria-live=\"polite\" className={cn(\"flex flex-col\", className)}>\n      {messages.map((m, i) => (\n        <MessageBubble\n          key={m.id}\n          role={m.role}\n          name={m.name}\n          timestamp={m.timestamp}\n          position={positions[i]}\n          copyText={m.copyText}\n          onRetry={m.role === \"assistant\" && onRetry ? () => onRetry(m.id) : undefined}\n          onFeedbackChange={onFeedbackChange ? (v) => onFeedbackChange(m.id, v) : undefined}\n        >\n          {m.content}\n        </MessageBubble>\n      ))}\n    </div>\n  );\n}\n"},{"path":"LICENSE-geturui.md","type":"registry:file","target":"LICENSE-geturui.md","content":"MIT License\n\nCopyright (c) 2026 geturui.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"}],"categories":["ai","ai","chat","message","bubble","thread","feedback"],"meta":{"tier":"free","license":"MIT","docs":"https://geturui.com/docs/message-bubble"}}