Skip to content

Commit

Permalink
Merge pull request #52090 from bernhardoj/fix/51431-list-isn't-scroll…
Browse files Browse the repository at this point in the history
…ed-to-bottom-when-edit-message-with-up-arrow-key

Fix chat list isn't scrolled to bottom when edit message using arrow up key
  • Loading branch information
rlinoz authored Nov 13, 2024
2 parents 4d9be4e + 3be82d5 commit 67d7299
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/components/FlatList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,31 @@ function MVCPFlatList<TItem>({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();
Expand All @@ -141,6 +144,7 @@ function MVCPFlatList<TItem>({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) => {
Expand All @@ -150,6 +154,12 @@ function MVCPFlatList<TItem>({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) {
Expand All @@ -162,7 +172,7 @@ function MVCPFlatList<TItem>({maintainVisibleContentPosition, horizontal = false
return;
}

adjustForMaintainVisibleContentPosition();
adjustForMaintainVisibleContentPosition(!isEditComposerAdded);
prepareForMaintainVisibleContentPosition();
});
mutationObserver.observe(contentView, {
Expand Down

0 comments on commit 67d7299

Please sign in to comment.