From 375066f7eecfefb2195dbb4b26e49b703f15da85 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Sat, 5 Aug 2023 22:21:01 +0300 Subject: [PATCH] scroll down chats on height change --- src/js/views/chat/ChatMessages.tsx | 43 +++++++++++++++++++----------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/js/views/chat/ChatMessages.tsx b/src/js/views/chat/ChatMessages.tsx index 97d31f9a..b7a42ddb 100644 --- a/src/js/views/chat/ChatMessages.tsx +++ b/src/js/views/chat/ChatMessages.tsx @@ -1,4 +1,4 @@ -import { useCallback } from 'react'; +import { useCallback, useLayoutEffect } from 'react'; import { Helmet } from 'react-helmet'; import { ChevronDownIcon } from '@heroicons/react/24/outline'; import { QrCodeIcon } from '@heroicons/react/24/solid'; @@ -24,7 +24,7 @@ import { addGroup, sendSecretInvite } from './NewChat'; export type DecryptedEvent = Event & { text?: string }; function ChatMessages({ id }) { - const ref = useRef(null); + const ref = useRef(null as any); const messages = useRef(new SortedMap('created_at')); const [sortedMessages, setSortedMessages] = useState([] as any[]); const [stickToBottom, setStickToBottom] = useState(true); @@ -48,11 +48,11 @@ function ChatMessages({ id }) { const center = $('
') .css({ position: 'fixed', top: 70, 'text-align': 'center' }) .attr('id', 'floating-day-separator') - .width($('#message-view').width()) + .width($(ref.current).width()) .append(s); $('#floating-day-separator').remove(); setTimeout(() => s.fadeOut(), 2000); - $('#message-view').prepend(center); + $(ref.current).prepend(center); }; const onClickSecretInvite = () => { @@ -61,7 +61,7 @@ function ChatMessages({ id }) { }; const toggleScrollDownBtn = () => { - const el = $('#message-view'); + const el = $(ref.current); const scrolledToBottom = el[0].scrollHeight - el.scrollTop() <= el.outerHeight() + 200; if (scrolledToBottom) { $('#scroll-down-btn:visible').fadeOut(150); @@ -89,9 +89,8 @@ function ChatMessages({ id }) { }; const scrollDown = () => { - Helpers.scrollToMessageListBottom(); - const el = document.getElementById('message-list'); - el && (el.style.paddingBottom = '0'); + const el = ref.current; + el?.scrollTo({ top: el.scrollHeight }); }; const renderMainView = () => { @@ -142,11 +141,11 @@ function ChatMessages({ id }) { mainView = (
onMessageViewScroll()} > -
+
{msgListContent}
@@ -225,6 +224,20 @@ function ChatMessages({ id }) { ); }; + // on ref.current height change scroll down. TODO only if stickToBottom + useLayoutEffect(() => { + const el = ref.current; + if (el) { + const observer = new ResizeObserver(() => { + scrollDown(); + }); + observer.observe(el); + return () => { + observer.disconnect(); + }; + } + }); + useEffect(() => { const hexId = Key.toNostrHexAddress(id); const subscribePrivate = (hexId) => { @@ -279,11 +292,11 @@ function ChatMessages({ id }) { subscribeGroup(); } - const container = document.getElementById('message-list'); - if (container) { - const el = $('#message-view'); + const el = $(ref.current); + if (el) { el.off('scroll').on('scroll', () => { - const scrolledToBottom = el[0].scrollHeight - el.scrollTop() == el.outerHeight(); + const scrolledToBottom = el.scrollTop() + el.innerHeight() >= el[0].scrollHeight; + console.log('scrolledToBottom', scrolledToBottom); if (stickToBottom && !scrolledToBottom) { setStickToBottom(false); } else if (!stickToBottom && scrolledToBottom) { @@ -312,7 +325,7 @@ function ChatMessages({ id }) { {'Messages'} -
+
{renderMainView()} 4}>{renderMsgForm()}