--- title: CopilotChatUserMessage description: "User message rendering component with edit and branch navigation" --- `CopilotChatUserMessage` is the default component used by [CopilotChatMessageView](/reference/copilot-chat-message-view) to render user messages. It handles message display, editing functionality, and branch navigation for conversation history. ## What is CopilotChatUserMessage? The CopilotChatUserMessage component: - Renders user messages in a styled bubble - Provides a toolbar with copy and edit buttons - Supports message editing functionality - Handles branch navigation for conversation forks - Built on the [slot system](/reference/slot-system) for deep customization ## Component Architecture CopilotChatUserMessage provides slots for customizing each part of the message: ```mermaid graph LR UM[CopilotChatUserMessage] --> messageRenderer UM --> toolbar UM --> copyButton UM --> editButton UM --> branchNavigation ``` ### Slot Descriptions | Slot | Description | | ------------------ | ----------------------------------------------- | | `messageRenderer` | Renders the message text content | | `toolbar` | Container for action buttons (appears on hover) | | `copyButton` | Button to copy message content | | `editButton` | Button to edit the message | | `branchNavigation` | Navigation controls for conversation branches | ## Basic Usage Customize user messages through the `messageView.userMessage` slot on [CopilotChat](/reference/copilot-chat): ```tsx handleEdit(message), }, }} /> ``` ## Callbacks CopilotChatUserMessage provides callbacks for user interactions: ```tsx { // Open edit modal or inline editor setEditingMessage(message); }, onSwitchToBranch: ({ message, branchIndex, numberOfBranches }) => { // Switch to a different conversation branch switchToBranch(message.id, branchIndex); }, }, }} /> ``` ### Callback Props | Callback | Signature | Description | | ------------------ | ------------------------------------------------------ | ------------------------------------------- | | `onEditMessage` | `({ message }) => void` | Called when user clicks the edit button | | `onSwitchToBranch` | `({ message, branchIndex, numberOfBranches }) => void` | Called when user navigates between branches | ## Slot Customization CopilotChatUserMessage uses the [slot system](/reference/slot-system). Each slot accepts four types of values: 1. **Tailwind class string** - Add or override CSS classes 2. **Props object** - Pass additional props to the default component 3. **Custom component** - Replace the component entirely 4. **Nested sub-slots** - Drill down to customize child components ### Message Renderer Customization Style the message bubble: ```tsx ``` ### Toolbar Customization The toolbar appears on hover and contains action buttons: ```tsx ``` ### Individual Button Customization Customize specific toolbar buttons: ```tsx ``` ### Hiding Buttons Hide buttons by returning null: ```tsx null, branchNavigation: () => null, }, }} /> ``` Note: The `editButton` only shows when `onEditMessage` callback is provided. ### Branch Navigation Customization Customize the branch navigation controls: ```tsx ``` Branch navigation only appears when there are multiple branches (conversation forks) available. ## Replacing the Component To completely replace the user message component: ```tsx import { CopilotChatUserMessage } from "@copilotkit/react-core"; function CustomUserMessage({ message, ...props }) { return (
); } ; ``` ### Using the Render Function For full layout control, use the children render function: ```tsx function CustomUserMessage(props) { return ( {({ messageRenderer, toolbar, message }) => (
You {new Date(message.createdAt).toLocaleTimeString()}
{messageRenderer} {toolbar}
)}
); } ; ``` The render function receives: | Property | Type | Description | | ------------------ | -------------- | ---------------------------- | | `messageRenderer` | `ReactElement` | The rendered message content | | `toolbar` | `ReactElement` | The action buttons toolbar | | `copyButton` | `ReactElement` | Copy button | | `editButton` | `ReactElement` | Edit button | | `branchNavigation` | `ReactElement` | Branch navigation controls | | `message` | `UserMessage` | The message data | | `branchIndex` | `number` | Current branch index | | `numberOfBranches` | `number` | Total number of branches | ## Branch Navigation When users edit messages and regenerate responses, CopilotKit creates conversation branches. The branch navigation allows users to switch between these alternative conversation paths: ```tsx { console.log( `Switching to branch ${branchIndex + 1} of ${numberOfBranches}`, ); // Your branch switching logic }, }, }} /> ``` The branch navigation shows: - Previous/Next arrows to navigate between branches - Current branch indicator (e.g., "2/3") ## Examples ### Chat Bubble Style ```tsx ``` ### With Edit Functionality ```tsx function ChatWithEdit() { const [editingMessage, setEditingMessage] = useState(null); return ( <> setEditingMessage(message), editButton: "text-blue-500 hover:text-blue-700", }, }} /> {editingMessage && ( setEditingMessage(null)} /> )} ); } ``` ### Minimal Style Hide all toolbar elements for a clean look: ```tsx null, }, }} /> ``` ### Custom Message with Avatar ```tsx function UserMessageWithAvatar(props) { return ( {({ messageRenderer, toolbar }) => (
{messageRenderer} {toolbar}
You
)}
); } ; ``` ### Styled for Dark Mode ```tsx ``` ## Related - [CopilotChat](/reference/copilot-chat) - Parent component - [CopilotChatMessageView](/reference/copilot-chat-message-view) - Message list component that uses user messages - [CopilotChatAssistantMessage](/reference/copilot-chat-assistant-message) - Counterpart for AI messages - [Slot System](/reference/slot-system) - Deep dive into slot customization