From cae7179c9459f12b1cb5e1a1d998a9dc45f927dc Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Fri, 12 Jan 2018 19:07:19 -0800 Subject: [PATCH] new feature to support smooth bi-directional content loading Summary: == Problem / Background == Most lists paginate in a single direction (standard infinite list), but some paginate in both directions. Most common example is a chat thread where new messages show up on the bottom, and old content can be loaded by scrolling up. Comment threads are another example. Right now, adding content to the bottom of a scroll view is smooth - the content doesn't jump. But when adding to the top of the scrollview, the content gets pushed down, which is jarring (note this may appear reversed because of inverting the list which is common for chat applications). == Approach == The basic idea is simple - we set a flag in JS, then for every uimanager transaction, we record which is the first eligible and visible view in the ScrollView, and compare it's new origin to the old one. If it has changed, we update the contentOffset of the ScrollView to compensate. This is done by observing `willPerformMounting` directly (only from scrollviews that have this new property set), and then observing the prev state with prependUIBlock and making the update synchronously in addUIBlock to avoid any flicker. There is also a way to skip views that we don't care about, like a spinner at the top of the view that we don't want to stay in place - we actually want it to get pushed up by the new content, replaced visually in the viewport. == Notes == Most chat applications will probably want to do a scrollToTop when new content comes in and the user is already scrolled at or near the bottom. This is glitchy if visible children are re-ordered, which could be fixed with additional logic, but it doesn't come up in the type of applications we're targetting here so punting on that. == Test Plan == https://youtu.be/4GcqDGz9eOE Reviewed By: shergin Differential Revision: D6696921 fbshipit-source-id: 822e7dfcb207006cd1ba098356324ea81f619428 --- Libraries/Components/ScrollView/ScrollView.js | 13 + RNTester/js/ScrollViewExample.js | 225 ++++++++++++++---- React/Views/ScrollView/RCTScrollView.h | 1 + React/Views/ScrollView/RCTScrollView.m | 70 +++++- React/Views/ScrollView/RCTScrollViewManager.m | 1 + 5 files changed, 261 insertions(+), 49 deletions(-) diff --git a/Libraries/Components/ScrollView/ScrollView.js b/Libraries/Components/ScrollView/ScrollView.js index b42dc736f..266e72836 100644 --- a/Libraries/Components/ScrollView/ScrollView.js +++ b/Libraries/Components/ScrollView/ScrollView.js @@ -233,6 +233,19 @@ const ScrollView = createReactClass({ * - `true`, deprecated, use 'always' instead */ keyboardShouldPersistTaps: PropTypes.oneOf(['always', 'never', 'handled', false, true]), + /** + * When non-null, the scroll view will adjust the scroll position so that the content at or + * beyond the specified index that is currently visible will not change position. This is useful + * for lists that are loading content in both directions, e.g. a chat thread, where new messages + * coming in might otherwise cause the scroll position to jump. A value of 1 can be used to skip + * a spinner that does not need to maintain position. The default value is null. + * + * Caveat: reordering elements in the scrollview with this enabled will probably cause jumpiness + * and jank. It can be fixed, but there are currently no plans to do so. + * + * @platform ios + */ + maintainPositionAtOrBeyondIndex: PropTypes.number, /** * The maximum allowed zoom scale. The default value is 1.0. * @platform ios diff --git a/RNTester/js/ScrollViewExample.js b/RNTester/js/ScrollViewExample.js index 0074ea91c..2d0dcde3d 100644 --- a/RNTester/js/ScrollViewExample.js +++ b/RNTester/js/ScrollViewExample.js @@ -12,10 +12,13 @@ */ 'use strict'; -var React = require('react'); -var ReactNative = require('react-native'); -var { - Platform, +import type {StyleObj} from 'StyleSheetTypes'; + +const ActivityIndicator = require('ActivityIndicator'); +const Platform = require('Platform'); +const React = require('react'); +const ReactNative = require('react-native'); +const { ScrollView, StyleSheet, Text, @@ -30,11 +33,11 @@ exports.description = 'Component that enables scrolling through child components'; exports.examples = [ { - title: '', + title: '\n', description: 'To make content scrollable, wrap it within a component', render: function() { - var _scrollView: ScrollView; + let _scrollView: ScrollView; return ( {THUMB_URLS.map(createThumbRow)} - { _scrollView.scrollTo({y: 0}); - }}> - Scroll to top - - +