From dc4c905dd1f267efb898a60a788c73d34fd8a238 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Mon, 18 Apr 2022 23:46:31 -0700 Subject: [PATCH] Allow specifying scroll behavior on initial chat history load --- webroot/js/components/chat/chat.js | 2 +- webroot/js/utils/helpers.js | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/webroot/js/components/chat/chat.js b/webroot/js/components/chat/chat.js index 244ed5e29..f5f523af4 100644 --- a/webroot/js/components/chat/chat.js +++ b/webroot/js/components/chat/chat.js @@ -187,7 +187,7 @@ export default class Chat extends Component { this.handleNetworkingError(`Fetch getChatHistory: ${error}`); } - this.scrollToBottom(); + jumpToBottom(this.scrollableMessagesContainer.current, 'instant'); } receivedWebsocketMessage(message) { diff --git a/webroot/js/utils/helpers.js b/webroot/js/utils/helpers.js index 149d3a622..d12cdda2e 100644 --- a/webroot/js/utils/helpers.js +++ b/webroot/js/utils/helpers.js @@ -24,15 +24,19 @@ export function clearLocalStorage(key) { } // jump down to the max height of a div, with a slight delay -export function jumpToBottom(element) { +export function jumpToBottom(element, behavior) { if (!element) return; + if (!behavior) { + behavior = document.visibilityState === 'visible' ? 'smooth' : 'instant'; + } + setTimeout( () => { element.scrollTo({ top: element.scrollHeight, left: 0, - behavior: document.visibilityState === 'visible' ? 'smooth' : 'instant', + behavior: behavior, }); }, 50,