Navigation Bar

Sunday, November 1, 2026

React - Using CSS stylesheet in chatbot - Full Code

A demo of the chatbot can be tried below 

Chatbot
Chatbot project with CSS stylesheet code
<!DOCTYPE html>
<html>
  <head>
    <title>Chatbot</title>
     <style>

       body {
          font-family: Arial;
          margin-top: 0px;
          margin-bottom: 0px;
       }
       .send-button {
         background-color: rgb(25,135,84);
         color: white;
         padding: 12px 20px;
         margin-left: 10px;
         border: none;
         border-radius: 10px;
         font-size:15px;
         cursor : pointer;
       } 

       .chat-input {
          padding : 12px 15px;
          border-radius : 10px;
          border-width : 1px;
          font-size:15px;
          flex-grow:1;
       }

       .chat-input-container{
        display:flex;
        margin-bottom: 30px;
       }

       .app-container {
          max-width:600px;
          margin-left:auto;
          margin-right:auto;
          height: 100vh; 
          display: flex;
          flex-direction: column;          
        }

       .chat-message-user {
         display: flex;
         justify-content: end;
         align-items: start;
       }

       .chat-message-robot {
         display: flex;
         align-items: start; 
       }

       .chat-message-text {
          background-color: rgb(238,238,238);
          padding : 15px 20px;
          border-radius : 10px;
          margin-right : 10px;
          margin-left : 10px;
          margin-bottom: 20px;
          max-width: 300px;
       }

       .chat-message-profile {
          width: 45px;

       }

       .chat-messages-container {
         flex-grow: 1;
         margin-top: 20px;
         overflow: scroll;
         scrollbar-width: none; 
       }

    </style>
  </head>
  <body>
    <div class="js-container"></div>
    

    <script src="https://unpkg.com/supersimpledev/react.js"></script>
    <script src="https://unpkg.com/supersimpledev/react-dom.js"></script>
    <script src="https://unpkg.com/supersimpledev/chatbot.js"></script>
    <script src="https://unpkg.com/supersimpledev/babel.js"></script>
 
  <script type="text/babel">
    
       function ChatInput({chatMessages, setChatMessages}){
          const [inputText, setInputText] = React.useState('');

          function saveInputText(event) {
          // console.log(event.target.value);
            setInputText(event.target.value);
          }

          function sendMessage() {
         //   console.log(inputText);

         const newChatMessages = [
            ...chatMessages,
          {
              message : inputText,
              sender : 'user',
              id  : crypto.randomUUID()
          }
        ];
        setChatMessages(newChatMessages);
         
        const response = Chatbot.getResponse(inputText);
        //    console.log(response);  
/* in react we should not modify the data directly. 
We should create a copy of the data and modify the copy. */
          setChatMessages([
                ...newChatMessages,
              {
                  message : response,
                  sender : 'robot',
                  id  : crypto.randomUUID()
              }
              
            ]);

            setInputText('');  
          }
        return (
       
          <div className="chat-input-container"> 
          {/* using fragment to remove extra div elements */}
             <input placeholder="Send a message to chatbot" 
             size="30"
             onChange={saveInputText}
             value={inputText}
             className="chat-input"
             />
             <button
               onClick = {sendMessage}
               className="send-button"
             >Send</button>
          </div>

        );
       }

    

      function ChatMessage({message, sender}){
          
          return (
              <div className={
                sender === 'user' 
                ? 'chat-message-user' 
                : 'chat-message-robot'
              }> {/* use div element to put the ChatMessage on a new line */}
              {sender === 'robot' && <img src="robot.jpeg"
               className="chat-message-profile"/>}
              <div className="chat-message-text">  
                {message}
              </div>  
              {sender === 'user' && <img src="human.jpeg" className="chat-message-profile"/>}
                </div>

              )
          
       }  

function ChatMessages({chatMessages}) {

const chatMessagesRef = React.useRef(null);

React.useEffect (() => {
    console.log('updated');
    console.log(chatMessagesRef.current);   
    const containerElem=chatMessagesRef.current;
    if (containerElem) {
      containerElem.scrollTop = containerElem.scrollHeight;
    }
}, [chatMessages]);


return (
<div className="chat-messages-container" ref={chatMessagesRef}>
{chatMessages.map((chatMessage) => {
return (
  <ChatMessage 
     message={chatMessage.message}
     sender={chatMessage.sender}
     key={chatMessage.id}
/>
);
} )}
</div>
);
}

      function App (){
        const array = React.useState([{
            message: 'hello chatbot',
            sender: 'user',
            id : 'id1'
        }, {
            message: 'Hello! How can I help you?',
            sender: 'robot',
            id : 'id2'
        },
        {
            message: 'Can you get me todays date?',
            sender: 'user',
            id : 'id3'
        },
        {
            message: 'Today is September 26',
            sender: 'robot',
            id : 'id4'
        } ] );
 
        const chatMessages = array[0];
        const setChatMessages = array[1];
        return (<div className="app-container">

        <ChatMessages 
          chatMessages={chatMessages}
        />

        <ChatInput 
           chatMessages={chatMessages}
           setChatMessages={setChatMessages}
         />        
      </div>);  

      } 

      //const app = ( App );
      const container = document.querySelector('.js-container');
      ReactDOM.createRoot(container).render(<App />);
    </script>
  </body>
</html>

No comments:

Post a Comment