OvidijusParsiunas / deep-chat

Fully customizable AI chatbot component for your website
https://deepchat.dev
MIT License
1.27k stars 175 forks source link

Floating Action Button #111

Closed HafizTariq-Dev closed 4 months ago

HafizTariq-Dev commented 4 months ago

Hi @OvidijusParsiunas

You are doing excellent work and this is what is really needed in the future and we will be seeing this on every website. I really appreciate your efforts you are putting in this project.

While I was just playing with the examples and an idea cross my mind. Most of the time, We see ChatBot on click of a Floating Action button placed on the Bottom right corner.

It is very rare case that some one embed the ChatBot in the page.

I've also checked the past Opened / Closed issues may be someone has pointed out this idea. Well I could not found it.

May be in the future someone is also looking for ready to place ChatBot component in the React App as a Floating Action Button.

As soon as component is placed, Floating Action Button will appear on the Bottom right corner and might be passing parameter of position ( Left, Right, Bottom , Top ), Either show ChatBot on click of a Floating Action Button or not, ChatBot component should be closed or opened when loaded First time, What sound to play when loading of component is completed,.

These are some parameter that we can configure when initializing the component.

Check this example for instance: React ChatBot link

You will see the Floating Action Button is placed on bottom right corner and when you explore the link further then you might see other cool options that you can add in your project.

Just to start with, Can you please share code example to use your component as a Floating Action Button?

I look forward to your kind response. Thank You

OvidijusParsiunas commented 4 months ago

Hi @HafizTariq-Dev. Currently this repo is focusing on optimising the core chatbot box component. Hence, the ability to minimise/maximise its layout (and other elements like a navbar) is something that we let the developers customise themselves in their own app. This has also been referenced in the following issue.

Having said that, I understand that the functionality that you have mentioned is very common and I am thinking of adding wrapper elements in the near future which will facilitate exactly what you need.

In the mean time, if you want to create a floating button that would expand/collapse the chat, you can use the following code to get you started (this is in React but can be tailored to any framework):

import {MessageContent} from 'deep-chat/dist/types/messages';
import {DeepChat} from 'deep-chat-react';
import React from 'react';

const messages: MessageContent[] = [];

function App() {
  const [isExpanded, setIsExpanded] = React.useState(false);

  function onNewMessage(newMessage: {message: MessageContent; isInitial: boolean}) {
    if (!newMessage.isInitial) {
      messages.push(newMessage.message);
    }
  }

  return (
    <div className="App">
      <div
        style={{
          transition: 'ease-in-out',
          transitionDuration: '0.4s',
          position: 'absolute',
          zIndex: 1,
          right: 20,
          bottom: 20,
          transform: isExpanded ? 'scale(1)' : 'scale(0)',
          transformOrigin: '100% 100%',
        }}
      >
        <div style={{width: '100%', height: 40, backgroundColor: 'blue', borderRadius: '10px 10px 0px 0px'}}>
          <button
            style={{
              color: 'white',
              backgroundColor: 'unset',
              border: 'unset',
              fontSize: '32px',
              float: 'right',
              marginRight: '5px',
              cursor: 'pointer',
            }}
            onClick={() => {
              setIsExpanded((prev) => !prev);
            }}
          >
            ×
          </button>
        </div>
        <DeepChat demo={true} onNewMessage={onNewMessage} initialMessages={messages} />
      </div>
      <button
        onClick={() => {
          setIsExpanded((prev) => !prev);
        }}
        style={{position: 'absolute', right: 20, bottom: 20}}
      >
        Click Here
      </button>
    </div>
  );
}

export default App;

You can change the above elements and styling properties to fit your parent website. Because React re-renders the parent app when isExpanded state changes, we need to persist messages so that we can use them in the initialMessages property - for the example above I simply store it in a variable, but you can tailor it to your use-case.

Here is also a live example.

Hope this helps you get started.

HafizTariq-Dev commented 4 months ago

Hi @OvidijusParsiunas

I tried to check the Live example and I think the link is expire. Moreover I tried to run the React code with express server but with the help of the doc I could not manage to execute the code successfully.

Can you share the doc link or setup working repo in which React with Express is setup and running and sending response successfully?

Your help will be really much appreciated.

I look forward to your quick response.

Thank You

OvidijusParsiunas commented 4 months ago

Hey @HafizTariq-Dev. Not sure why the project was removed. Here is a link to a new one, hopefully it works for you.

Setting up the project with Express is a little bit out of scope for this issue. For a quick reference, you can clone the project locally and run the following ui project which has already been set up to run with the following express server. If you run both of these, they will work for you right out of the box. If you have any issues when running these, please create a new issue (to keep the focus relevant to the issue topic) and I will help you there.

Thanks!

OvidijusParsiunas commented 4 months ago

I will be closing this issue since the original request has now been resolved. Feel free to comment below if you have other questions or create a new issue for anything else. Thanks!