Chat Message
User and assistant messages that arrive from their own side, with the reply streaming in word by word.
Can you turn this into a checklist?
Code
components/thinking-ui/chat-message.tsx
"use client";
import { AnimatePresence, motion } from "motion/react";
import type { ReactNode } from "react";
import { StreamingText } from "@/components/thinking-ui/streaming-text";
import { TypingIndicator } from "@/components/thinking-ui/typing-indicator";
import { useMotion } from "@/lib/motion";
import { cn } from "@/lib/utils";
export type ChatMessageProps = {
role: "user" | "assistant";
/** Message content. For the assistant, pass the reply as a string as it arrives and each new word fades in. */
children?: ReactNode;
/** Waiting for the first token: shows typing dots in place of the reply. */
pending?: boolean;
/** More text is on its way: shows a caret after the last word. */
streaming?: boolean;
/** Replaces the default assistant avatar. */
avatar?: ReactNode;
className?: string;
};
export function ChatMessage({ role, children, pending = false, streaming = false, avatar, className }: ChatMessageProps) {
const m = useMotion();
const isUser = role === "user";
// Plain text from the assistant streams word by word; anything richer is shown as given
const body = !isUser && typeof children === "string" ? <StreamingText text={children} streaming={streaming} /> : children;
return (
<motion.div
className={cn("flex w-full gap-3", isUser ? "justify-end" : "justify-start", className)}
style={{ transformOrigin: isUser ? "100% 100%" : "0% 100%" }}
initial={{
opacity: 0,
y: m.distance.md,
x: isUser ? m.distance.sm : -m.distance.sm,
scale: Math.max(m.personality.scaleFrom, 0.9),
filter: m.blur(),
}}
animate={{ opacity: 1, y: 0, x: 0, scale: 1, filter: m.blur(0) }}
transition={m.spring("base")}
>
{!isUser && (
<span className="mt-0.5 shrink-0">
{avatar ?? (
<motion.span
aria-hidden
className="block size-6 rounded-full shadow-bevel"
style={{ background: "radial-gradient(circle at 35% 30%, var(--orb-3), var(--orb-1) 60%, var(--orb-shade))" }}
// The avatar breathes while the reply is on its way
animate={!m.reduce && (pending || streaming) ? { scale: [1, 1.12, 1] } : { scale: 1 }}
transition={pending || streaming ? { duration: 1.4 * m.scale, repeat: Infinity, ease: "easeInOut" } : m.spring("micro")}
/>
)}
</span>
)}
<div
className={cn(
"max-w-[85%] text-base leading-relaxed",
isUser ? "rounded-bubble rounded-br-sm bg-bubble-user px-4 py-2.5 text-bubble-user-fg" : "min-h-6 pt-0.5 text-fg-secondary",
)}
>
<AnimatePresence mode="popLayout" initial={false}>
{pending ? (
<motion.span key="pending" className="flex h-6 items-center" {...m.swap()}>
<TypingIndicator />
</motion.span>
) : (
<motion.div key="body" initial={false} exit={{ opacity: 0 }}>
{body}
</motion.div>
)}
</AnimatePresence>
</div>
</motion.div>
);
}Motion notes
- User messages grow from the bottom-right, assistant messages from the bottom-left. Direction tells you who's talking.
- Before the first token the reply shows typing dots; then each new word fades up out of a blur as the text grows.
- The avatar breathes while a reply is pending or streaming, and settles when it's done.
- Bubble color, radius and text come from the bubble tokens, so restyling is a token change.
Installation
- 1
Install dependencies
Terminalnpm install motion clsx tailwind-merge - 2
Add the design tokens and motion system (once per project)
Copy
styles/tokens/*,lib/motion.tsxandlib/utils.tsfrom Get started. - 3
Add the components it uses
- 4
Paste the source into components/thinking-ui/chat-message.tsx
Copy it from the Code tab, or use Copy prompt to let your coding agent do all of this.
Usage
Example
<ChatMessage role="user">Make it a checklist</ChatMessage>
<ChatMessage role="assistant" pending={!reply} streaming={isStreaming}>
{reply}
</ChatMessage>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| role | "user" | "assistant" | — | Who sent the message. |
| children | ReactNode | — | Message content. Pass the assistant's reply as a string as it arrives to stream it word by word. |
| pending | boolean | false | Waiting for the first token: shows typing dots. |
| streaming | boolean | false | More text is coming: shows a caret after the last word. |
| avatar | ReactNode | — | Replaces the default assistant avatar. |