- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3.4k
##23220 WEB maintainVisibleContentPosition #28793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            thienlnam
  merged 17 commits into
  Expensify:main
from
margelo:feat/##23220-web-adjustForMaintainVisibleContentPosition
  
      
      
   
  Nov 4, 2023 
      
    
  
     Merged
                    Changes from 16 commits
      Commits
    
    
            Show all changes
          
          
            17 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9bc2771
              
                Merge branch 'main' of https://github.com/Expensify/App
              
              
                perunt ccb82d8
              
                Merge branch 'main' of https://github.com/Expensify/App
              
              
                perunt 8ba2000
              
                create MVCPScrollView
              
              
                perunt ef49188
              
                use renderScrollComponent
              
              
                perunt d467473
              
                Merge branch 'main' of https://github.com/Expensify/App into feat/##2…
              
              
                perunt 1beed0d
              
                Merge branch 'main' of https://github.com/Expensify/App into feat/##2…
              
              
                perunt 3cc63f8
              
                create MVCPScrollView
              
              
                perunt d82cf20
              
                use renderScrollComponent
              
              
                perunt 3c5d279
              
                update implementation
              
              
                janicduplessis 4d1657a
              
                Merge branch 'feat/##23220-web-adjustForMaintainVisibleContentPositio…
              
              
                perunt 6927201
              
                WIP testing maintainVisibleContentPosition
              
              
                perunt 71cf343
              
                Fix mvcp for inverted style rn patch
              
              
                janicduplessis bfea8cb
              
                Merge branch 'feat/##23220-web-adjustForMaintainVisibleContentPositio…
              
              
                perunt c242869
              
                clean code
              
              
                perunt da257ac
              
                Merge branch 'main' of https://github.com/Expensify/App into feat/##2…
              
              
                perunt ec8dfab
              
                types
              
              
                perunt c541460
              
                prettier
              
              
                perunt File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| /* eslint-disable es/no-optional-chaining, es/no-nullish-coalescing-operators, react/prop-types */ | ||
| import React from 'react'; | ||
| import {FlatList} from 'react-native'; | ||
| import PropTypes from 'prop-types'; | ||
|  | ||
| function mergeRefs(...args) { | ||
| return function forwardRef(node) { | ||
| args.forEach((ref) => { | ||
| if (ref == null) { | ||
| return; | ||
| } | ||
| if (typeof ref === 'function') { | ||
| ref(node); | ||
| return; | ||
| } | ||
| if (typeof ref === 'object') { | ||
| // eslint-disable-next-line no-param-reassign | ||
| ref.current = node; | ||
| return; | ||
| } | ||
| console.error(`mergeRefs cannot handle Refs of type boolean, number or string, received ref ${String(ref)}`); | ||
| }); | ||
| }; | ||
| } | ||
|  | ||
| function useMergeRefs(...args) { | ||
| return React.useMemo( | ||
| () => mergeRefs(...args), | ||
| // eslint-disable-next-line | ||
| [...args], | ||
| ); | ||
| } | ||
|  | ||
| const MVCPFlatList = React.forwardRef(({maintainVisibleContentPosition, horizontal, inverted, onScroll, ...props}, forwardedRef) => { | ||
| const {minIndexForVisible: mvcpMinIndexForVisible, autoscrollToTopThreshold: mvcpAutoscrollToTopThreshold} = maintainVisibleContentPosition ?? {}; | ||
| const scrollRef = React.useRef(null); | ||
| const prevFirstVisibleOffsetRef = React.useRef(null); | ||
| const firstVisibleViewRef = React.useRef(null); | ||
| const mutationObserverRef = React.useRef(null); | ||
| const lastScrollOffsetRef = React.useRef(0); | ||
|  | ||
| const getScrollOffset = React.useCallback(() => { | ||
| if (scrollRef.current == null) { | ||
| return 0; | ||
| } | ||
| return horizontal ? scrollRef.current.getScrollableNode().scrollLeft : scrollRef.current.getScrollableNode().scrollTop; | ||
| }, [horizontal]); | ||
|  | ||
| const getContentView = React.useCallback(() => scrollRef.current?.getScrollableNode().childNodes[0], []); | ||
|  | ||
| const scrollToOffset = React.useCallback( | ||
| (offset, animated) => { | ||
| const behavior = animated ? 'smooth' : 'instant'; | ||
| scrollRef.current?.getScrollableNode().scroll(horizontal ? {left: offset, behavior} : {top: offset, behavior}); | ||
| }, | ||
| [horizontal], | ||
| ); | ||
|  | ||
| const prepareForMaintainVisibleContentPosition = React.useCallback(() => { | ||
| if (mvcpMinIndexForVisible == null) { | ||
| return; | ||
| } | ||
|  | ||
| const contentView = getContentView(); | ||
| if (contentView == null) { | ||
| return; | ||
| } | ||
|  | ||
| const scrollOffset = getScrollOffset(); | ||
|  | ||
| const contentViewLength = contentView.childNodes.length; | ||
| for (let i = mvcpMinIndexForVisible; i < contentViewLength; i++) { | ||
| const subview = contentView.childNodes[inverted ? contentViewLength - i - 1 : i]; | ||
| const subviewOffset = horizontal ? subview.offsetLeft : subview.offsetTop; | ||
| if (subviewOffset > scrollOffset || i === contentViewLength - 1) { | ||
| prevFirstVisibleOffsetRef.current = subviewOffset; | ||
| firstVisibleViewRef.current = subview; | ||
| break; | ||
| } | ||
| } | ||
| }, [getContentView, getScrollOffset, mvcpMinIndexForVisible, horizontal, inverted]); | ||
|  | ||
| const adjustForMaintainVisibleContentPosition = React.useCallback(() => { | ||
| if (mvcpMinIndexForVisible == null) { | ||
| return; | ||
| } | ||
|  | ||
| const firstVisibleView = firstVisibleViewRef.current; | ||
| const prevFirstVisibleOffset = prevFirstVisibleOffsetRef.current; | ||
| if (firstVisibleView == null || prevFirstVisibleOffset == null) { | ||
| return; | ||
| } | ||
|  | ||
| const firstVisibleViewOffset = horizontal ? firstVisibleView.offsetLeft : firstVisibleView.offsetTop; | ||
| const delta = firstVisibleViewOffset - prevFirstVisibleOffset; | ||
| if (Math.abs(delta) > 0.5) { | ||
| const scrollOffset = getScrollOffset(); | ||
| prevFirstVisibleOffsetRef.current = firstVisibleViewOffset; | ||
| scrollToOffset(scrollOffset + delta, false); | ||
| if (mvcpAutoscrollToTopThreshold != null && scrollOffset <= mvcpAutoscrollToTopThreshold) { | ||
| scrollToOffset(0, true); | ||
| } | ||
| } | ||
| }, [getScrollOffset, scrollToOffset, mvcpMinIndexForVisible, mvcpAutoscrollToTopThreshold, horizontal]); | ||
|  | ||
| const setupMutationObserver = React.useCallback(() => { | ||
| const contentView = getContentView(); | ||
| if (contentView == null) { | ||
| return; | ||
| } | ||
|  | ||
| mutationObserverRef.current?.disconnect(); | ||
|  | ||
| const mutationObserver = new MutationObserver(() => { | ||
| // Chrome adjusts scroll position when elements are added at the top of the | ||
| // view. We want to have the same behavior as react-native / Safari so we | ||
| // reset the scroll position to the last value we got from an event. | ||
| const lastScrollOffset = lastScrollOffsetRef.current; | ||
| const scrollOffset = getScrollOffset(); | ||
| if (lastScrollOffset !== scrollOffset) { | ||
| scrollToOffset(lastScrollOffset, false); | ||
| } | ||
|  | ||
| // This needs to execute after scroll events are dispatched, but | ||
| // in the same tick to avoid flickering. rAF provides the right timing. | ||
| requestAnimationFrame(() => { | ||
| adjustForMaintainVisibleContentPosition(); | ||
| }); | ||
| }); | ||
| mutationObserver.observe(contentView, { | ||
| attributes: true, | ||
| childList: true, | ||
| subtree: true, | ||
| }); | ||
|  | ||
| mutationObserverRef.current = mutationObserver; | ||
| }, [adjustForMaintainVisibleContentPosition, getContentView, getScrollOffset, scrollToOffset]); | ||
|  | ||
| React.useEffect(() => { | ||
| prepareForMaintainVisibleContentPosition(); | ||
| setupMutationObserver(); | ||
| }, [prepareForMaintainVisibleContentPosition, setupMutationObserver]); | ||
|  | ||
| const setMergedRef = useMergeRefs(scrollRef, forwardedRef); | ||
|  | ||
| const onRef = React.useCallback( | ||
| (newRef) => { | ||
| // Make sure to only call refs and re-attach listeners if the node changed. | ||
| if (newRef == null || newRef === scrollRef.current) { | ||
| return; | ||
| } | ||
|  | ||
| setMergedRef(newRef); | ||
| prepareForMaintainVisibleContentPosition(); | ||
| setupMutationObserver(); | ||
| }, | ||
| [prepareForMaintainVisibleContentPosition, setMergedRef, setupMutationObserver], | ||
| ); | ||
|  | ||
| React.useEffect(() => { | ||
| const mutationObserver = mutationObserverRef.current; | ||
| return () => { | ||
| mutationObserver?.disconnect(); | ||
| }; | ||
| }, []); | ||
|  | ||
| const onScrollInternal = React.useCallback( | ||
| (ev) => { | ||
| lastScrollOffsetRef.current = getScrollOffset(); | ||
|  | ||
| prepareForMaintainVisibleContentPosition(); | ||
|  | ||
| onScroll?.(ev); | ||
| }, | ||
| [getScrollOffset, prepareForMaintainVisibleContentPosition, onScroll], | ||
| ); | ||
|  | ||
| return ( | ||
| <FlatList | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| horizontal={horizontal} | ||
| inverted={inverted} | ||
| onScroll={onScrollInternal} | ||
| scrollEventThrottle={1} | ||
| ref={onRef} | ||
| /> | ||
| ); | ||
| }); | ||
|  | ||
| MVCPFlatList.displayName = 'MVCPFlatList'; | ||
| MVCPFlatList.propTypes = { | ||
| maintainVisibleContentPosition: PropTypes.shape({ | ||
| minIndexForVisible: PropTypes.number.isRequired, | ||
| autoscrollToTopThreshold: PropTypes.number, | ||
| }), | ||
| horizontal: PropTypes.bool, | ||
| }; | ||
|  | ||
| MVCPFlatList.defaultProps = { | ||
| maintainVisibleContentPosition: null, | ||
| horizontal: false, | ||
| }; | ||
|  | ||
| export default MVCPFlatList; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import MVCPFlatList from './MVCPFlatList'; | ||
|  | ||
| export default MVCPFlatList; | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we try to resolve some of these lints instead of just disabling them? Or you could write this in TS if you'd like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to keep the code as close as possible as the PR it is based on so I disabled the lint rules instead of trying to fix it. I think this is ok since this code is temporary until proper maintainVisibleContentPosition is merged in rn-web.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool thanks for the context - agree with that