Skip to content

Commit 5de3bca

Browse files
committed
Fix TriggeringView when not directly child of ScrollView
1 parent a2a8fff commit 5de3bca

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

src/ImageHeaderScrollView.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ class ImageHeaderScrollView extends Component {
4949
super(props);
5050
this.state = {
5151
scrollY: new Animated.Value(0),
52+
pageY: 0,
5253
};
5354
}
5455

5556
getChildContext() {
56-
return { scrollY: this.state.scrollY };
57+
return {
58+
scrollY: this.state.scrollY,
59+
scrollPageY: this.state.pageY + this.props.minHeight,
60+
};
5761
}
5862

5963
/*
@@ -76,7 +80,6 @@ class ImageHeaderScrollView extends Component {
7680
this.getScrollResponder().scrollTo(...args);
7781
}
7882

79-
8083
interpolateOnImageHeight(outputRange) {
8184
const headerScrollDistance = this.props.maxHeight - this.props.minHeight;
8285
return this.state.scrollY.interpolate({
@@ -138,9 +141,13 @@ class ImageHeaderScrollView extends Component {
138141
const headerScrollDistance = this.props.maxHeight - this.props.minHeight;
139142
const scrollViewProps = _.pick(this.props, _.keys(ScrollView.propTypes));
140143
return (
141-
<View style={styles.container}>
144+
<View
145+
style={styles.container}
146+
ref={(ref) => { this.container = ref; }}
147+
onLayout={() => this.container.measureInWindow((x, y) => this.setState({ pageY: y }))}
148+
>
142149
<ScrollView
143-
ref={(sv) => { this[SCROLLVIEW_REF] = sv; }}
150+
ref={(ref) => { this[SCROLLVIEW_REF] = ref; }}
144151
style={[styles.container, { marginTop: this.props.minHeight }]}
145152
scrollEventThrottle={16}
146153
onScroll={Animated.event(
@@ -188,6 +195,7 @@ ImageHeaderScrollView.defaultProps = {
188195

189196
ImageHeaderScrollView.childContextTypes = {
190197
scrollY: React.PropTypes.instanceOf(Animated.Value),
198+
scrollPageY: React.PropTypes.number,
191199
};
192200

193201
export default ImageHeaderScrollView;

src/TriggeringView.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class TriggeringView extends Component {
77
constructor(props) {
88
super(props);
99
this.state = {
10-
layout: {},
1110
touched: false,
1211
hidden: false,
1312
};
@@ -29,30 +28,28 @@ class TriggeringView extends Component {
2928
nextContext.scrollY.addListener(this.onScroll);
3029
}
3130

32-
onLayout(e) {
33-
this.layout = this.setState({
34-
layout: e.nativeEvent.layout,
35-
bottom: e.nativeEvent.layout.y + e.nativeEvent.layout.height,
31+
onScroll() {
32+
this.containerView.measure((x, y, width, height, pageX, pageY) => {
33+
this.triggerEvents(this.context.scrollPageY, pageY, pageY + height);
3634
});
3735
}
3836

39-
onScroll(e) {
40-
const value = e.value;
41-
if (!this.state.touched && value >= this.state.layout.y) {
37+
triggerEvents(value, top, bottom) {
38+
if (!this.state.touched && value >= top) {
4239
this.setState({ touched: true });
4340
this.props.onBeginHidden();
4441
this.props.onTouchTop(true);
45-
} else if (this.state.touched && value < this.state.layout.y) {
42+
} else if (this.state.touched && value < top) {
4643
this.setState({ touched: false });
4744
this.props.onDisplay();
4845
this.props.onTouchTop(false);
4946
}
5047

51-
if (!this.state.hidden && value >= this.state.bottom) {
48+
if (!this.state.hidden && value >= bottom) {
5249
this.setState({ hidden: true });
5350
this.props.onHide();
5451
this.props.onTouchBottom(true);
55-
} else if (this.state.hidden && value < this.state.bottom) {
52+
} else if (this.state.hidden && value < bottom) {
5653
this.setState({ hidden: false });
5754
this.props.onBeginDisplayed();
5855
this.props.onTouchBottom(false);
@@ -62,12 +59,12 @@ class TriggeringView extends Component {
6259
render() {
6360
const viewProps = _.pick(this.props, _.keys(View.propTypes));
6461
return (
65-
<Animated.View
66-
onLayout={e => this.onLayout(e)}
62+
<View
63+
ref={(view) => { this.containerView = view; }}
6764
{...viewProps}
6865
>
6966
{ this.props.children }
70-
</Animated.View>
67+
</View>
7168
);
7269
}
7370
}
@@ -92,6 +89,7 @@ TriggeringView.defaultProps = {
9289

9390
TriggeringView.contextTypes = {
9491
scrollY: React.PropTypes.instanceOf(Animated.Value),
92+
scrollPageY: React.PropTypes.number,
9593
};
9694

9795
export default TriggeringView;

src/__tests__/__snapshots__/TriggeringView.js.snap

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
exports[`TriggeringView renders correctly by default 1`] = `
22
<View
3+
onLayout={[Function]}
34
style={
45
Object {
56
"flex": 1,
@@ -24,8 +25,7 @@ exports[`TriggeringView renders correctly by default 1`] = `
2425
"paddingTop": 45,
2526
}
2627
}>
27-
<View
28-
onLayout={[Function]} />
28+
<View />
2929
</View>
3030
</ScrollView>
3131
<View
@@ -95,8 +95,7 @@ exports[`TriggeringView renders correctly by default 1`] = `
9595
`;
9696

9797
exports[`TriggeringView should display children 1`] = `
98-
<View
99-
onLayout={[Function]}>
98+
<View>
10099
<Text
101100
accessible={true}
102101
allowFontScaling={true}
@@ -106,7 +105,4 @@ exports[`TriggeringView should display children 1`] = `
106105
</View>
107106
`;
108107

109-
exports[`TriggeringView should dont't crash without ImageHeaderScrollView 1`] = `
110-
<View
111-
onLayout={[Function]} />
112-
`;
108+
exports[`TriggeringView should dont't crash without ImageHeaderScrollView 1`] = `<View />`;

src/__tests__/__snapshots__/index.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
exports[`ImageHeaderScrollView renders correctly by default 1`] = `
22
<View
3+
onLayout={[Function]}
34
style={
45
Object {
56
"flex": 1,

0 commit comments

Comments
 (0)