Chat Message

AI
Chat

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

You2:30 PM

Can you help me understand how React hooks work?

AI Assistant2:30 PM

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.

AI Assistant2:31 PM

Streaming message in progress...

Variants

Two visual styles for different contexts

Default

You

This is a user message with default styling

AI Assistant

This is an AI response with default styling - white background with border

Minimal

You

This is a user message with minimal styling

AI Assistant

This is an AI response with minimal styling - subtle backgrounds

Convenience Components

AIMessage and UserMessage shortcuts

You3:00 PM

UserMessage automatically sets role='user'

Eidetic AI3:00 PM

AIMessage automatically sets role='assistant'

Custom Avatar

Override default avatars with custom elements

AI Assistant

Custom avatar example with Bot icon instead of default Sparkles

Props

ChatMessage component API reference

PropTypeDefaultDescription
role*'user' | 'assistant'Message sender role - determines styling and avatar
content*stringMessage content text
variant'default' | 'minimal''default'Visual style variant
timestampstringOptional timestamp to display
isStreamingbooleanfalseShow streaming indicator (pulsing cursor)
showActionsbooleantrueShow action buttons (copy, regenerate, feedback)
userNamestring'You'Display name for user messages
aiNamestring'AI Assistant'Display name for assistant messages
avatarReactNodeCustom avatar element to override default
onCopy() => voidCallback when copy button is clicked
onRegenerate() => voidCallback when regenerate button is clicked
onFeedback(feedback: 'up' | 'down' | null) => voidCallback when thumbs up/down is clicked
classNamestringAdditional CSS classes

ChatContainer Props

PropTypeDefaultDescription
children*ReactNodeChat messages to wrap
classNamestringAdditional 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 change

Keyboard Navigation

KeyAction
TabNavigate between copy, regenerate, and feedback buttons
Enter / SpaceActivate 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)