Skip to content
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

Added paging support to scrollview and ART #603

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
.idea
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"inline-style-prefixer": "^3.0.6",
"normalize-css-color": "^1.0.2",
"prop-types": "^15.5.10",
"react-timer-mixin": "^0.13.3"
"react-timer-mixin": "^0.13.3",
"@ecliptic/react-art": "0.15.4-fork.1"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the this fork instead of the one owned by FB?

https://www.npmjs.com/package/react-art

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out this issue reactjs/react-art#113
The npm package no longer worked with latest react version, unfortunately no one is publishing it :(

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll consider this once the official package can be used

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you fork and publish one yourself? We can use that

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No thanks. Let's get the official package updated

},
"devDependencies": {
"babel-cli": "^6.24.1",
Expand Down
2 changes: 2 additions & 0 deletions src/components/ART/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import ART from '@ecliptic/react-art';
export default ART;
224 changes: 224 additions & 0 deletions src/components/ScrollView/PagingScrollViewBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import React, {Component} from 'react';
import View from '../View';
import Animated from '../../apis/Animated';
import Easing from 'animated/lib/Easing';

const normalizeScrollEvent = (parent, xOffset) => ({
nativeEvent: {
contentOffset: {
get x() {
return xOffset;
},
get y() {
return 0;
}
},
contentSize: {
get height() {
return parent._contentHeight;
},
get width() {
return parent._contentWidth;
}
},
layoutMeasurement: {
get height() {
return parent._parentHeight;
},
get width() {
return parent._parentWidth;
}
}
},
timeStamp: Date.now()
});

export default class PagingScrollViewBase extends Component {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a new component?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it does not really use the base at all. Scroll events are raised using onTouchMove, you can snap browser scrolling. Switching the base seemed like a good idea to me. I'm open to ideas here since you know the code better

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about scrolling without touch?

static propTypes = {
onScroll: React.PropTypes.func
};

constructor(props) {
super(props);
this._onTouchStart = this._onTouchStart.bind(this);
this._onTouchEnd = this._onTouchEnd.bind(this);
this._onTouchMove = this._onTouchMove.bind(this);
this._totalOffset = 0;
this._startPos = 0;
this._currentSelPosition = 0;
this._scrollItemCount = 0;
this._contentWidth = 0;
this._contentHeight = 0;
this._parentWidth = 0;
this._parentHeight = 0;
this._currentOffset = new Animated.Value(0);
}

componentDidMount() {
this._measureContent();
}

componentDidUpdate() {
this._measureContent();
}

componentWillMount() {
this._currentOffset.addListener(this._offsetChange);
}

componentWillUnmount() {
this._currentOffset.removeListener(this._offsetChange);
}

//For some reason onLayout/measure callbacks are not working on the content view
_measureContent() {
this._contentWidth = this._contentRef.clientWidth;
this._contentHeight = this._contentRef.clientHeight;
this._updatePositions();
}

_updatePositions() {
this._scrollItemCount = Math.ceil(this._contentWidth / this._parentWidth);
this.maxPositiveTransform = this._contentWidth - this._parentWidth;
this.pixelThreshold = this._parentWidth / 3;
}

scrollTo(y?: number | { x?: number, y?: number, animated?: boolean },
x?: number,
animated?: boolean) {

if (typeof y === 'number') {
console.warn(
'`scrollTo(y, x, animated)` is deprecated. Use `scrollTo({x: 5, y: 5, animated: true})` instead.'
);
} else {
({x, y, animated} = y || {});
}
this._totalOffset = Math.min(this.maxPositiveTransform, x);
this._currentSelPosition = this._getPositionMetaForX(this._totalOffset);
this._scrollToCurrentPosition(animated);
}

scrollToEnd(options?: { animated?: boolean }) {
this.scrollTo({y: 0, x: this.maxPositiveTransform, animated: options.animated});
}

scrollWithoutAnimationTo(y: number = 0, x: number = 0) {
console.warn('`scrollWithoutAnimationTo` is deprecated. Use `scrollTo` instead');
this.scrollTo({x, y, animated: false});
}

_getCurrentTimeInSec() {
return new Date().getTime() / 1000;
}

_onTouchMove(e: Object) {
this.offset = this._startPos - e.touches[0].pageX;
let newOffset = this._totalOffset + this.offset;
if (newOffset < 0) {
this.offset = 0;
this._totalOffset = newOffset = 0;
} else if (newOffset > this.maxPositiveTransform) {
this.offset = 0;
this._totalOffset = newOffset = this.maxPositiveTransform;
}
this._currentOffset.setValue(-newOffset);
}

_onTouchStart(e: Object) {
this._startPos = e.touches[0].pageX;
this.animationStartTS = this._getCurrentTimeInSec();
}

_onTouchEnd(e: Object) {
const totalPixelsCovered = this.offset;
this._totalOffset += this.offset;
const moveVelocity = totalPixelsCovered / (this._getCurrentTimeInSec() - this.animationStartTS);
if (moveVelocity < -this.pixelThreshold) {
this._currentSelPosition = Math.max(0, this._currentSelPosition - 1);
} else if (moveVelocity > this.pixelThreshold) {
this._currentSelPosition = Math.min(this._scrollItemCount - 1, this._currentSelPosition + 1);
} else {
this._currentSelPosition = this._getPositionMetaForX(this._totalOffset);
}
this._scrollToCurrentPosition(true);
}

_getPositionMetaForX(x) {
return Math.round(x / this._parentWidth);
}

_scrollToCurrentPosition(enableAnim) {
const correctOffsetForPosition = this._parentWidth * this._currentSelPosition;
this._totalOffset = correctOffsetForPosition;
this.offset = 0;
if (enableAnim) {
Animated.timing(this._currentOffset, {
toValue: -correctOffsetForPosition,
easing: Easing.easeOut,
duration: 200
}).start();
}
else {
this._currentOffset.setValue(-correctOffsetForPosition);
}
}

_onContentLayout = (e) => {
this._contentWidth = e.nativeEvent.layout.width;
this._contentHeight = e.nativeEvent.layout.height;
this._updatePositions();
};

_onParentLayout = (e) => {
this._parentWidth = e.nativeEvent.layout.width;
this._parentHeight = e.nativeEvent.layout.height;
this._updatePositions();
};

_offsetChange = (e) => {
if (this.props.onScroll) {
this.props.onScroll(normalizeScrollEvent(this, -1 * e.value));
}
};

_setContentRef = x => {
this._contentRef = x.children[0].children[0];
};


render() {
const {
/* eslint-disable */
onScroll,
onMomentumScrollBegin,
onMomentumScrollEnd,
onScrollBeginDrag,
onScrollEndDrag,
removeClippedSubviews,
scrollEnabled,
scrollEventThrottle,
showsHorizontalScrollIndicator,
showsVerticalScrollIndicator,
style,
/* eslint-enable */
...other
} = this.props;
return (
<View
onLayout={this._onParentLayout}
onTouchEnd={this._onTouchEnd}
onTouchMove={this._onTouchMove}
onTouchStart={this._onTouchStart}
style={{flex: 1, overflow: 'hidden'}}
>
<Animated.View domRef={this._setContentRef}
style={{
flex: 1,
willChange: 'transform',
transform: [{translateX: this._currentOffset}]
}} {...other} />
</View>
);
}
}
5 changes: 3 additions & 2 deletions src/components/ScrollView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import ViewPropTypes from '../View/ViewPropTypes';
import ViewStylePropTypes from '../View/ViewStylePropTypes';
import React from 'react';
import { bool, element, func, number, oneOf } from 'prop-types';
import PagingScrollViewBase from "./PagingScrollViewBase";

const emptyObject = {};

Expand Down Expand Up @@ -191,9 +192,9 @@ const ScrollView = createReactClass({
onResponderReject: this.scrollResponderHandleResponderReject
};

const ScrollViewClass = ScrollViewBase;
const ScrollViewClass = horizontal && pagingEnabled ? PagingScrollViewBase : ScrollViewBase;

invariant(ScrollViewClass !== undefined, 'ScrollViewClass must not be undefined');
invariant(ScrollViewClass !== undefined, 'ScrollViewClass must not be undefined');

if (refreshControl) {
return React.cloneElement(
Expand Down
2 changes: 2 additions & 0 deletions src/components/View/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class View extends Component {
const {
hitSlop,
style,
domRef,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had wrtiten a comment around this, onLayout does not fire and measure doesn't work on one of the views that I created. Needed dom node for clientWidth, let me know the alternative

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't have the time to look into the missing callbacks issue

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I don't think I will accept a patch that hacks around what is apparently another issue though

/* eslint-disable */
collapsable,
onAccessibilityTap,
Expand All @@ -52,6 +53,7 @@ class View extends Component {
const { isInAParentText } = this.context;

otherProps.style = [styles.initial, isInAParentText && styles.inline, style];
otherProps.ref = domRef;

if (hitSlop) {
const hitSlopStyle = calculateHitSlopStyle(hitSlop);
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
TouchableWithoutFeedback,
View,
VirtualizedList,
ART,

// propTypes
ColorPropType,
Expand Down Expand Up @@ -118,6 +119,7 @@ const ReactNative = {
TouchableWithoutFeedback,
View,
VirtualizedList,
ART,

// propTypes
ColorPropType,
Expand Down
1 change: 1 addition & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export {
} from './components/Touchable/TouchableWithoutFeedback';
export { default as View } from './components/View';
export { default as VirtualizedList } from './components/VirtualizedList';
export { default as ART } from './components/ART';

// propTypes
export { default as ColorPropType } from './propTypes/ColorPropType';
Expand Down