From 37ce132ccec3b1405b73503c766eea0838ba65bb Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 12 May 2021 03:13:20 +0300 Subject: [PATCH] Message List: Fix scroll animation after window resize --- src/components/middle/MessageList.tsx | 7 +++++-- src/hooks/useWindowSize.ts | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/components/middle/MessageList.tsx b/src/components/middle/MessageList.tsx index e563f6f6..305254ba 100644 --- a/src/components/middle/MessageList.tsx +++ b/src/components/middle/MessageList.tsx @@ -52,6 +52,7 @@ import resetScroll from '../../util/resetScroll'; import fastSmoothScroll, { isAnimatingScroll } from '../../util/fastSmoothScroll'; import renderText from '../common/helpers/renderText'; import useLang, { LangFn } from '../../hooks/useLang'; +import useWindowSize from '../../hooks/useWindowSize'; import Loading from '../ui/Loading'; import MessageScroll from './MessageScroll'; @@ -328,9 +329,11 @@ const MessageList: FC = ({ }; }, []); - useLayoutEffect(() => { + // Memorize height for scroll animation + const { height: windowHeight } = useWindowSize(); + useEffect(() => { containerRef.current!.dataset.normalHeight = String(containerRef.current!.offsetHeight); - }, []); + }, [windowHeight]); // Workaround for an iOS bug when animated stickers sometimes disappear useLayoutEffect(() => { diff --git a/src/hooks/useWindowSize.ts b/src/hooks/useWindowSize.ts index f40a05a7..1b6c2c81 100644 --- a/src/hooks/useWindowSize.ts +++ b/src/hooks/useWindowSize.ts @@ -5,7 +5,7 @@ import { IDimensions } from '../modules/helpers'; import { throttle } from '../util/schedulers'; import windowSize from '../util/windowSize'; -const RESIZE_TIMEOUT_MS = 250; +const THROTTLE = 250; export default () => { const [size, setSize] = useState(windowSize.get()); @@ -13,14 +13,14 @@ export default () => { useEffect(() => { const handleResize = throttle(() => { setSize(windowSize.get()); - }, RESIZE_TIMEOUT_MS, false); + }, THROTTLE, false); window.addEventListener('resize', handleResize); return () => { window.removeEventListener('resize', handleResize); }; - }); + }, []); return size; };