React Chat - Popup Integration
A common way to add a chat assistant to your website is by embedding it in a modal at the bottom right corner. Follow these steps:
- Place DevExtreme Floating Action Button at the bottom right of the window. Users can click it to open the popup.
- Position our Popup at the bottom right (or another preferred location).
- Add the Chat component for the virtual assistant.
The following code snippet demonstrates this scenario. Check the CSS tab for instructions on how to integrate Popup and Chat.
App.tsx
App.css
- import { Chat, SpeedDialAction, Popup } from 'devextreme-react';
- import { ChatTypes } from 'devextreme-react/chat';
- import 'devextreme/dist/css/dx.light.css'
- import { useCallback, useState } from 'react';
- import './App.css';
- const assistant = {
- id: "02",
- name: "Assistant",
- };
- export default function App() {
- const [popupVisible, setPopupVisible] = useState(false);
- const [messages, setMessages] = useState<ChatTypes.Message[]>([{
- id: 1,
- timestamp: new Date(),
- author: assistant,
- text: "Hello! How can I help you?"
- }]);
- const onMessageEntered = useCallback((e: ChatTypes.MessageEnteredEvent) => {
- setMessages((prevMessages) => [...prevMessages, e.message]);
- }, []);
- const showPopup = () => {
- setPopupVisible(true);
- }
- const hidePopup = () => {
- setPopupVisible(false);
- }
- return(
- <>
- <SpeedDialAction
- label="Chat"
- icon="send"
- onClick={showPopup}
- />
- <Popup
- visible={popupVisible}
- width={400}
- height={650}
- shading={false}
- dragEnabled={false}
- onHiding={hidePopup}
- position="right bottom"
- >
- <Chat
- id="chat"
- width={400}
- height={600}
- items={messages}
- onMessageEntered={onMessageEntered}
- />
- </Popup>
- </>
- );
- }
- #chat {
- border: none;
- }
- .dx-popup-content-scrollable {
- padding: 0;
- overflow: hidden;
- }