diff --git a/src/components/FlatList/index.tsx b/src/components/FlatList/index.tsx index be0227375470..52ccf50cd722 100644 --- a/src/components/FlatList/index.tsx +++ b/src/components/FlatList/index.tsx @@ -109,28 +109,31 @@ function MVCPFlatList({maintainVisibleContentPosition, horizontal = false } }, [getContentView, getScrollOffset, mvcpMinIndexForVisible, horizontal]); - const adjustForMaintainVisibleContentPosition = useCallback(() => { - if (mvcpMinIndexForVisible == null) { - return; - } + const adjustForMaintainVisibleContentPosition = useCallback( + (animated = true) => { + if (mvcpMinIndexForVisible == null) { + return; + } - const firstVisibleView = firstVisibleViewRef.current; - const prevFirstVisibleOffset = prevFirstVisibleOffsetRef.current; - if (firstVisibleView == null || !firstVisibleView.isConnected || prevFirstVisibleOffset == null) { - return; - } + const firstVisibleView = firstVisibleViewRef.current; + const prevFirstVisibleOffset = prevFirstVisibleOffsetRef.current; + if (firstVisibleView == null || !firstVisibleView.isConnected || prevFirstVisibleOffset == null) { + return; + } - const firstVisibleViewOffset = horizontal ? firstVisibleView.offsetLeft : firstVisibleView.offsetTop; - const delta = firstVisibleViewOffset - prevFirstVisibleOffset; - if (Math.abs(delta) > (IS_MOBILE_SAFARI ? 100 : 0.5)) { - const scrollOffset = lastScrollOffsetRef.current; - prevFirstVisibleOffsetRef.current = firstVisibleViewOffset; - scrollToOffset(scrollOffset + delta, false, true); - if (mvcpAutoscrollToTopThresholdRef.current != null && scrollOffset <= mvcpAutoscrollToTopThresholdRef.current) { - scrollToOffset(0, true, false); + const firstVisibleViewOffset = horizontal ? firstVisibleView.offsetLeft : firstVisibleView.offsetTop; + const delta = firstVisibleViewOffset - prevFirstVisibleOffset; + if (Math.abs(delta) > (IS_MOBILE_SAFARI ? 100 : 0.5)) { + const scrollOffset = lastScrollOffsetRef.current; + prevFirstVisibleOffsetRef.current = firstVisibleViewOffset; + scrollToOffset(scrollOffset + delta, false, true); + if (mvcpAutoscrollToTopThresholdRef.current != null && scrollOffset <= mvcpAutoscrollToTopThresholdRef.current) { + scrollToOffset(0, animated, false); + } } - } - }, [scrollToOffset, mvcpMinIndexForVisible, horizontal]); + }, + [scrollToOffset, mvcpMinIndexForVisible, horizontal], + ); const setupMutationObserver = useCallback(() => { const contentView = getContentView(); @@ -141,6 +144,7 @@ function MVCPFlatList({maintainVisibleContentPosition, horizontal = false mutationObserverRef.current?.disconnect(); const mutationObserver = new MutationObserver((mutations) => { + let isEditComposerAdded = false; // Check if the first visible view is removed and re-calculate it // if needed. mutations.forEach((mutation) => { @@ -150,6 +154,12 @@ function MVCPFlatList({maintainVisibleContentPosition, horizontal = false } firstVisibleViewRef.current = null; }); + mutation.addedNodes.forEach((node) => { + if (node.nodeType !== Node.ELEMENT_NODE || !(node as HTMLElement).querySelector('#composer')) { + return; + } + isEditComposerAdded = true; + }); }); if (firstVisibleViewRef.current == null) { @@ -162,7 +172,7 @@ function MVCPFlatList({maintainVisibleContentPosition, horizontal = false return; } - adjustForMaintainVisibleContentPosition(); + adjustForMaintainVisibleContentPosition(!isEditComposerAdded); prepareForMaintainVisibleContentPosition(); }); mutationObserver.observe(contentView, {