Get Back
December 20, 2024
Chat Box Bubbles Animation Using Framer Motion
tricks

React Chatbox Component with Animations

This code snippet implements a chatbox component using React with features like message history, user input, and animated bot responses. Let's break down the code and understand its functionalities.

Imports:

The code imports necessary libraries from React:

  • useState for managing component state.
  • AnimatePresence and motion from react-motion for animations.

ChatBox Component:

The ChatBox component is the main functional component responsible for rendering the chatbox UI and handling interactions.

State Variables:

  • inputValue: Stores the current user input text.
  • disableForBot: Controls the disabled state of the input field during bot responses.
  • messages: An array of objects representing chat messages, including text and sender ("user" or "bot").
  • isTyping: A boolean flag indicating if the bot is currently "typing" (simulated animation).

Message Handling:

  • handleInputChange: This function updates the inputValue state variable whenever the user types in the input field. It also triggers a temporary isTyping state to simulate bot activity.

  • handleSubmit: This function handles the form submission when the user presses Enter or clicks the send button. It prevents default form submission behavior. Here's a breakdown of the logic:

    • Adds the user's message to the messages state.
    • Disables the input field temporarily using disableForBot to prevent user input during bot response.
    • Simulates bot response by adding a bot message to the messages state after a 1-second delay.
    • Clears the user input field after sending the message.

Message Rendering:

The component iterates through the messages state array using map and renders each message using AnimatePresence and motion from react-motion. This enables animations for new messages and bot typing simulation.

  • Message Box:

    • A conditional statement applies different styles to the message box depending on the sender ("bot" or "user").
    • The message text is displayed within the box.
  • Bot Typing Animation:

    • An AnimatePresence component controls the animation for the bot typing indicator.
    • When isTyping is true, a motion div with the message "Bot is listening ..." fades in and moves up from the bottom.
    • The animation includes styles for the text and a spinner icon.

Input Field:

  • The input field component is wrapped in a form element to handle form submission.
  • The disabled state of the input field is controlled by the disableForBot state variable.
  • A button is positioned absolutely at the right end of the input field to trigger form submission.

Overall, this code snippet demonstrates a well-structured React component with state management, user interactions, and animations to create an engaging chatbox experience.

Additional Notes for Advanced Developers:

  • The code utilizes framer-motion for animations. Consider exploring advanced animation concepts and customization options provided by the library.
  • Error handling and edge cases can be further implemented to improve robustness.
  • The current implementation simulates a simple bot response. In a real-world scenario, you might integrate with a chatbot API for more intelligent interactions.
Check out the full code in github
Made With Kisses By Mohamed Rafik