Skip to content

Fix typing indicator - Round #2 #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class Demo extends Component {
constructor() {
super();
this.state = {
messageList: []
messageList: [],
isTyping: true
};
}

Expand Down Expand Up @@ -74,6 +75,7 @@ class Demo extends Component {
onMessageWasSent={this._onMessageWasSent.bind(this)}
messageList={this.state.messageList}
showEmoji
showTypingIndicator={this.state.isTyping}
/>
</div>)
}
Expand All @@ -92,15 +94,16 @@ Launcher props:

| prop | type | required | description |
|------------------|--------|----------|-------------|
| agentProfile | [object](#agent-profile-objects) | 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](#message-objects)] | 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](https://developer.mozilla.org/en-US/docs/Web/API/FileList)) | no | Called after file has been selected from dialogue in chat window. |
| onMessageWasSent | function([message](#message-objects)) | 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`.
| agentProfile | [object](#agent-profile-objects) | 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](#message-objects)] | 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](https://developer.mozilla.org/en-US/docs/Web/API/FileList)) | no | Called after file has been selected from dialogue in chat window. |
| onMessageWasSent | function([message](#message-objects)) | 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`.
| showTypingIndicator | boolean | no | Whether or not to show typing indicator in the message box. Defaults to `false`.


### Message Objects
Expand Down
22 changes: 22 additions & 0 deletions demo/src/TestArea.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import React, { Component } from 'react';

class TestArea extends Component {
constructor (props) {
super(props);

this.timeout = null;
this._onKeyUp = this._onKeyUp.bind(this);
}

_onKeyUp() {
if (this.timeout) {
clearTimeout(this.timeout);
}

this.props.startTyping();

// stop typing after 1 second of inactivity
this.timeout = setTimeout(() => {
this.props.stopTyping();
}, 1000);
}

render () {
return (
<div className="demo-test-area--wrapper">
Expand All @@ -13,13 +33,15 @@ class TestArea extends Component {
<form className="demo-test-area" onSubmit={(e)=> {
e.preventDefault();
this.props.onMessage(this.textArea.value);
this.props.stopTyping();
this.textArea.value = '';
}}>
<div className="demo-test-area--preamble">Test the chat window by sending a message:</div>
<textarea
ref={(e) => { this.textArea = e; }}
className="demo-test-area--text"
placeholder="Write a test message...."
onKeyUp={this._onKeyUp}
/>
<button className="demo-test-area--button"> Send Message! </button>
</form>
Expand Down
18 changes: 17 additions & 1 deletion demo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Demo extends Component {
this.state = {
messageList: messageHistory,
newMessagesCount: 0,
isOpen: false
isOpen: false,
isTyping: false
};
}

Expand Down Expand Up @@ -54,6 +55,18 @@ class Demo extends Component {
}
}

_startTyping() {
this.setState({
isTyping: true
});
}

_stopTyping() {
this.setState({
isTyping: false
});
}

_handleClick() {
this.setState({
isOpen: !this.state.isOpen,
Expand All @@ -66,6 +79,8 @@ class Demo extends Component {
<Header />
<TestArea
onMessage={this._sendMessage.bind(this)}
startTyping={this._startTyping.bind(this)}
stopTyping={this._stopTyping.bind(this)}
/>
<Launcher
agentProfile={{
Expand All @@ -79,6 +94,7 @@ class Demo extends Component {
handleClick={this._handleClick.bind(this)}
isOpen={this.state.isOpen}
showEmoji
showTypingIndicator={this.state.isTyping}
/>
<img className="demo-monster-img" src={monsterImgUrl} />
<Footer />
Expand Down
4 changes: 3 additions & 1 deletion src/components/ChatWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ChatWindow extends Component {
<MessageList
messages={messageList}
imageUrl={this.props.agentProfile.imageUrl}
showTypingIndicator={this.props.showTypingIndicator}
/>
<UserInput
onSubmit={this.onUserInputSubmit.bind(this)}
Expand All @@ -51,7 +52,8 @@ ChatWindow.propTypes = {
onClose: PropTypes.func.isRequired,
onFilesSelected: PropTypes.func,
onUserInputSubmit: PropTypes.func.isRequired,
showEmoji: PropTypes.bool
showEmoji: PropTypes.bool,
showTypingIndicator: PropTypes.bool
};

export default ChatWindow;
5 changes: 4 additions & 1 deletion src/components/Launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Launcher extends Component {
isOpen={isOpen}
onClose={this.handleClick.bind(this)}
showEmoji={this.props.showEmoji}
showTypingIndicator={this.props.showTypingIndicator}
/>
</div>
);
Expand All @@ -84,11 +85,13 @@ Launcher.propTypes = {
messageList: PropTypes.arrayOf(PropTypes.object),
mute: PropTypes.bool,
showEmoji: PropTypes.bool,
showTypingIndicator: PropTypes.bool
};

Launcher.defaultProps = {
newMessagesCount: 0,
showEmoji: true
showEmoji: true,
showTypingIndicator: false
};

export default Launcher;
2 changes: 2 additions & 0 deletions src/components/MessageList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react';
import Message from './Messages';
import TypingIndicator from './TypingIndicator';

class MessageList extends Component {

Expand All @@ -13,6 +14,7 @@ class MessageList extends Component {
{this.props.messages.map((message, i) => {
return <Message message={message} key={i} />;
})}
{this.props.showTypingIndicator && <TypingIndicator />}
</div>);
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/components/TypingIndicator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

function TypingIndicator () {
return (
<div className="sc-typing-indicator">
<div className="sc-typing-indicator--message">
<div className="sc-typing-indicator--dot"></div>
<div className="sc-typing-indicator--dot"></div>
<div className="sc-typing-indicator--dot"></div>
</div>
</div>
);
}


export default TypingIndicator;
1 change: 1 addition & 0 deletions src/styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import './header.css';
import './message.css';
import './user-input.css';
import './popup-window.css';
import './typing-indicator.css';
52 changes: 52 additions & 0 deletions src/styles/typing-indicator.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.sc-typing-indicator {
padding-top: 20px;
width: 300px;
margin: auto;
}

.sc-typing-indicator--message {
width: 28px;
height: 22px;
padding: 5px 8px;
border-radius: 15px;
background-color: #f4f7f9;
display: flex;
justify-content: center;
align-items: center;
}

.sc-typing-indicator--dot {
background-color: #90949c;
animation: typing-indicator 1.5s infinite ease-in-out;
display: inline-block;
border-radius: 2px;
margin-right: 2px;
height: 4px;
width: 4px;
}

@keyframes typing-indicator {
0% {
transform: translateY(0px)
}

28% {
transform: translateY(-5px)
}

44% {
transform: translateY(0px)
}
}

.sc-typing-indicator--dot:nth-child(1) {
animation-delay: 200ms;
}

.sc-typing-indicator--dot:nth-child(2) {
animation-delay: 300ms;
}

.sc-typing-indicator--dot:nth-child(3) {
animation-delay: 400ms;
}