Skip to content

Repository files navigation

React Chat Widget

circle-cinpm

FEArmy

Features

  • Plain text message UI
  • Snippet style for links (only as responses for now)
  • Fully customizable
  • Easy to use

demonstration

Installation

npm

npm install --save react-chat-widget

yarn

yarn add react-chat-widget

Usage

1- Add the Widget component to your root component

importReactfrom'react';import{Widget}from'react-chat-widget';import'react-chat-widget/lib/styles.css';functionApp(){return(<divclassName="App"><Widget/></div>);}exportdefaultApp;

2- The only required prop you need to use is the handleNewUserMessage, which will receive the input from the user.

importReactfrom'react';import{Widget}from'react-chat-widget';import'react-chat-widget/lib/styles.css';functionApp(){consthandleNewUserMessage=(newMessage)=>{console.log(`New message incoming! ${newMessage}`);// Now send the message throught the backend API};return(<divclassName="App"><WidgethandleNewUserMessage={handleNewUserMessage}/></div>);}exportdefaultApp;

3- Import the methods for you to add messages in the Widget. (See messages)

importReactfrom'react';import{Widget,addResponseMessage}from'react-chat-widget';import'react-chat-widget/lib/styles.css';functionApp(){useEffect(()=>{addResponseMessage('Welcome to this awesome chat!');},[]);consthandleNewUserMessage=(newMessage)=>{console.log(`New message incoming! ${newMessage}`);// Now send the message throught the backend APIaddResponseMessage(response);};return(<divclassName="App"><WidgethandleNewUserMessage={handleNewUserMessage}/></div>);}exportdefaultApp;

4- Customize the widget to match your app design! You can add both props to manage the title of the widget and the avatar it will use. Of course, feel free to change the styles the widget will have in the CSS

importReact,{useEffect}from'react';import{Widget,addResponseMessage}from'react-chat-widget';import'react-chat-widget/lib/styles.css';importlogofrom'./logo.svg';functionApp(){useEffect(()=>{addResponseMessage('Welcome to this **awesome** chat!');},[]);consthandleNewUserMessage=(newMessage)=>{console.log(`New message incoming! ${newMessage}`);// Now send the message throught the backend API};return(<divclassName="App"><WidgethandleNewUserMessage={handleNewUserMessage}profileAvatar={logo}title="My new awesome title"subtitle="And my cool subtitle"/></div>);}exportdefaultApp;

API

Props

proptyperequireddefault valuedescription
handleNewUserMessage(...args: any[]) => anyYESFunction to handle the user input, will receive the full text message when submitted
titlestringNO'Welcome'Title of the widget
subtitlestringNO'This is your chat subtitle'Subtitle of the widget
senderPlaceHolderstringNO'Type a message...'The placeholder of the message input
profileAvatarstringNOThe profile image that will be set on the responses
profileClientAvatarstringNOThe profile image that will be set on the client messages
titleAvatarstringNOThe picture image that will be shown next to the chat title
showCloseButtonbooleanNOfalseShow or hide the close button in full screen mode
fullScreenModebooleanNOfalseAllow the use of full screen in full desktop mode
autofocusbooleanNOtrueAutofocus or not the user input
launcher(handleToggle) => ElementTypeNOCustom Launcher component to use instead of the default
handleQuickButtonClicked(...args: any[]) => anyNOFunction to handle the user clicking a quick button, will receive the 'value' when clicked.
showTimeStampbooleanNOtrueShow time stamp on messages
chatIdstringNO'rcw-chat-container'Chat container id for a11y
handleToggle(...args: any[]) => anyNO'rcw-chat-container'Function to handle when the widget is toggled, will receive the toggle status
launcherOpenLabelstringNO'Open chat'Alt value for the laucher when closed
launcherCloseLabelstringNO'Close chat'Alt value for the laucher when open
launcherOpenImgstringNO''local or remote image url, if not provided it will show default image
launcherCloseImgstringNO''local or remote image url, if not provided it will show default image
sendButtonAltstringNO'Send'Send button alt for a11y purposes
handleTextInputChange(event) => anyNOProp that triggers on input change
handleSubmit(event) => anyNOProp that triggers when a message is submitted, used for custom validation
resizablebooleanNOfalseProp that allows to resize the widget by dragging it's left border
emojisbooleanNOfalseenable emoji picker
showBadgebooleanNOtrueProp that allows to show or hide the unread message badge

Styles

To change the styles you need the widget to have, simply override the CSS classes wrapping them within the containers and add your own style to them! All classes are prefixed with rcw- so they don't override your other classes in case you are not hasing them. To override, you can do, for expample:

.rcw-conversation-container> .rcw-header {
background-color: red;
}
.rcw-message> .rcw-response {
background-color: black;
color: white;
}

That way, you can leave the JS clean and keep the styles within the CSS.

Messages

As of v3.0, messages now have an optional ID that can be added on creation.If you want to add new messages, you can use the following methods:

  • addResponseMessage

    • params:
      • text: string (supports markdown)
      • id: string (optional)
    • Method to add a new message written as a response to a user input.
  • addUserMessage

    • params:
      • text: string (supports markdown)
      • id: string (optional)
    • This method will add a new message written as a user. Keep in mind it will not trigger the prop handleNewUserMessage()
  • addLinkSnippet

    • params:
      • link
    • Method to add a link snippet. You need to provide this method with a link object, which must be in the shape of:
      {title: 'My awesome link',link: 'https://github.com/Wolox/react-chat-widget',target: '_blank'}
    • By default, target value is _blank which will open the link in a new window.
  • renderCustomComponent

    • params:
      • component: Component to be render,
      • props: props the component needs,
      • showAvatar: boolean, default value: false; the component will be rendered with the avatar like the messages
    • Method to render a custom component inside the messages container. With this method, you can add whatever component you need the widget to have.
  • setQuickButtons

    • params:
      • buttons: An array of objects with the keys label and value

Markdown is supported for both the responses and user messages.

Widget behavior

You can also control certain actions of the widget:

  • toggleWidget

    • params: No params expected
    • This method is to toggle the widget at will without the need to trigger the click event on the launcher
  • toggleInputDisabled

    • params: No params expected
    • Method to toggle the availability of the message input for the user to write on
  • toggleMsgLoader

    • Toggles the message loader that shows as a "typing..." style.
  • deleteMessages*

    • params:
      • count: messages to delete counting from last to first
      • id: message id to delete
    • Delete messages that either have an id you previously set with the addResponseMessage or delete based on position or both of them. For example deleteMessages(2, 'myId') will delete the message that has the id myId and the previous message.
  • markAllAsRead

    • Marks all response messages as read. The user messages doesn't have the read/unread property.
  • setBadgeCount

    • params:
      • count: number
    • As of v3.0, the badge prop is being changed to be managed from within the Widget. This method is manually set the badge number.

Widget components

Custom Launcher

You can use a custom component for the Launcher if you need one that's not the default, simply use the launcher prop:

importReactfrom'react';import{Widget}from'react-chat-widget';
...
functionMyApp(){constgetCustomLauncher=(handleToggle)=><buttononClick={handleToggle}>This is my launcher component!</button>return(<Widget...launcher={handleToggle=>getCustomLauncher(handleToggle)}/>)}

getCustomLauncher() is a method that will return the Launcher component as seen in the example. By default, the function passed by that prop, will receive the handleToggle parameter which is the method that will toggle the widget.

About

This project is maintained by Martín Callegari and it was written by Wolox.

Wolox

About

Awesome chat widget for your React App

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages