forked from brandonmowat/react-chat-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
201 lines (176 loc) · 5.32 KB
/
index.tsx
File metadata and controls
201 lines (176 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2017 Brandon Mowat
// Written, developed, and designed by Brandon Mowat for the purpose of helping
// other developers make chat interfaces.
import * as React from "react";
import BubbleGroup from "../BubbleGroup";
import DefaultChatBubble from "../ChatBubble";
import ChatInput from "../ChatInput";
import Message from "../Message";
import styles from "./styles";
// Model for ChatFeed props.
interface ChatFeedInterface {
props: {
bubblesCentered?: boolean;
bubbleStyles?: object;
chatBubble?: React.Component;
hasInputField?: boolean;
isTyping?: boolean;
maxHeight?: number;
messages: any;
preventConflictingAutoScroll?: boolean;
showSenderName?: boolean;
};
}
// React component to render a complete chat feed
export default class ChatFeed extends React.Component<ChatFeedInterface> {
static defaultProps = {
preventConflictingAutoScroll: true,
};
// If the user scrolls this close to the bottom of the feed, we will re-enable autoscroll
static MANUAL_SCROLL_BOTTOM_MARGIN = 20;
props;
chat: {
scrollHeight: number;
clientHeight: number;
scrollTop: number;
addEventListener: Function;
removeEventListener: Function;
querySelectorAll: Function;
};
_hasUserScrolledUp: boolean = false;
_scrollOnLoadChatLogId: string;
_scrollOnLoadClassName: string;
constructor(props: ChatFeedInterface) {
super(props);
this.handleScrollEvent = this.handleScrollEvent.bind(this);
}
componentDidMount() {
if (this._scrollOnLoadChatLogId) {
this.scrollToChatLogId(
this._scrollOnLoadChatLogId,
this._scrollOnLoadClassName
);
} else {
this.scrollToBottom();
}
this.chat.addEventListener("scroll", this.handleScrollEvent);
}
componentDidUpdate() {
const { preventConflictingAutoScroll } = this.props;
if (preventConflictingAutoScroll && this._hasUserScrolledUp) {
return;
}
if (this._scrollOnLoadChatLogId) {
this.scrollToChatLogId(
this._scrollOnLoadChatLogId,
this._scrollOnLoadClassName
);
} else {
this.scrollToBottom();
}
}
private getMaxScrollTop(): number {
if (!this.chat) return 0;
const scrollHeight = this.chat.scrollHeight;
const height = this.chat.clientHeight;
return scrollHeight - height;
}
scrollToBottom() {
const maxScrollTop = this.getMaxScrollTop();
this.chat.scrollTop = maxScrollTop > 0 ? maxScrollTop : 0;
}
componentWillUnmount(): void {
this.chat.removeEventListener("scroll", this.handleScrollEvent);
}
private handleScrollEvent(event: Event) {
if (!this.chat) return;
const maxScrollTop = this.getMaxScrollTop();
if (
this.chat.scrollTop <
maxScrollTop - ChatFeed.MANUAL_SCROLL_BOTTOM_MARGIN
) {
this._hasUserScrolledUp = true;
} else {
this._hasUserScrolledUp = false;
}
}
scrollToChatLogId(chatLogId: string, className?: string) {
try {
const viewPortHeight = this.chat.clientHeight;
const scrollTopAdjustment = viewPortHeight / 2;
const matchingNodes = this.chat.querySelectorAll(
`[data-chat-log-id="${chatLogId}"]`
);
// Store ID to trigger scroll once message loads
if (matchingNodes.length === 0) {
this._scrollOnLoadChatLogId = chatLogId;
this._scrollOnLoadClassName = className;
return;
}
this._scrollOnLoadChatLogId = undefined;
this._scrollOnLoadClassName = undefined;
this._hasUserScrolledUp = true;
const chatBubbleNode = matchingNodes[matchingNodes.length - 1];
const parentElement = chatBubbleNode.parentElement;
this.chat.scrollTop = parentElement.offsetTop - scrollTopAdjustment;
if (className) {
parentElement.classList.remove(className);
// Trigger a reflow
void chatBubbleNode.offsetWidth;
parentElement.classList.add(className);
}
} catch {}
}
/**
* Determines what type of message/messages to render.
*/
renderMessages(messages: [Message]) {
const { isTyping, bubbleStyles, chatBubble } = this.props;
const ChatBubble = chatBubble || DefaultChatBubble;
const messageNodes = messages.map((message, index) => {
return (
<ChatBubble
bubbleStyles={bubbleStyles}
key={message.id || index}
message={message}
/>
);
});
// Other end is typing...
if (isTyping) {
messageNodes.push(
<div key="isTyping" style={{ ...styles.chatbubbleWrapper }}>
<ChatBubble
message={new Message({ id: 1, message: "...", senderName: "" })}
bubbleStyles={bubbleStyles}
/>
</div>
);
}
// return nodes
return <div style={styles.chatbubbleWrapper}>{messageNodes}</div>;
}
/**
* render : renders our chatfeed
*/
render() {
const inputField = this.props.hasInputField && <ChatInput />;
const { maxHeight } = this.props;
return (
<div id="chat-panel" style={styles.chatPanel}>
<div
ref={(c) => {
this.chat = c;
}}
className="chat-history"
style={{ ...styles.chatHistory, maxHeight }}
>
<div className="chat-messages">
{this.renderMessages(this.props.messages)}
</div>
</div>
{inputField}
</div>
);
}
}