Chat Message
Unified chat message component for user and AI conversations. Features copy, regenerate, and feedback actions with streaming indicator support.
Preview
Interactive conversation with action buttons
Can you help me understand how React hooks work?
Of course! React hooks are functions that let you use state and other React features in functional components. The most common hooks are useState for managing state, useEffect for side effects, and useContext for consuming context.
Streaming message in progress...
Variants
Two visual styles for different contexts
Default
This is a user message with default styling
This is an AI response with default styling - white background with border
Minimal
This is a user message with minimal styling
This is an AI response with minimal styling - subtle backgrounds
Convenience Components
AIMessage and UserMessage shortcuts
UserMessage automatically sets role='user'
AIMessage automatically sets role='assistant'
Custom Avatar
Override default avatars with custom elements
Custom avatar example with Bot icon instead of default Sparkles
Props
ChatMessage component API reference
| Prop | Type | Default | Description |
|---|---|---|---|
role* | 'user' | 'assistant' | — | Message sender role - determines styling and avatar |
content* | string | — | Message content text |
variant | 'default' | 'minimal' | 'default' | Visual style variant |
timestamp | string | — | Optional timestamp to display |
isStreaming | boolean | false | Show streaming indicator (pulsing cursor) |
showActions | boolean | true | Show action buttons (copy, regenerate, feedback) |
userName | string | 'You' | Display name for user messages |
aiName | string | 'AI Assistant' | Display name for assistant messages |
avatar | ReactNode | — | Custom avatar element to override default |
onCopy | () => void | — | Callback when copy button is clicked |
onRegenerate | () => void | — | Callback when regenerate button is clicked |
onFeedback | (feedback: 'up' | 'down' | null) => void | — | Callback when thumbs up/down is clicked |
className | string | — | Additional CSS classes |
ChatContainer Props
| Prop | Type | Default | Description |
|---|---|---|---|
children* | ReactNode | — | Chat messages to wrap |
className | string | — | Additional CSS classes |
Usage
Import and implementation example
import { ChatMessage, ChatContainer, AIMessage, UserMessage } from '@/blocks/ai-conversation/chat-message'
export default function Example() {
return (
<>
{/* Using ChatContainer for consistent styling */}
<ChatContainer>
<ChatMessage
role="user"
content="How do I use this component?"
timestamp="2:30 PM"
/>
<ChatMessage
role="assistant"
content="It's easy! Just pass the role and content props."
timestamp="2:30 PM"
onFeedback={(fb) => console.log('Feedback:', fb)}
onCopy={() => console.log('Copied!')}
onRegenerate={() => console.log('Regenerate')}
/>
</ChatContainer>
{/* Using convenience components */}
<ChatContainer>
<UserMessage content="Hello!" />
<AIMessage content="Hi there! How can I help?" />
</ChatContainer>
{/* Streaming message */}
<ChatMessage
role="assistant"
content="Generating response..."
isStreaming={true}
showActions={false}
/>
{/* Custom avatar */}
<ChatMessage
role="assistant"
content="Custom avatar example"
avatar={<CustomAvatarComponent />}
/>
</>
)
}Features
Built-in functionality
- Two variants: Default (bordered) and minimal (subtle) styles
- Copy to clipboard: One-click message copying with confirmation
- Regenerate button: Request new AI response with callback
- Feedback controls: Thumbs up/down with toggle behavior
- Streaming indicator: Pulsing cursor for in-progress generation
- Custom avatars: Override default avatars with any React element
- ChatContainer: Wrapper with gradient background and consistent spacing
- Convenience components: AIMessage and UserMessage shortcuts
- Dark mode: Full dark mode support
Accessibility
Accessibility considerations
ARIA Attributes
Action buttons are native button elementsCopy confirmation provides visual feedbackFeedback buttons show active state with color changeKeyboard Navigation
| Key | Action |
|---|---|
| Tab | Navigate between copy, regenerate, and feedback buttons |
| Enter / Space | Activate the focused action button |
Notes
- User and assistant roles are visually distinct
- Names are displayed above each message for clarity
- Streaming indicator is purely visual
- Consider adding aria-label to action buttons for screen readers
- Feedback toggle behavior is intuitive (click again to deselect)