mirror of
https://github.com/status-im/react-native.git
synced 2025-02-06 22:54:27 +00:00
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
328 lines
9.3 KiB
JavaScript
328 lines
9.3 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @flow
|
|
* @providesModule ScrollViewExample
|
|
* @format
|
|
*/
|
|
'use strict';
|
|
|
|
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,
|
|
TouchableOpacity,
|
|
View,
|
|
Image,
|
|
} = ReactNative;
|
|
|
|
exports.displayName = 'ScrollViewExample';
|
|
exports.title = '<ScrollView>';
|
|
exports.description =
|
|
'Component that enables scrolling through child components';
|
|
exports.examples = [
|
|
{
|
|
title: '<ScrollView>\n',
|
|
description:
|
|
'To make content scrollable, wrap it within a <ScrollView> component',
|
|
render: function() {
|
|
let _scrollView: ScrollView;
|
|
return (
|
|
<View>
|
|
<ScrollView
|
|
ref={scrollView => {
|
|
_scrollView = scrollView;
|
|
}}
|
|
automaticallyAdjustContentInsets={false}
|
|
onScroll={() => {
|
|
console.log('onScroll!');
|
|
}}
|
|
scrollEventThrottle={200}
|
|
style={styles.scrollView}>
|
|
{THUMB_URLS.map(createThumbRow)}
|
|
</ScrollView>
|
|
<Button
|
|
label="Scroll to top"
|
|
onPress={() => {
|
|
_scrollView.scrollTo({y: 0});
|
|
}}
|
|
/>
|
|
<Button
|
|
label="Scroll to bottom"
|
|
onPress={() => {
|
|
_scrollView.scrollToEnd({animated: true});
|
|
}}
|
|
/>
|
|
<Button
|
|
label="Flash scroll indicators"
|
|
onPress={() => {
|
|
_scrollView.flashScrollIndicators();
|
|
}}
|
|
/>
|
|
</View>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
title: '<ScrollView> (horizontal = true)\n',
|
|
description:
|
|
"You can display <ScrollView>'s child components horizontally rather than vertically",
|
|
render: function() {
|
|
function renderScrollView(
|
|
title: string,
|
|
addtionalStyles: typeof StyleSheet,
|
|
) {
|
|
let _scrollView: ScrollView;
|
|
return (
|
|
<View style={addtionalStyles}>
|
|
<Text style={styles.text}>{title}</Text>
|
|
<ScrollView
|
|
ref={scrollView => {
|
|
_scrollView = scrollView;
|
|
}}
|
|
automaticallyAdjustContentInsets={false}
|
|
horizontal={true}
|
|
style={[styles.scrollView, styles.horizontalScrollView]}>
|
|
{THUMB_URLS.map(createThumbRow)}
|
|
</ScrollView>
|
|
<Button
|
|
label="Scroll to start"
|
|
onPress={() => {
|
|
_scrollView.scrollTo({x: 0});
|
|
}}
|
|
/>
|
|
<Button
|
|
label="Scroll to end"
|
|
onPress={() => {
|
|
_scrollView.scrollToEnd({animated: true});
|
|
}}
|
|
/>
|
|
<Button
|
|
label="Flash scroll indicators"
|
|
onPress={() => {
|
|
_scrollView.flashScrollIndicators();
|
|
}}
|
|
/>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<View>
|
|
{renderScrollView('LTR layout', {direction: 'ltr'})}
|
|
{renderScrollView('RTL layout', {direction: 'rtl'})}
|
|
</View>
|
|
);
|
|
},
|
|
},
|
|
];
|
|
if (Platform.OS === 'ios') {
|
|
exports.examples.push({
|
|
title: '<ScrollView> smooth bi-directional content loading\n',
|
|
description:
|
|
'The `maintainPositionAtOrBeyondIndex` prop allows insertions to either end of the content ' +
|
|
'without causing the visible content to jump. Re-ordering is not supported.',
|
|
render: function() {
|
|
let itemCount = 6;
|
|
class AppendingList extends React.Component<{}, *> {
|
|
state = {
|
|
items: [...Array(itemCount)].map((_, ii) => (
|
|
<Thumb msg={`Item ${ii}`} />
|
|
)),
|
|
};
|
|
render() {
|
|
return (
|
|
<View>
|
|
<ScrollView
|
|
automaticallyAdjustContentInsets={false}
|
|
maintainPositionAtOrBeyondIndex={1}
|
|
style={styles.scrollView}>
|
|
<ActivityIndicator style={{height: 40}} />
|
|
{this.state.items.map(item =>
|
|
React.cloneElement(item, {key: item.props.msg}),
|
|
)}
|
|
</ScrollView>
|
|
<ScrollView
|
|
horizontal={true}
|
|
automaticallyAdjustContentInsets={false}
|
|
maintainPositionAtOrBeyondIndex={1}
|
|
style={[styles.scrollView, styles.horizontalScrollView]}>
|
|
<ActivityIndicator style={{height: 40}} />
|
|
{this.state.items.map(item =>
|
|
React.cloneElement(item, {key: item.props.msg, style: null}),
|
|
)}
|
|
</ScrollView>
|
|
<View style={styles.row}>
|
|
<Button
|
|
label="Add to top"
|
|
onPress={() => {
|
|
this.setState(state => {
|
|
const idx = itemCount++;
|
|
return {
|
|
items: [
|
|
<Thumb
|
|
style={{paddingTop: idx * 5}}
|
|
msg={`Item ${idx}`}
|
|
/>,
|
|
].concat(state.items),
|
|
};
|
|
});
|
|
}}
|
|
/>
|
|
<Button
|
|
label="Remove top"
|
|
onPress={() => {
|
|
this.setState(state => ({
|
|
items: state.items.slice(1),
|
|
}));
|
|
}}
|
|
/>
|
|
<Button
|
|
label="Change height top"
|
|
onPress={() => {
|
|
this.setState(state => ({
|
|
items: [
|
|
React.cloneElement(state.items[0], {
|
|
style: {paddingBottom: Math.random() * 40},
|
|
}),
|
|
].concat(state.items.slice(1)),
|
|
}));
|
|
}}
|
|
/>
|
|
</View>
|
|
<View style={styles.row}>
|
|
<Button
|
|
label="Add to end"
|
|
onPress={() => {
|
|
this.setState(state => ({
|
|
items: state.items.concat(
|
|
<Thumb msg={`Item ${itemCount++}`} />,
|
|
),
|
|
}));
|
|
}}
|
|
/>
|
|
<Button
|
|
label="Remove end"
|
|
onPress={() => {
|
|
this.setState(state => ({
|
|
items: state.items.slice(0, -1),
|
|
}));
|
|
}}
|
|
/>
|
|
<Button
|
|
label="Change height end"
|
|
onPress={() => {
|
|
this.setState(state => ({
|
|
items: state.items.slice(0, -1).concat(
|
|
React.cloneElement(
|
|
state.items[state.items.length - 1],
|
|
{
|
|
style: {paddingBottom: Math.random() * 40},
|
|
},
|
|
),
|
|
),
|
|
}));
|
|
}}
|
|
/>
|
|
</View>
|
|
</View>
|
|
);
|
|
}
|
|
}
|
|
return <AppendingList />;
|
|
},
|
|
});
|
|
}
|
|
|
|
class Thumb extends React.PureComponent<{|
|
|
source?: string | number,
|
|
msg?: string,
|
|
style?: StyleObj,
|
|
|}> {
|
|
render() {
|
|
const {source} = this.props;
|
|
return (
|
|
<View style={[styles.thumb, this.props.style]}>
|
|
<Image
|
|
style={styles.img}
|
|
source={source == null ? THUMB_URLS[6] : source}
|
|
/>
|
|
<Text>{this.props.msg}</Text>
|
|
</View>
|
|
);
|
|
}
|
|
}
|
|
|
|
let THUMB_URLS = [
|
|
require('./Thumbnails/like.png'),
|
|
require('./Thumbnails/dislike.png'),
|
|
require('./Thumbnails/call.png'),
|
|
require('./Thumbnails/fist.png'),
|
|
require('./Thumbnails/bandaged.png'),
|
|
require('./Thumbnails/flowers.png'),
|
|
require('./Thumbnails/heart.png'),
|
|
require('./Thumbnails/liking.png'),
|
|
require('./Thumbnails/party.png'),
|
|
require('./Thumbnails/poke.png'),
|
|
require('./Thumbnails/superlike.png'),
|
|
require('./Thumbnails/victory.png'),
|
|
];
|
|
|
|
THUMB_URLS = THUMB_URLS.concat(THUMB_URLS); // double length of THUMB_URLS
|
|
|
|
const createThumbRow = (uri, i) => <Thumb key={i} source={uri} />;
|
|
|
|
const Button = ({label, onPress}) => (
|
|
<TouchableOpacity style={styles.button} onPress={onPress}>
|
|
<Text>{label}</Text>
|
|
</TouchableOpacity>
|
|
);
|
|
|
|
const styles = StyleSheet.create({
|
|
scrollView: {
|
|
backgroundColor: '#eeeeee',
|
|
height: 300,
|
|
},
|
|
horizontalScrollView: {
|
|
height: 106,
|
|
},
|
|
text: {
|
|
fontSize: 16,
|
|
fontWeight: 'bold',
|
|
margin: 5,
|
|
},
|
|
button: {
|
|
margin: 5,
|
|
padding: 5,
|
|
alignItems: 'center',
|
|
backgroundColor: '#cccccc',
|
|
borderRadius: 3,
|
|
},
|
|
row: {
|
|
flexDirection: 'row',
|
|
justifyContent: 'space-around',
|
|
},
|
|
thumb: {
|
|
margin: 5,
|
|
padding: 5,
|
|
backgroundColor: '#cccccc',
|
|
borderRadius: 3,
|
|
minWidth: 96,
|
|
},
|
|
img: {
|
|
width: 64,
|
|
height: 64,
|
|
},
|
|
});
|