项目作者: kingofthestack

项目描述 :
Intercom-style live chat window written in react
高级语言: JavaScript
项目地址: git://github.com/kingofthestack/react-chat-window.git
创建时间: 2017-06-19T04:52:11Z
项目社区:https://github.com/kingofthestack/react-chat-window

开源协议:The Unlicense

下载


react-chat-window

react-chat-window provides an intercom-like chat window that can be included easily in any project for free. It provides no messaging facilities, only the view component.

GitHub license






Demo gif of react-chat-window being used

Features

  • Customizeable
  • Backend agnostic
  • Free

Demo

Table of Contents

Installation

  1. $ npm install react-chat-window

Example

  1. import React, {Component} from 'react'
  2. import {Launcher} from 'react-chat-window'
  3. class Demo extends Component {
  4. constructor() {
  5. super();
  6. this.state = {
  7. messageList: []
  8. };
  9. }
  10. _onMessageWasSent(message) {
  11. this.setState({
  12. messageList: [...this.state.messageList, message]
  13. })
  14. }
  15. _sendMessage(text) {
  16. if (text.length > 0) {
  17. this.setState({
  18. messageList: [...this.state.messageList, {
  19. author: 'them',
  20. type: 'text',
  21. data: { text }
  22. }]
  23. })
  24. }
  25. }
  26. render() {
  27. return (<div>
  28. <Launcher
  29. agentProfile={{
  30. teamName: 'react-chat-window',
  31. imageUrl: 'https://a.slack-edge.com/66f9/img/avatars-teams/ava_0001-34.png'
  32. }}
  33. onMessageWasSent={this._onMessageWasSent.bind(this)}
  34. messageList={this.state.messageList}
  35. showEmoji
  36. ></Launcher>
  37. </div>)
  38. }
  39. }

For more detailed examples see the demo folder.

Components

Launcher

Launcher is the only component needed to use react-chat-window. It will react dynamically to changes in messages. All new messages must be added via a change in props as shown in the example.

Launcher props:

prop type required description
agentProfile object yes Represents your product or service’s customer service agent. Fields: imageUrl (string), teamName (string).
handleClick function yes Intercept the click event on the launcher. No argument sent when function is called.
isOpen boolean yes Force the open/close state of the chat window. If this is not set, it will open and close when clicked.
messageList [message] yes An array of message objects to be rendered as a conversation.
mute boolean no Don’t play sound for incoming messages. Defaults to false.
newMessagesCount number no The number of new messages. If greater than 0, this number will be displayed in a badge on the launcher. Defaults to 0.
onFilesSelected function(fileList) no Called after file has been selected from dialogue in chat window.
onMessageWasSent function(message) yes Called when a message is sent, with a message object as an argument.
showEmoji boolean no Whether or not to show the emoji button in the input bar. Defaults to true.

Message Objects

Message objects are rendered differently depending on their type. Currently, only text, file, and emoji types are supported. Each message object has an author field which can have the value ‘me’ or ‘them’.

  1. {
  2. author: 'them',
  3. type: 'text',
  4. data: {
  5. text: 'some text'
  6. }
  7. }
  8. {
  9. author: 'me',
  10. type: 'emoji',
  11. data: {
  12. code: 'someCode'
  13. }
  14. }
  15. {
  16. author: 'me',
  17. type: 'file',
  18. data: {
  19. url: 'somefile.mp3',
  20. fileName: 'Any old name'
  21. }
  22. }

Agent Profile Objects

Look like this:

  1. {
  2. imageUrl: 'https://somewhere.on/the_web.png',
  3. teamName: 'Da best'
  4. }