Vercel

Conversation

AI chats 42

About

Here is Conversation component

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Conversation to your project and fits it to your theme.

Code

How Conversation is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

'use client';

import React, { useState, useEffect } from 'react';
import {
  Conversation,
  ConversationContent,
  ConversationScrollButton,
} from '@/components/ui/conversation';
import { cn } from '@/lib/utils';

// Message component
type MessageProps = {
  from: 'user' | 'bot';
  children: React.ReactNode;
};

const Message = ({ from, children }: MessageProps) => (
  <div
    className={cn(
      'my-2 flex',
      from === 'user' ? 'justify-end' : 'justify-start'
    )}
  >
    <div
      className={cn(
        'max-w-xs rounded-lg p-2',
        from === 'user'
          ? 'bg-blue-500 text-white'
          : 'bg-gray-200 text-gray-800'
      )}
    >
      {children}
    </div>
  </div>
);

const MessageContent = ({ children }: { children: React.ReactNode }) => (
  <div>{children}</div>
);

// Main chat component
export default function AutoChatExample() {
  const [messages, setMessages] = useState<
    { from: 'user' | 'bot'; text: string }[]
  >([]);

  // Predefined conversation flow
  const conversationFlow = [
    { from: 'user', text: 'Hi there!' },
    { from: 'bot', text: 'Hello! How are you doing?' },
    { from: 'user', text: 'I am doing great, thanks!' },
    { from: 'bot', text: 'Glad to hear that! What are you up to today?' },
    { from: 'user', text: 'Just working on some React projects.' },
    { from: 'bot', text: 'Awesome! React is fun to work with.' },
  ];

  useEffect(() => {
    if (messages.length < conversationFlow.length) {
      const timer = setTimeout(() => {
        setMessages((prev) => [
          ...prev,
          conversationFlow[prev.length],
        ]);
      }, 1200); // delay between messages
      return () => clearTimeout(timer);
    }
  }, [messages]);

  return (
    <div className="w-full max-w-md mx-auto mt-10 border rounded-lg overflow-hidden shadow-md">
      <Conversation className="relative w-full" style={{ height: '400px' }}>
        <ConversationContent>
          {messages.map((msg, idx) => (
            <Message key={idx} from={msg.from}>
              <MessageContent>{msg.text}</MessageContent>
            </Message>
          ))}
        </ConversationContent>
        <ConversationScrollButton />
      </Conversation>
    </div>
  );
}

Dependencies

More by Vercel

View all
  • Announcement

    Announcement

    289
  • Message

    Message

    48
  • Task

    Task

    40
  • Inline Citation

    Inline Citation

    36
  • Loader

    Loader

    22
  • Sources

    Sources

    20
  • Code Block

    Code Block

    12
  • Suggestion

    Suggestion

    8

More in AI chats

View all
  • AI Actions

    AI Actions

    317
  • Agent Chat

    Agent Chat

    17
  • Color Picker

    Color Picker

    73
  • Prompt with Model Picker

    Prompt with Model Picker

    2
  • Approval Card

    Approval Card

    12
  • System Message

    System Message

    53
  • AI Task List

    AI Task List

    158
  • Stream Reply

    Stream Reply