浏览代码

fix(chat): fix auto-scrolling to bottom

Empower the parent.
master
Leonard Kim 6 年前
父节点
当前提交
5cd0b1a9be
共有 2 个文件被更改,包括 52 次插入24 次删除
  1. 46
    1
      react/features/chat/components/web/Chat.js
  2. 6
    23
      react/features/chat/components/web/MessageContainer.js

+ 46
- 1
react/features/chat/components/web/Chat.js 查看文件

@@ -27,6 +27,12 @@ class Chat extends AbstractChat<Props> {
27 27
      */
28 28
     _isExited: boolean;
29 29
 
30
+    /**
31
+     * Reference to the React Component for displaying chat messages. Used for
32
+     * scrolling to the end of the chat messages.
33
+     */
34
+    _messageContainerRef: Object;
35
+
30 36
     /**
31 37
      * Initializes a new {@code Chat} instance.
32 38
      *
@@ -37,11 +43,34 @@ class Chat extends AbstractChat<Props> {
37 43
         super(props);
38 44
 
39 45
         this._isExited = true;
46
+        this._messageContainerRef = React.createRef();
40 47
 
41 48
         // Bind event handlers so they are only bound once for every instance.
42 49
         this._renderPanelContent = this._renderPanelContent.bind(this);
43 50
     }
44 51
 
52
+    /**
53
+     * Implements {@code Component#componentDidMount}.
54
+     *
55
+     * @inheritdoc
56
+     */
57
+    componentDidMount() {
58
+        this._scrollMessageContainerToBottom(true);
59
+    }
60
+
61
+    /**
62
+     * Implements {@code Component#componentDidUpdate}.
63
+     *
64
+     * @inheritdoc
65
+     */
66
+    componentDidUpdate(prevProps) {
67
+        if (this.props._messages !== prevProps._messages) {
68
+            this._scrollMessageContainerToBottom(true);
69
+        } else if (this.props._isOpen && !prevProps._isOpen) {
70
+            this._scrollMessageContainerToBottom(false);
71
+        }
72
+    }
73
+
45 74
     /**
46 75
      * Implements React's {@link Component#render()}.
47 76
      *
@@ -99,7 +128,9 @@ class Chat extends AbstractChat<Props> {
99 128
     _renderChat() {
100 129
         return (
101 130
             <>
102
-                <MessageContainer messages = { this.props._messages } />
131
+                <MessageContainer
132
+                    messages = { this.props._messages }
133
+                    ref = { this._messageContainerRef } />
103 134
                 <ChatInput />
104 135
             </>
105 136
         );
@@ -162,6 +193,20 @@ class Chat extends AbstractChat<Props> {
162 193
             </div>
163 194
         );
164 195
     }
196
+
197
+    /**
198
+     * Scrolls the chat messages so the latest message is visible.
199
+     *
200
+     * @param {boolean} withAnimation - Whether or not to show a scrolling
201
+     * animation.
202
+     * @private
203
+     * @returns {void}
204
+     */
205
+    _scrollMessageContainerToBottom(withAnimation) {
206
+        if (this._messageContainerRef.current) {
207
+            this._messageContainerRef.current.scrollToBottom(withAnimation);
208
+        }
209
+    }
165 210
 }
166 211
 
167 212
 export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));

+ 6
- 23
react/features/chat/components/web/MessageContainer.js 查看文件

@@ -31,24 +31,6 @@ export default class MessageContainer extends AbstractMessageContainer<Props> {
31 31
         this._messagesListEndRef = React.createRef();
32 32
     }
33 33
 
34
-    /**
35
-     * Implements React's {@link Component#componentDidMount()}.
36
-     *
37
-     * @inheritdoc
38
-     */
39
-    componentDidMount() {
40
-        this._scrollMessagesToBottom();
41
-    }
42
-
43
-    /**
44
-     * Updates chat input focus.
45
-     *
46
-     * @inheritdoc
47
-     */
48
-    componentDidUpdate() {
49
-        this._scrollMessagesToBottom();
50
-    }
51
-
52 34
     /**
53 35
      * Implements {@code Component#render}.
54 36
      *
@@ -75,17 +57,18 @@ export default class MessageContainer extends AbstractMessageContainer<Props> {
75 57
         );
76 58
     }
77 59
 
78
-    _getMessagesGroupedBySender: () => Array<Array<Object>>;
79
-
80 60
     /**
81 61
      * Automatically scrolls the displayed chat messages down to the latest.
82 62
      *
83
-     * @private
63
+     * @param {boolean} withAnimation - Whether or not to show a scrolling
64
+     * animation.
84 65
      * @returns {void}
85 66
      */
86
-    _scrollMessagesToBottom() {
67
+    scrollToBottom(withAnimation: boolean) {
87 68
         this._messagesListEndRef.current.scrollIntoView({
88
-            behavior: 'smooth'
69
+            behavior: withAnimation ? 'smooth' : 'auto'
89 70
         });
90 71
     }
72
+
73
+    _getMessagesGroupedBySender: () => Array<Array<Object>>;
91 74
 }

正在加载...
取消
保存